Page 1 of 1

Need help with misbehaving DOS code

Posted: 28 Apr 2024 14:25
by OM3
I made a batch file to optimise PDF files with Ghostscript...
The first version adds '-compressed' to the newly created PDF file.
I wanted to make the same code but overwrite the original.
I've tried using Google Gemini and Chat GPT, the code they give seems to be OK when I read through... but doesn't work.

One thing... the code should pause at the end... for some reason it doesn't pause. How can I change to make it pause? I don't understand why it doesn't pause.
When the new code doesn't work, there must be errors thrown - but I can't read these because the DOS window doesn't pause.

Here is the original code I have:

Code: Select all

@ECHO OFF

REM Loop through each PDF file passed as arguments
:loop
IF "%~1"=="" GOTO :eof

REM Compose the output filename
SET "outputFile=%~n1-compressed.pdf"

REM Process the PDF and output to the unique filename
"C:\Program Files\gs\gs10.03.0\bin\gswin64c.exe" -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dSAFER -sOutputFile="%outputFile%" "%~1"

SHIFT
GOTO :loop

ECHO Finished processing PDFs.
TIMEOUT /T 20
Hoping someone can help.

Thanks.

Re: Need help with misbehaving DOS code

Posted: 29 Apr 2024 08:13
by OJBakker
The timeout command is never reached in your code.
You end your code with the 'goto :eof' line which stops the processing.
You can add a label before the timeout-command and change the goto :eof to jump to this new label.
Or move the timeout-command to within the if-command so the timeout will be executed before the 'goto :eof'.

Re: Need help with misbehaving DOS code

Posted: 03 May 2024 16:36
by Squashman
Test if you have shifted out all the arguments before you goto the loop.

Code: Select all

SHIFT
IF NOT "%~1"=="" GOTO loop
And the first two rules of debugging a batch file.
1) Do not run the batch file with your mouse. Open up a command prompt and run it from there.
2) Use ECHO ON at the top of your script.