-1

I am writing a batch script around the qpdf executable. The end goal is to be able to drag and drop PDF files on the batch script and process each PDF using qpdf.exe.

I have managed to write a batch script to accept the drag and drop of multiple files (Hopefully a folder full of files later) and loop through all of the files printing the file to the screen.

I have been unable to get the batch script to execute the qpdf command so far. I think my problem is the qpdf command line switches, they use "--" instead of "/". Also, the use of spaces and punctuation is present in the file paths and names. (Thank you end users who don't listen. You know who you are.)

I will attach the script I have so far.

If someone could help me understand what I am doing wrong, I would greatly appreciate it.

@ECHO off

REM For each argument (file in my case)
FOR %%a IN (%*) DO (
    REM If the path turns out to be a folder
    IF EXIST %%a\* (
        ECHO Skipping directory
        ECHO %%a
        ECHO;
    REM If not a folder
    ) ELSE (
        ECHO %%a was dropped on me
        ECHO;
        REM This is the command to be run
        REM qpdf.exe --rotate=+180 --replace-input "C:\Users\user\desktop\SO-005553 7-2-2020.pdf"
        REM
        REM define the executable path and executable and command switches and input file
        REM %~dp0 is file path of this script
        REM qpdf.exe is executable
        REM $$~a is the input file
        SET command=""%~dp0qpdf.exe"" --rotate=+180 --replace-input ""%%~a""
        ECHO %command%
        ECHO;
        START "" %command%
    )
)

PAUSE

For some reason, %command% will not echo or execute.

It appears to be empty.

However, if I turn @ECHO on, I can see that the set command is correct.

I have tried 3 different ways to execute the command.

One of them threw a fit about the command not found, one of them opened a command window, and this one appears to do nothing.

  • 4
    Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) Specifically, you add `setlocal enabledelayedexpansion` to the top of your script and use `!command!` instead of `%command%`. Or you can just run the command directly instead of setting it to a variable. – SomethingDark May 09 '22 at 21:05
  • @SomethingDark: I just tried your solution with no luck. That is a good read and I didn't think about searching for that before. @Mofi: I thought I had tried this solution, however upon trying this solution again the script is working. My end solution is a hybrid of SomethingDark's and Mofi's solutions. I used `SETLOCAL enabledelayedexpansion` and the variable changes from SomethingDark. I used Mofi's changes to the command. – CheesyTech May 10 '22 at 13:18

0 Answers0