如何设置命令输出作为一个批文件的可变 [英] How to set commands output as a variable in a batch file
本文介绍了如何设置命令输出作为一个批文件的可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能一个批处理文件语句的输出设置为变量,例如:
FINDSTR测试> %变量%回声%变量%
解决方案
FOR / F令牌= *有usebackq%% f由于(`command`)DO(
SET VAR = %%˚F
)
ECHO%VAR%
我总是使用usebackq所以,如果你有一个字符串插入或长文件名,您可以使用双引号没有搞砸了该命令。
现在,如果你的输出将包含多行,你可以做到这一点。
SETLOCAL ENABLEDELAYEDEXPANSION
SET计数= 1
FOR / F令牌= *有usebackq%% f由于(`command`)DO(
SET VAR!算!= %%˚F
SET /计数=!算!+1
)
ECHO%VAR1%
ECHO%VAR2%
ECHO%VAR3%
ENDLOCAL
Is it possible to set a statement's output of a batch file to a variable, for example:
findstr testing > %VARIABLE%
echo %VARIABLE%
解决方案
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
这篇关于如何设置命令输出作为一个批文件的可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文