繁体   English   中英

如何使用批处理脚本在变量中设置带空格的字符串(来自文件的内容)

[英]How to set string with spaces (from contents of a file) in a variable using batch script

我有这段代码可以读取文件的内容,并将所有内容设置在一个变量中。

for /f "tokens=1,*" %%h in ('type C:\user\userfiles\title.txt') do (
set title=%%h
)echo %title%

title.txt的示例内容

AAAA BBBB CCCC

现在,它总是在第一个空格之前显示单词。 如何以变量标题显示文件的所有内容?

您需要指定FOR参数中不应包含定界符,否则将使用空格,并且只能选择第一个值:

REM Pick up a single token with no delimiters.
REM This will read the entire line of the file.
for /f "usebackq tokens=* delims=" %%h in (`type "C:\user\userfiles\title.txt"`) do set title=%%h
echo %title%

请注意,如果文件有多行,则仅将最后一行设置为%title%变量。

如果您想转过来使用%title%的结果作为程序的参数,请确保将其放在引号中,因为它可能包含空格:

SomeProgram.exe "%title%"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM