且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

蝙蝠文件和标签

更新时间:2023-02-02 21:40:31

因为GOTO只是执行到脚本中的一个点的跳转,所以从那个点开始执行。如果你想让它在运行EXISTING之后停止,那么你需要这样做。注意额外的GOTO和新标签:

Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:

@ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING

:EXISTING
echo file exists
goto :NEXTBIT

:MISSING
echo file missing

:NEXTBIT
ping localhost -n 5 >NUL



It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:

@ECHO OFF
IF EXIST c:\test\test.txt (
    ECHO File exists
) ELSE (
    ECHO File missing
)
ping localhost -n 5 >nul


$ b b

...所以你可以完全消除你的GOTO。

...so you can eliminate your GOTOs entirely.