Morrtz
Morrtz

Reputation: 47

Batch Loop through command result (command in a different folder)

I'm trying to Loop through results of a command with FOR /F.

The command to actually run - C:\Program Files\my folder\command.exe

%installdir% is C:\Program Files\my folder\

The code I run is -

FOR /F "delims=, tokens=1,2" %%c in (results.txt) DO (
  if %%d EQU [something] (
    FOR /F "delims=: skip=2" %%e in ('%installdir%command.exe /checkstatus Client %%c') do (
      echo %%e %%f
    )
  )
)

I'm guessing this is not running correctly because I need to use usebackq in some fashion, but I haven't managed to do so. Appreciate any help on this!

Upvotes: 0

Views: 2830

Answers (1)

MC ND
MC ND

Reputation: 70923

FOR /F "delims=: skip=2" %%e in ('"%installdir%command.exe" /checkstatus ....
                                  ^         quotes        ^

If your command includes spaces, you need to quote it.

The problem with this is that sametimes the cmd instance that is started to handle the command to execute removes some double quotes (the quotes we added) and at the end we get a new error (see cmd /? for more information)

To solve it, you will need to add ... more quotes.

FOR /F "delims=: skip=2" %%e in ('""%installdir%command.exe" /checkstatus ... "') do ....
        quoted executable          ^.......................^ 
        quoted full command       ^...........................................^

You will need usebackq when

  • You run a command that needs single quotes in it, so you use backquotes to enclose the command
  • You are reading a file and needs to use double quotes because it includes spaces/special chars. A double quoted string is handled as a direct string to process and need usebackq to indicate that it is a file name.

You can see both cases indicated if you execute for /?

Upvotes: 2

Related Questions