Batch - Store command output to a variable (multiple lines)

14,232

You can put it into a single variable with including linefeeds.

setlocal EnableDelayedExpansion
set LF=^


REM The two empty lines are required here
set "output="
for /F "delims=" %%f in ('dir /b') do (
    if defined output set "output=!output!!LF!"
    set "output=!output!%%f"
)
echo !output!

But it can be a bit tricky to handle the data later, because of the embedded linefeeds.
And there is still a limit of 8191 characters per variable.

Often it's easier to use an array.

setlocal EnableDelayedExpansion
set "output_cnt=0"
for /F "delims=" %%f in ('dir /b') do (
    set /a output_cnt+=1
    set "output[!output_cnt!]=%%f"
)
for /L %%n in (1 1 !output_cnt!) DO echo !output[%%n]!
Share:
14,232

Related videos on Youtube

ozcanovunc
Author by

ozcanovunc

My favorite class in C++: class A {};

Updated on September 15, 2022

Comments

  • ozcanovunc
    ozcanovunc over 1 year

    I know a way of doing this which is a bit like cheating but the code below creates a temporary file and then deletes it. I don't want that happen. So is there a proper or better way of doing it?

    command 2>> "temp"
    set /p OUTPUT=<"temp"
    del "temp"
    echo %OUTPUT%
    

    I know there is a solution which uses for loop but that doesn't work for commands which return more than one line of result. I want to store all of them into my variable. (I tried this code btw)

    • ozcanovunc
      ozcanovunc almost 9 years
      That would be useful. How do we store them into array of variables?
  • ozcanovunc
    ozcanovunc almost 9 years
    That prints everything ok but I want to store them. When I try to set %%i into a variable like do set var=%%i it stores only the last line.
  • ozcanovunc
    ozcanovunc almost 9 years
    I modified your loop's body and now that works for the command I'm using but for example it doesn't work for "dir" command. do ( <nul set /p "output=%output%%%i" )
  • Stephan
    Stephan almost 9 years
    see the delayed expansion trap. Besides that, a varaible's value can't have linefeeds. set output=!output!%%i will concatenate all lines into a one-line-value (you don't need /p here and therefore also no <nul.
  • jeb
    jeb almost 9 years
    @Stephan Why a variable shoudn't have linefeeds included? There is no such limitation
  • ozcanovunc
    ozcanovunc almost 9 years
    Both of them works however I prefer the second one as well. Thank you!
  • Stephan
    Stephan almost 9 years
    @jeb: correct, but creating and working with such variables is a pain. Variables in batch are simply not designed to have linefeeds, although Implementing them works, with some experience. Using "arrays" should be preferred.
  • jeb
    jeb almost 9 years
    @Stephan From this point of view, you're right. It's not very easy and a little bit error prone
  • Foad S. Farimani
    Foad S. Farimani over 3 years
    @jeb Are you the one who wrote this batch include library?
  • jeb
    jeb over 3 years
    @Foad Yes, please talk in chat