0

I'm trying to write a 2 line .bat file in Windows 7 to search recursively in the root folder and subfolders for pdfs and print them however the FORFILES command as in my file:

set fxread="C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe"
forfiles /s /m *.pdf /c "%fxread% /p @FILE"

Gives the following error:

ERROR: Invalid argument/option - 'Files'.

However if I just use FORFILES to echo the filename I get as expected a list of pdf files; i.e this code:

forfiles /s /m *.pdf /c "cmd /c echo @FILE"

Produces a list of pdf files, one per line and encased in quotation marks.

Anyone know why I can't pass these files to the PDF viewer for printing? Note that this command does print a pdf file as expected:

%fxread% /p "FILE_NAME.pdf"

1 Answers1

0

When you wrap a string in quotes to preserve the spaces, the quotes become part of the string. Combine that with the quotes required by forfiles, and you're passing the command forfiles /s /m *.pdf /c ""C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe" /p @FILE"

Place the first quote before fxread to avoid including quotes in the string. Additionally, spaces, parentheses, and the hex equivalents of quotes need to be escaped.

set "fxread=^0x22C:\Program^ Files^ ^(x86^)\Foxit^ Software\Foxit^ Reader\Foxit^ Reader.exe^0x22^ /p"
forfiles /s /m *.pdf /c "cmd /c %fxread% @path"
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Roger that about the quotes - I've looked at your post and mine and am having a blind minute where I can – user2727391 Oct 30 '14 at 04:20
  • Roger that about the quotes - I'm having a blind moment now and can't spot the difference between mine and your command but I've tried your fix and now just get `ERROR: The system cannot find the file specified.` - again something to do with the output of `FORFILES` having quotes? – user2727391 Oct 30 '14 at 04:23
  • I forgot that system commands indicated by `/c` should be preceded by `cmd /c`. I've edited my code above. – SomethingDark Oct 30 '14 at 04:32
  • And Windows, god bless it, now tells me it cannot find 'C:\Program' so it looks to me like the quotes which were breaking the command are necessary to not break the command *bashes head on wall* – user2727391 Oct 30 '14 at 04:36
  • I finally found [this answer](http://stackoverflow.com/a/508937/4158862) which indicated that I needed to escape some characters in order for it to work. – SomethingDark Oct 30 '14 at 04:55
  • 1
    Problem solved - wow that was unexpectedly complicated. I cannot understand how a human being is expected to use the hex code for the quotation mark! You amaze me and your help is much appreciated SomethingDark. – user2727391 Oct 30 '14 at 05:28