I know how to redirect a Windows shell command with the >|>>|<|<<
operators, but I cannot accomplish it for commands used inside a FOR
command?
For instance:
for /f "usebackq tokens=*" %%I in (`__COMMAND__ 2>nul`) do (
set MYVAR=%%I
)
You see, here I would like to silent the stderr of this __COMMAND__
.
The shell complaints that it does not expect a 2
in that place (same behaviour for other redirections).
Anybody can help here?
Redirect the Standard Output to a Text File From Within a Batch File. To redirect the standard output to a text file, add the redirection operator between the command and the text file, as shown in the syntax below. For example, we have to redirect the output of the command powercfg to a text file named stdoutput. txt ...
To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
for /f "usebackq tokens=*" %%I in (`__COMMAND__ 2^>nul`) do (
set MYVAR=%%I
)
in this case redirection and conditional execution operators need to be escaped with caret.
Or put everything in double quotes :
for /f "usebackq tokens=*" %%I in (`"__COMMAND__ 2>nul"`) do (
set MYVAR=%%I
)
Using delayed expansion is also possible:
@echo off
setlocal enableDelayedExpansion
set "command=__COMMAND__ 2>nul"
for /f "usebackq tokens=*" %%I in (`!command!`) do (
set MYVAR=%%I
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With