在 WINDOWS / DOS 批处理编程中,使用标签定义一个程序块,标签类似其它编程语言(例如:C语言)中的函数。
标签作为程序块的示意图如下:
蓝色方框是 :firstLabel 标签,红色方框是 :secondLabel 标签
那么如何使用已经定义的标签呢?
【标签不被调用时的流程】
在 WINDOWS / DOS 脚本编程中,一般使用goto命令或call命令调用标签。
如果标签没有被调用,则当脚本程序执行到标签时,标签会作为一般程序代码被执行(就像标签不存在一样)。
示例一
::demo.bat
@echo off
:firstLabel
echo firstLabel
:secondLabel
echo secondLabel
echo this the end of file.
运行
C:\>demo.bat
firstLabel
secondLabel
this the end of file.
C:\>