Search code examples
batch-fileprintingforfiles

Batch script Mix Forfiles Cmd to print a PDF


I'm stuck with this script

echo off
SET pathAdobe="C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
SET pathDestination=T:\
cd %pathDestination%

(1)
forfiles /P %pathDestination% /M *8.pdf /D +0 /C "cmd /c echo @PATH"
(2)
"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe" /o /h /s /t "%pathDestination%\pdf8.pdf" "MyPrinterName"


pause

(1) Work fine, i got a list of pdf according my forfiles

(2) Work fine, print my file

(3) But when i want to mix the 2 first step that doesn't work like i want

forfiles /P %pathDestination% /M *8.pdf /D +0 /C "CMD /C "%pathAdobe%" /o /h /s /t @PATH"

I got this error: Error: Invalid argument or option - « Files\Adobe\Acrobat »

I try to escape with ^ " \ but don't change the result

Can't find a solution!

Thanks for any help you can give me :)

J


Solution

  • Your issue is that you are including double quotes, in the wrong places, and that those double quotes require escaping. You can escape those using backward slashes (\"), or by using their hexadecimal character code, (0x22).

    Backward slash example:

    @Echo Off
    Set "pathAdobe=%ProgramFiles%\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
    Set "pathDestination=T:\"
    CD /D "%pathDestination%" 2> NUL || Exit /B
    %SystemRoot%\System32\forfiles.exe /M "*8.pdf" /D 0 /C "%SystemRoot%\System32\cmd.exe /D /C \"\"%pathAdobe%\" /o /h /s /t @Path \"MyPrinterName\"\""
    Pause
    

    Hexadecimal character example:

    @Echo Off
    Set "pathAdobe=%ProgramFiles%\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
    Set "pathDestination=T:\"
    CD /D "%pathDestination%" 2> NUL || Exit /B
    %SystemRoot%\System32\forfiles.exe /M "*8.pdf" /D 0 /C "%SystemRoot%\System32\cmd.exe /D /C 0x220x22%pathAdobe%0x22 /o /h /s /t @Path 0x22MyPrinterName0x220x22"
    Pause