在每个字符串后批量输出空格

时间:2015-04-04 18:29:49

标签: batch-file

嘿,我目前有这个代码将不同文本文件中的不同文本转换为变量,然后输出到一个文件中:

@echo off
echo system starting..
cls
echo grabbing versions 
cd C:\Scripts\Bamboo
FOR /F "tokens=1 delims==" %%n IN (CurrentBuild.txt) DO SET build-version-number=%%n
cd C:\Users\Administrator\bamboo-home\xml-data\build-dir\MC-CC-MAIN
FOR /F "tokens=1 delims==" %%n IN (MinecraftVersion.txt) DO SET minecraft-version-number=%%n
FOR /F "tokens=1 delims==" %%n IN (ForgeVersion.txt) DO SET forge-version-number=%%n
FOR /F "tokens=1 delims==" %%n IN (ModVersion.txt) DO SET mod-version-number=%%n
cls
echo setting versions to variables
set mod-version=mod_version = %mod-version-number%
set forge-version=forge_version = %forge-version-number%
set build-version=build_version = %build-version-number%
set minecraft-version=minecraft_version = %minecraft-version-number%
cls
echo outputing variables to build.properties
del build.properties
@echo %minecraft-version% >> build.properties
@echo %forge-version% >> build.properties
@echo %mod-version% >> build.properties
@echo %build-version% >> build.properties
cls
echo done, exiting inject script 
exit

但输出后面的空格(白线):

"minecraft_version = 1.7.10 "
"forge_version = 10.13.2.1291 "
"mod_version = v1.0 "
"build_version = 33 "

不确定为什么会这样。

2 个答案:

答案 0 :(得分:2)

它是>>>重定向器之前的空格。按照以下方式删除它(关于dbenham's benefitting comment已编辑):

(@echo %minecraft-version%)>> build.properties

>> build.properties (@echo %minecraft-version%)

说明:

  • @echo %minecraft-version%>> build.properties如果变量%minecraft-version% 结束并且前面带有空格的单个数字,则可能会中断输出;
  • >> build.properties @echo %minecraft-version%不够,因为该行可能包含不需要的(遗忘的)尾随空格。

此外,要完全控制变量名称和值中的前导和尾随空格,请在set命令中使用双引号,如下所示:

set "variable=value"

想出别人:

set "variable= this value contains a leading space"
set "variable= this value surrounded with spaces "
set "variable=this value contains a trailing space "
set "variable =this variable name contains a trailing space"

应用于脚本中的某些命令:

FOR /F "tokens=1 delims==" %%n IN (ModVersion.txt) DO SET "mod-version-number=%%n"
:::
set "mod-version=mod_version = %mod-version-number%"

答案 1 :(得分:1)

这是因为批处理文件中的空间。这个测试文件

@echo three >testbat.txt

生成一个文本文件,其中包含带有尾随空格的行"three "。但这一个

@echo three>testbat.txt

生成一个文本文件,其中包含没有尾随空格的行"three"