Batch file 批处理脚本:IF中的集合存在问题
问题是我的Batch file 批处理脚本:IF中的集合存在问题,batch-file,if-statement,set,Batch File,If Statement,Set,问题是我的set tap=c:\ca\sf\1st-2nd…等根本不起作用。 echo不显示任何内容,set由于某种原因没有将路径放入变量中。 我完全明白了如果s正确,还有其他问题吗 setlocal enabledelayedexpansion if NEWYORK == %region% ( set tap=C:\ny CALL :process %1 %2 %tap% %cl% GOTO :EOF )
set tap=c:\ca\sf\1st-2nd…
等根本不起作用。
echo
不显示任何内容,set
由于某种原因没有将路径放入变量中。
我完全明白了如果s正确,还有其他问题吗
setlocal enabledelayedexpansion
if NEWYORK == %region% (
set tap=C:\ny
CALL :process %1 %2 %tap% %cl%
GOTO :EOF
)
if California == %region% (
if '%3'=='sanfrancisco' (
set cl=c:\ca\sf\cl
if '%2'=='1st' set tap=c:\ca\sf\1st
if '%2'=='2nd' set tap=c:\ca\sf\2nd
if '%2'=='3rd' set tap=c:\ca\sf\3rd
if '%2'=='4th' set tap=c:\ca\sf\4th
if '%2'=='5th' set tap=c:\ca\sf\5th
echo %tap%, echo %cl%,
pause
CALL :process %1 %2 %tap% %cl%
GOTO :EOF
)
if '%3' == 'LosAngeles' (
set tap=c:\ca\la
set cl=c:\ca\la\cl
echo %tap%, %cl%
pause
CALL :process %1 %2 %tap% %cl%
GOTO :EOF
)
set tap=c:\USA
set cl=c:\usa\cl
echo %tap%, %cl%
pause
CALL :process %1 %2 %tap% %cl%
GOTO :EOF ) else (
echo faiiiiiiiiiillllllllll
pause
GOTO :END)
endlocal
GOTO :EOF
你错过了第一个SET命令。线路
tap=C:\ny
一定是
set tap=C:\ny
使用在IF中修改的变量时,其值必须展开为!瓦尔!与%var%无关;否则,展开的值就是变量在输入IF或FOR之前的值(这是EnableDelayedExpansion的目标)。例如:
set var=Old value
if 1 == 1 (
set var=New value
echo With percent: %var%. With exclamation: !var!
)
上一段显示:和百分比:旧值。带感叹号:新值
另一项评论:
虽然执行时,如果纽约==%region%
与如果%region%==NEWYORK
相同,但从程序员的角度来看,第二个是习惯性的,更清晰
编辑
我稍微修改了你的代码。看看它:
setlocal enabledelayedexpansion
if /I %region% == NEWYORK (
set tap=C:\ny
REM cl IS NOT DEFINED HERE, BUT USED IN NEXT LINE
CALL :process %1 %2 !tap! !cl!
GOTO :EOF
)
if /I %region% == California (
if /I '%3' == 'sanfrancisco' (
set cl=c:\ca\sf\cl
set tap=c:\ca\sf\%2
echo !tap!, !cl!
pause
CALL :process %1 %2 !tap! !cl!
GOTO :EOF
)
if /I '%3' == 'LosAngeles' (
set tap=c:\ca\la
set cl=c:\ca\la\cl
echo !tap!, !cl!
pause
CALL :process %1 %2 !tap! !cl!
GOTO :EOF
)
set tap=c:\USA
set cl=c:\usa\cl
echo !tap!, !cl!
pause
CALL :process %1 %2 !tap! !cl!
GOTO :EOF
) else (
echo faiiiiiiiiiillllllllll
pause
GOTO :END
)
endlocal
GOTO :EOF
如果在不启用DelayedExpansion的情况下运行脚本会发生什么?你的“:进程”是什么?如果集合和回音不工作,很可能整个分支都没有执行。除了“set=tap=c:\ca\sf\1st-2rd-3rd等。。。如果没有enabledelayed它根本不工作,my:process是另一个代码,如果我不把这部分代码放进去,它工作得很好。.正如Aacini所写的,您需要使用延迟扩展,而不是
CALL:process%1%2%tap%%cl%
您需要CALL:process%1%2!轻敲!%cl%
你们已经试过了,问题不在于这个!或在“如果”中用“设置”表示%It