Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WMIC call in BATCH FOR command Returns unexplained Error

I am trying to create a Volume Shadow copy from a BATCH file but have encountered a issue I am unable to explain.

When running the below command:

FOR /F "tokens=3" %%A IN ('"WMIC shadowcopy call create ClientAccessible,"C:\""^| FIND /I "ShadowID"') DO SET ID=%%A

I receive the below error

Invalid format. Hint <paramlist> = <param> [, <paramlist>].

but if I replace FIND with FINDSTR and remove the quotes from around the word I am searching for it seems to work correctly.

FOR /F "tokens=3" %%A IN ('"WMIC shadowcopy call create ClientAccessible,"C:\""^| FINDSTR /I ShadowID') DO SET ID=%%A

Can anyone explain to me why the first command will not work or what I am overlooking to make the first command work?

Yes, I know there are many other, including better, ways to achieve what I am wanting to do but for now I am just trying to better understand what I am missing here.

Thanks greatly

like image 410
MTHome2010 Avatar asked Mar 06 '26 03:03

MTHome2010


1 Answers

There are two problems in your code:

  1. You stumbled upon a strange behaviour of FOR /F: it does not only remove the single quotes (apostrophies, '') around the command to parse, it also removes enclosing quotation marks if the first and last character of the remaining string are " both1, so the command line that is actually tried to be executed is WMIC shadowcopy call create ClientAccessible,"C:\""| FIND /I "ShadowID, which is invalid of course.
  2. The WMIC command line is enclosed in quotation marks, so even if the above issue was not there, your code would fail, because the command interpreter tries to find a command or program named "WMIC shadowcopy call create ClientAccessible,", which does not exist of course.

So the corrected command line is:

FOR /F "tokens=3" %%A IN ('WMIC shadowcopy call create ClientAccessible^,"C:\" ^| FIND /I "ShadowID"') DO SET "ID=%%A"

Note that you need to escape the , herein (similar to the pipe).

1) You can affirm that by trying the command line FOR /F "tokens=3" %%A IN ('"WMIC" shadowcopy call create ClientAccessible^,"C:\" ^| FIND /I "ShadowID"') DO SET "ID=%%A", which fails, and FOR /F "tokens=3" %%A IN ('^""WMIC" shadowcopy call create ClientAccessible^,"C:\" ^| FIND /I "ShadowID"^"') DO SET "ID=%%A", which works because there are additional "" put around (they are escaped like ^" so that the command line does not need to be altered in terms of escaping; you could also state the " literally, but then you needed to unescape the , and the |).


In addition, I recommend to nest another FOR /F loop inside yours, because WMIC returns Unicode text, which could leave artefacts (orphaned carriage-return characters) when being parsed by FOR /F (as it expects ANSI text); an additional parsing phase eliminates such noise:

FOR /F "tokens=3" %%A IN ('WMIC shadowcopy call create ClientAccessible^,"C:\" ^| FIND /I "ShadowID"') DO FOR /F "delims=" %%B in ("%%A") DO SET "ID=%%B"

This method is credited to dbenham -- see his answer to Why is the FOR /f loop in this batch script evaluating a blank line? and also his external article WMIC and FOR /F : A fix for the trailing <CR> problem.

like image 94
aschipfl Avatar answered Mar 08 '26 21:03

aschipfl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!