2015-04-04 37 views
0

嘿,我现在有这个代码把不同的文本不同的文本文件到变量,然后输出到一个文件:批次中的每个字符串后空间输出

@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 

但输出具有各一个空格(whitline) :

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

不知道为什么会发生这种情况。

回答

2

它是位于>>>重定向器之前的空格。删除它如下(dbenham's benefitting comment方面编辑):

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

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

说明:如果变量%minecraft-version%结束

  • @echo %minecraft-version%>> build.properties可以打破输出单个数字前有一个空格;
  • >> 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%" 

+0

它的工作非常感谢! – StrayanDropbear 2015-04-04 18:40:53

+0

@ user3327050请“接受”这个答案来衡量你的感激之情。这就是StackOverflow的工作原理! – 2015-04-04 18:45:43

+0

@Stephan是的,但是您的声明仅适用于在“set”变量= value“'中出现的第一个”=“。而'value'包含'='这里由空间包围的设计! – JosefZ 2015-04-04 18:45:50

1

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

@echo three >testbat.txt 

产生包含与一个尾随空间中的直线"three "的文本文件。但是这一个

@echo three>testbat.txt 

生成包含线"three"没有尾随空间的文本文件。

+0

太晚了,但是无论如何张贴。 – 2015-04-04 18:43:32

相关问题