1

I'm using a MediaInfo CLI in a batch script and I don't know what the issue is, but I cannot get this command to work if I use a folder path that has spaces in it. I'm not new to batch scripts and have created hundreds over the years. Normally I would think that using double quotes would solve the issue, but I know it's something else related to using the "for /f" command and passing multiple arguments with double quotes. I've tried everything I could think of but still can't get it to work if I'm using a path with spaces. Without spaces it works just fine, just not with spaces.

Please Note that this is not the full batch script and is only a snippet of the offending code. I also changed the double "%%" variables to single "%" to make it easier for testing on the command line.

Also, in my batch script, instead of using "echo" I am outputting to a variable, which is why I must use the "for" command. All of which is irrelevant to this specific issue.

WORKING EXAMPLES

for /f %g in ('C:\MediaInfo.exe "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"') do echo %g

.

for /f "usebackq delims=" %g in (`C:\MediaInfo.exe "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"`) do echo %g

.

NOT WORKING EXAMPLES

for /f %g in ('"C:\folder with spaces\MediaInfo.exe" "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"') do echo %g

.

for /f "usebackq delims=" %g in (`C:\folder with spaces\MediaInfo.exe "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"`) do echo %g

Both of the "NOT WORKING" commands result in the same error...

'C:\folder' is not recognized as an internal or external command, operable program or batch file.

  • `for /f "USEBACKQ Tokens=* delims=" %g in ('"C:\folder with spaces\MediaInfo.exe" "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"') do echo %g` OR `for /f "USEBACKQ delims=" %g in ('"C:\folder with spaces\MediaInfo.exe" "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"') do echo %g` – T3RR0R May 19 '20 at 14:21
  • note the use of **'** as opposed to **`**, which in this context is for Sets containing commands. – T3RR0R May 19 '20 at 14:23
  • @T3RR0R, using `usebackq` together with `'` lets the part after `in` be interpreted as a literal string but not as a command to be executed; you will need the ` instead. Anyway, this does not help in the situation at hand... – aschipfl May 19 '20 at 14:33
  • Perhaps you misunderstand, I was referring to ` enabling commands. – T3RR0R May 19 '20 at 15:47

1 Answers1

3

The command within the set (that is the parenthesised part before do) of a for /F loop is actually executed by cmd /C, which may remove potential quotes depending on their positions. For instance, in the command line:

for /f "delims=" %g in ('"C:\folder with spaces\MediaInfo.exe" "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"') do echo %g

the outer pair of quotes is removed, leaving behind the invalid command line:

C:\folder with spaces\MediaInfo.exe" "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv

If you now add an additional pair of quotes, it will work. You may even escape these quotes not to have to alter any other escaping in case:

for /f "delims=" %g in ('^""C:\folder with spaces\MediaInfo.exe" "--Inform=General;%MenuCount%" "D:\Some Folder\filename.mkv"^"') do echo %g
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Well, it should work, though I don't know what `MediaInfo.exe` is, so I can't tell if its command line is wrong. To me the quoted part `"--Inform=General;%MenuCount%"` looks a bit strange -- maybe it should read `--Inform^=General^;%MenuCount%` (the escaping `^` is only necessary within the `for /F` loop!). What does variable `%MenuCount%` contain? Anyway, at first you should try the command line outside of `for /F`... – aschipfl May 19 '20 at 21:15
  • Sorry I posted my comment and then deleted it because I realized I made a mistake. But yeah, your solution is working great! I spent hours on this yesterday and couldn't figure it out. All the years I been using batch scripts and this is the first time I've came across this issue. I will definitely add it to my arsenal. =) – AppetiteForDestruction May 19 '20 at 22:12