Go to the GdP Software main page
GdP Software
Contact
Shop
Products
Faq

back Directory Monitor, watchDirectory, automatically start command when folder contents change.

Using the GOTO statement

The GOTO statement is the most commonly used way to change the flow of control in a .bat file. In a previous topic, writing comments, we saw a very simple use to skip a block of comments.

The target label

GOTO.... Where are we going to? The GOTO statements needs a target label to jump to. A small example will help.

		 GOTO EndComment

		 All this is skipped
		 This won't be run
		 You can put anything here
		 
		 :EndComment

The target-label here is named ":EndComment". In your GOTO statement you do not have to add the colon, so you can use either GOTO :EndComment or GOTO EndComment

Special case, :EOF

On Windows NT based systems, Windows NT, 2000, XP and 2003, there is a special, predefined label you can use. This label is called :EOF. EOF is short for "End Of File", so executing the statement GOTO :EOF will quit your .bat file. You should not create this label yourself, if you want to avoid confusion.

GOTO %variable%

The label you jump to can also be a variable, as in the following example.

		 SET LAB=Target
		 SET RET=RetLabel
		 GOTO %LAB%
		 :RetLabel
		 Echo Back again
		 ...
		 :Target
		 Echo here we are
		 GOTO %RET%

In the above example, the flow of control is first passed to the "Target" label. After that, it passes control back to the "RetLabel" label. We created a simple sub-routine here. Note that this is not considered good style, it is better to use the call statement for this.

back