Need help with misbehaving DOS code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Need help with misbehaving DOS code

#1 Post by OM3 » 28 Apr 2024 14:25

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.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Need help with misbehaving DOS code

#2 Post by OJBakker » 29 Apr 2024 08:13

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'.

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Need help with misbehaving DOS code

#3 Post by Squashman » 03 May 2024 16:36

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.

Post Reply