0

First I will explain the scenario:

In a machine which has the PATH environment variable corrupted/empty (no directory specified) I need to run a Batch-Script to repair and return some settings to defaults, then obviouslly the Script needs to work in a console environment even with the PATH empty so I need to specify the absolute path for each com/exe file.

Well, understanding what I've said above, why the expression inside the FOR is not working and throws a syntax error?

For /F "tokens=*" %%# in (
    '"%SystemRoot%\System32\WBEM\WMIC.exe" useraccount where name^=^'%UserName%^' Get SID ^| "%SystemRoot%\System32\FINDSTR.exe" "[0-9]-[0-9]"'
) Do (
    Echo %%#
)

If I remove the absolute path of the FINDSTR command it works again as normally.

PS: I know that one solution could be using a CSV format on the WMIC query to parse the output instead trying to redirect the output to FINDSTR, but anyways I need to find a solution about the redirection for other loops where I'm having the same problem.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

1 Answers1

1

The problem are the quotes

For /F "tokens=*" %%# in (
    '""%SystemRoot%\System32\WBEM\WMIC.exe" useraccount where name^=^'%UserName%^' Get SID | "%SystemRoot%\System32\FINDSTR.exe" "[0-9]-[0-9]""'
) Do (
    Echo %%#
)

I had the same problem, and here is the solution and why the problem raises.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126