windows 将 ECHO / TYPE 输出设置为批处理文件中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6226884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:59:05  来源:igfitidea点击:

Set ECHO / TYPE output as a Variable in Batch Files

windowsbatch-file

提问by Jay

Is it possible to set the output of TYPE or ECHO as a variable in a batch file?

是否可以将 TYPE 或 ECHO 的输出设置为批处理文件中的变量?

回答by Joey

Convoluted, and it only works for a single line, but general:

令人费解,它只适用于单行,但一般:

for /f "delims=" %%x in ('some command with output') do set "Var=%%x"

For echoyou don't need to do anything special, just change

因为echo你不需要做任何特别的事情,只需改变

echo Foo

into

进入

set Var=Foo

And for files there is also the option of either

对于文件,也可以选择

set /p Var=<file.txt

or

或者

for /f "delims=" %%x in (file.txt) do set "Var=%%x"