Platform Windows XP
When writing a command file (.bat) how can i “catch” the output from a command into a variable ?
I want to do something like this
SET CR='dir /tw /-c b.bat | findstr /B "[0-9]"'
But this do not work
Regards Stefan
PS
No, I can not dowload grep, cygwin or any other software, it have to be the CMD
DS
You can use
FOR /Fand go through some loops inside a batch file to capture the output:It is especially important that there are two newlines between
!var!^and the%%ibelow.Additionally you need to escape (again using
^) all characters inside the command line forFORthat have special meaning to the shell, such as the pipe in this instance.The solution works by iterating over the output of the command and appending each line to the contents of
varincrementally. To do that in a somewhat convenient manner the script enables delayed variable expansion (the!var!syntax).