批处理脚本:IF 中的 SET 问题

发布于 2024-12-12 11:48:32 字数 1335 浏览 1 评论 0原文

问题是我的 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 ifs 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-12-19 11:48:32

您错过了第一个 SET 命令。该行

tap=C:\ny

必须是

set tap=C:\ny

当您使用在 IF 或 FOR 内部修改的变量时,其值必须用 !var! 扩展而不是 %var%;否则扩展值是变量在输入 IF 或 FOR 之前的值(这是 EnableDelayedExpansion 的目的)。例如:

set var=Old value
if 1 == 1 (
    set var=New value
    echo With percent: %var%. With exclamation: !var!
)

上一段显示:带百分比:旧值。带感叹号:新值

补充一点注释:

虽然 if NEWYORK == %region% 执行时与 if %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  

You missed the first SET command. The line

tap=C:\ny

must be

set tap=C:\ny

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:

set var=Old value
if 1 == 1 (
    set var=New value
    echo With percent: %var%. With exclamation: !var!
)

Previous segment show: With percent: Old value. With exclamation: New value

An additional comment:

Although if NEWYORK == %region% is the same as if %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:

    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  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文