How to redirect a FOR command to a Windows package

I know how to redirect a Windows shell command with the operators >|>>|<|<< , but I canโ€™t execute it for commands used inside the FOR command?

For instance:

 for /f "usebackq tokens=*" %%I in (`__COMMAND__ 2>nul`) do ( set MYVAR=%%I ) 

You see, here I would like to disable the stderr of this __COMMAND__ . The corps complains that it does not expect at this point 2 (the same behavior for other redirects).

Can anyone help here?

+5
source share
1 answer
 for /f "usebackq tokens=*" %%I in (`__COMMAND__ 2^>nul`) do ( set MYVAR=%%I ) 

in this case, the redirection and conditional execution statements must be escaped with a caret.

Or put everything in double quotes:

 for /f "usebackq tokens=*" %%I in (`"__COMMAND__ 2>nul"`) do ( set MYVAR=%%I ) 

Using delayed extension is also possible:

 @echo off setlocal enableDelayedExpansion set "command=__COMMAND__ 2>nul" for /f "usebackq tokens=*" %%I in (`!command!`) do ( set MYVAR=%%I ) 
+5
source

Source: https://habr.com/ru/post/1243681/


All Articles