2015-04-04 48 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

相關問題