批处理脚本:IF 中的 SET 问题
问题是我的 set tap=c:\ca\sf\1st 2nd...
等根本不起作用。 echo
没有显示任何内容,set
由于某种原因没有将路径放入变量中。 我的所有if
都正确,还有其他问题吗?
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
The problem is that my set tap=c:\ca\sf\1st 2nd...
etc. isn't working at all.
The echo
shows nothing, the set
isn't putting the path in the variable for some reason.
I got all if
s right, is there another problem?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错过了第一个 SET 命令。该行
必须是
当您使用在 IF 或 FOR 内部修改的变量时,其值必须用 !var! 扩展而不是 %var%;否则扩展值是变量在输入 IF 或 FOR 之前的值(这是 EnableDelayedExpansion 的目的)。例如:
上一段显示:
带百分比:旧值。带感叹号:新值
补充一点注释:
虽然
if NEWYORK == %region%
执行时与if %region% == NEWYORK
是一样的,第二个是习惯性的,从程序员的角度来看更清晰。编辑
我稍微修改了你的代码。看看它:
You missed the first SET command. The line
must be
When you use a variable that is modified inside an IF or FOR its value must be expanded with !var! and not with %var%; otherwise the expanded value is the value the variable had BEFORE enter the IF or FOR (this is the objective of EnableDelayedExpansion). For example:
Previous segment show:
With percent: Old value. With exclamation: New value
An additional comment:
Although
if NEWYORK == %region%
is the same asif %region% == NEWYORK
when it is executed, the second one is customary and clearer from programmers point of view.EDIT
I slightly modified your code. Take a look at it: