命令格式
GOTO label
其中label指定用作标签的文字字符串
命令功能
GOTO命令用于批处理文件中,搜索 label 标签,找到该标签后,程序将从标签的下一行继续执行,直至批处理文件的末尾,并退出程序。示例如下:
demo.bat的内容如下:
::demo.bat
@echo off
echo before...
goto target1
echo after...
:target1
echo target1
:target2
echo target2
运行
C:\>demo
before...
target1
target2
C:\>
如果“GOTO label”命令中的“lable”未定义或者找不到,则报错并退出程序,示例如下:
demo.bat的内容如下:
::demo.bat
echo before
goto target
echo after
运行
C:\>demo
C:\>echo before
before
C:\>goto target
系统找不到指定的批处理标签 - target
C:\>
【说明】
如果启用了命令扩展(即默认状态),并且使用了标签为 ":EOF"的goto命令, 则可以在不定义标签的情况下,将控制传送到当前批处理脚本文件的末端,然后退出批处理脚本文件。 ":EOF"标签中的冒号不可省略。什么是命令扩展https://blog.csdn.net/mt15306338066/article/details/127198526
demo.bat的内容如下:
::demo.bat
echo before
goto :EOF
ECHO after
运行
C:\>cmd /e:on
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\>demo
C:\>echo before
before
C:\>goto :EOF
C:\>cmd /e:off
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\>demo
C:\>echo before
before
C:\>goto :EOF
系统找不到指定的批处理标签 - EOF
C:\>
在DOS编程中,由于for命令功能有很大局限性,使用goto命令来间接实现循环就显得至关重要,有时可能是唯一可行的方法。例如,下面的“获取字符串的长度”示例
demo.bat的内容如下:
::demo.bat
@echo off
set /p str=please input a string:
echo the string:"%str%"
call :stringlenth "%str%" num
echo length is: %num%
goto :EOF
:StringLenth
::@echo off
set theString=%~1
if not defined theString goto :eof
set Return=0
:StringLenth_continue
set /a Return+=1
set thestring=%thestring:~0,-1%
if defined thestring goto StringLenth_continue
if not "%2"=="" set %2=%Return%
goto :eof
运行
C:\>demo.bat
please input a string:abc
the string:"abc"
length is: 3
C:\>
写作不易,如果您觉得对您有用,请 “在下面一圈三连”。