下面的for循环结果在我的机器上不是预期的。我不知道自己做错了什么。
请在下面找到check.bat和softwareList.txt的代码列表。softwareList.txt也在check.bat所在的同一目录中。
检查.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
下面是软件列表.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%
@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
在延迟扩展下,当定义变量并在括号内访问时,应使用!
而不是%