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

前端 未结 9 1269
我在风中等你
我在风中等你 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:52

    FOR /F "tokens=* USEBACKQ" %%F IN (`command`) DO (
    SET var=%%F
    )
    ECHO %var%
    

    I always use the USEBACKQ so that if you have a string to insert or a long file name, you can use your double quotes without screwing up the command.

    Now if your output will contain multiple lines, you can do this

    SETLOCAL ENABLEDELAYEDEXPANSION
    SET count=1
    FOR /F "tokens=* USEBACKQ" %%F IN (`command`) DO (
      SET var!count!=%%F
      SET /a count=!count!+1
    )
    ECHO %var1%
    ECHO %var2%
    ECHO %var3%
    ENDLOCAL
    

提交回复
热议问题