2014-09-28 49 views
0

以下for循環結果與我的機器上的預期不符。 我無法找出我做錯了什麼。For循環結果與批處理文件中的預期不符

請在下面的代碼清單中找到check.bat和softwareList.txt。 softwareList.txt也位於check.bat所在的同一目錄中。

check.bat

@echo off 
call:updateVariables check 
goto:eof 

:updateVariables 
set operation=%~1 
echo %operation% 
setlocal enabledelayedexpansion 
for /F "tokens=*" %%A in (softwareList.txt) do ( 
set bundle=%%A 
echo a. command %%A %operation% 
echo b. command %bundle% %operation% 
) 
endlocal 
goto:eof 

及以下softwareList.txt: -

maven 
tomcat 
derby 
java 
eclipse 

預期結果: -

check 
a. command maven check 
b. command maven check 
a. command tomcat check 
b. command tomcat check 
a. command derby check 
b. command derby check 
a. command java check 
b. command java check 
a. command eclipse check 
b. command eclipse check 

實際結果: -

check 
a. command maven check 
b. command java check 
a. command tomcat check 
b. command java check 
a. command derby check 
b. command java check 
a. command java check 
b. command java check 
a. command eclipse check 
b. command java check 

此外,如何修剪從for循環迭代結果拖尾和先行空間? 我在for循環使用以下命令和一個不工作: -

set A=%A:~0,-1% 

回答

0
@echo off 
call:updateVariables check 
goto:eof 

:updateVariables 
set operation=%~1 
echo %operation% 
setlocal enabledelayedexpansion 
for /F "tokens=*" %%A in (softwareList.txt) do ( 
set bundle=%%A 
echo a. command %%A %operation% 
echo b. command !bundle! %operation% 
) 
endlocal 
goto:eof 

在延遲擴展,當一個變量定義和括號內的訪問 - !應該被用來代替%

+0

謝謝你快速的回覆。我apppreciate你的時間。現在我在文件中,然後去排球;) – mogli 2014-09-28 19:13:22

+0

我添加了一個語句來修剪前導和尾隨空格,但這是行不通的。 set bundle =!bundle:〜0,-1% – mogli 2014-09-28 19:24:30

+0

@rits - remove'「tokens = *」' - 如果你沒有超過1個單詞的行,這將工作。 – npocmaka 2014-09-28 19:27:30