2

Im making a windows batch file to run 7zip command line to compress a set of files. The part after /C in the script below needs to be quoted according to documentation. The issue comes from already having double quotes around the file paths. Do I need to \ escape certain quotes, use extra quotes, single quotes? Nothing I've tried has worked.

Here's the command part after /c:

/c "c:\program files\data\7-zip\7z" a "c:\program files\data\backups\test1.7z" "c:\program files\data\backups\test1.txt" -mmt1

The Whole Script (in case needed):

forfiles /p "c:\program files\data\backups" /m *.txt /c "c:\program files\data\7-zip\7z" a "c:\program files\data\backups\test1.7z" "c:\program files\data\backups\test1.txt" -mmt1
JMC
  • 1,700
  • 7
  • 25
  • 34
  • 2
    use `"` for the outer quote (whole command line) and `0x22` for the inner "quotes" (that's special to `forfiles` only!): `forfiles .... /c "0x22c:\folder\7z.exe0x22 a 0x22c:\....7z0x22 0x22c:\....txt0x22 -mmt1" – Stephan May 14 '19 at 12:20
  • 1
    Why do you use `forfiles` when you don't use any of its variables `@file`, `@path` anyway? – aschipfl May 14 '19 at 12:22
  • @aschipfl, I'm not expert on this. Assumption on my part, using forfiles to perform the command on each individual file. Theres probably a better way – JMC May 14 '19 at 12:25
  • 2
    Open up the Command Prompt, and enter `forfiles /?` to read its usage information. You'll clearly see that the command string following the `/C` option should be doublequoted. Any doublequotes within that command string itself can be replaced with their hex string equivalent, `0x22`, e.g. `FORFILES /P "C:\Program Files\data\backups" /M *.txt /C "0x22C:\Program Files\data\7-zip\7z.exe0x22 a 0x22C:\Program Files\data\backups\test1.7z0x22 0x22C:\Program Files\data\backups\test1.txt0x22 -mmt1"`. However it would be more efficient if ran from a `For` loop instead, enter `for /?` for its usage etc. – Compo May 14 '19 at 12:28
  • 2
    try a simple [for](https://ss64.com/nt/for.html) loop, like: `for %%a in ("c:\program files\data\backups\*.txt") do echo %%a`. `forfiles` isn't *that* user-friendly... – Stephan May 14 '19 at 12:28
  • 1
    `forfiles` is a nasty beast as it doesn't treat `/C` command line arguments properly as described [here](https://stackoverflow.com/a/26990159); so @Stephan's suggestion to use `0x22` might still fail because of that design flaw... – aschipfl May 14 '19 at 12:29
  • @stephan the 0x22 did fail, but I "think" it failed because 7zip couldn't understand the 0x22. The batch file did execute 7zip. 7zip returned "unsupported command: files". files is the first word after a space. – JMC May 14 '19 at 12:41
  • yeah - `forfiles` looks quite "built-in-a-hurry" to me. Does the simpler `for` work for you? – Stephan May 14 '19 at 12:57
  • @Stephan, yes I got it working, thanks for checking. "For" is a more intuitive alternative to "forfiles" in this case. – JMC May 14 '19 at 14:46

1 Answers1

2

I faced the same problem and here is a concept how I solved it for my case:

@FORFILES /s /m *.ext /c "cmd /c \"\"C:\Program Files\Blah\blah-blah-blah.exe\" -o1 --option2 /option3 @file\"" >%~n0.log 2>&1

Quite tricky but it works!

Vitaly
  • 121
  • 1
  • 6
  • You are such a life saver to me, thank you, it works! The suggestion to use 0x22 looked right when echo'ing it, but the final command would still lose the double quotes for unknown reason, but using `\"` is the key here. – Martin Braun Mar 10 '20 at 23:27