How to set commands output as a variable in a batch file

前端 未结 9 1385
我在风中等你
我在风中等你 2020-11-22 01:19

Is it possible to set a statement\'s output of a batch file to a variable, for example:

findstr testing > %VARIABLE%

echo %VARIABLE%
9条回答
  •  一个人的身影
    2020-11-22 01:41

    I found this thread on that there Interweb thing. Boils down to:

    @echo off 
    setlocal enableextensions 
    for /f "tokens=*" %%a in ( 
    'VER' 
    ) do ( 
    set myvar=%%a 
    ) 
    echo/%%myvar%%=%myvar% 
    pause 
    endlocal 
    

    You can also redirect the output of a command to a temporary file, and then put the contents of that temporary file into your variable, likesuchashereby. It doesn't work with multiline input though.

    cmd > tmpFile 
    set /p myvar= < tmpFile 
    del tmpFile 
    

    Credit to the thread on Tom's Hardware.

提交回复
热议问题