In the world of technology, there are some commands that you just know. Save! Exit? Logout is one such command that most people have been using for years now with little thought to its history and origins. What other mysterious yet ubiquitous commands can we find in our tech-saturated lives? Let’s explore today’s batch: “Goto.”
The “cmd goto” is a command-line tool that allows users to jump to any file in the current directory.
Batch files have been used to automate routine operations in Windows for decades. Despite the existence of competing scripting languages such as PowerShell, batch files are still commonly utilized. If you’ve ever wondered what the goto batch command in a batch file performs, you’re in luck.
With various examples, you’ll discover how the goto batch command works in batch files, as well as where you may still utilize this older command today!
Because the goto executable is part of the cmd.exe batch command suite, it will run on any supported Windows version.
Getting to Know a Simple goto Command
The goto command, in a nutshell, is a technique to regulate the flow of a batch file. When you run a batch file, the script usually runs from top to bottom, one line at a time. However, there are situations when you require the script to begin execution at a different point in the script. This is when the goto command comes in handy.
Assume you have a batch script with a few basic lines to transmit text to the screen, similar to the one below.
off echo @echo First, echo this line. Then, echo this line again. Third, run this line.
When you execute it, it displays the three messages as seen below.
Using a basic batch file
You may want to run the third line before the second line now. You could cut & paste, but this may not be feasible with a true batch file. Instead, you may use the goto command to redirect the batch file. You’ll utilize the GOTO command in conjunction with a label to do this.
Three ideas are shown below:
- The goto labels were produced by beginning with a colon and adding run first code beneath.
- Comments that begin with the letter:: indicate that the code is not executable.
- The goto:eof command instructs the batch script to complete or jump to the end of the file (eof). Here, the colon replaces the space. GOTO eof without the colon searches for the label of:eof anywhere in the file and does not leap to the end and return!
When you run the script below, you’ll see that the batch script executes the code in the same sequence as the GOTO instructions. The goto command alters the batch script’s flow. Because the batch script skips to the given goto command, the initial echo command is never executed. The script then runs line by line from that point forward.
off @echo: Start a discussion. The labels are referenced by the goto instructions. GOTO first run:: Internal goto command that skips straight to the conclusion of the script. echo GOTO:eof This line will not be printed: echo run first First, run this line: A colon echo before each label. repeat this line a second time Third, run this line.
Using a basic batch file
Labeling Best Practices
You used an underscore to specify labels in the preceding example. There’s nothing wrong with this technique, but there are plenty of alternative options. There are a few things to remember, since not every label will work.
What makes a good label:
What does not belong on a label:
- (=) equal signs
- Semicolons are used to separate sentences (;)
- Anything other than a space, colon, CR/LF (carriage return / line feed), or simply hitting Enter as the last character.
Adding different control characters to the batch script will cause it to continue indefinitely in a loop, requiring human termination.
If the goto command can’t locate a label you defined, you’ll get an error that says “The system cannot find the batch label supplied.”
If no label is discovered after searching from the GOTO statement to the end of the file, cmd.exe will continue the search from the beginning.
:: Spaces are OK in labels GOTO Label with spaces :Label with spaces Echo This is from a label with spaces :: Underscores, hyphens, and periods are all ok GOTO Label_hyphenated-with.a.period :Label_hyphenated-with.a.period Echo This label has an underscore, hyphen, and a period :: Control characters such as semi-colons (;) or (=) equal signs cannot be used :: GOTO Label=Equal :: GOTO Label;Semicolon :: If you specify a GOTO statement but do not have a corresponding label :: you will see an error GOTO Label_nonexisting
In fact, if you use either of the GOTO statements with the equal (=) or Semicolons are used to separate sentences (;),
Various label iterations are shown.
Using the if and goto commands together
Jumping to various locations in a batch file is useful, but it wasn’t feasible in the preceding case. What if you required a batch script that would only perform specific commands if a particular condition was met?
When used in combination with the if command, the goto command allows you to control when that leap occurs. Consider the following example. The batch file in this example uses the call command to launch a program. That software will always produce an exit code, which will be stored in the ERRORLEVEL variable.
The batch file then checks if the ERRORLEVEL variable is greater than zero. If this is the case, the script is skipped to the end. Otherwise, it uses the echo command to inform you that an error has occurred before continuing to the finish of the batch script.
@echo off:: Using a single space following a call command will change the percent ERRORLEVEL percent :: environmental variable to 1, but will not exit the script. If percent ERRORLEVEL percent NEQ 0 GOTO, call it. End the echo There was a problem, thus the Error Level was set to anything other than zero. GOTO: eof:: eof:: eof:: eof If there is a problem, this will never run: End the echo The Script Comes to an End
When you execute the preceding script, you’ll see that call returned a Exit code of non-zero and that an error was reported using the echo command.
Exit code of non-zero
Using the goto Command and Calling a Subroutine
How about utilizing the goto command with a subroutine now that you’ve understood the fundamentals of a goto statement? A subroutine is a code block or a function that may be called to run. A call command is similar to the goto command in that it employs labels.
Unlike the goto command, the call command will execute code following the label before returning to the original call command when it is cancelled.
This batch file uses the call command to execute the subroutine named my subroutine in the example below. The batch script then uses the goto:eof command to move to the end of the script inside that function.
@echo off:: Get user input using the SET command and add it to the percent Text percent variable set /p :: Text=”Enter Text”:: call:my subroutine percent Text percent :: After the subroutine, return here, echo some text, and then stop the script. Echo GOTO:eof:: is the end of the script. Run the following commands in the subroutine: subroutine name: Each variable is given a number in order, such as percent Text percent = percent 1. Echo percent 1:: user text Jumps to the end of the file, which ends the procedure automatically. GOTO:eof
The function is being executed via a batch file.
GOTO Statement Considerations, Bugs, and Special Cases!
There are specific instances, faults, and critical considerations to bear in mind when using the goto statement in your code, just as there are with any command.
The:eof Special Label and Command Extensions
You may have noted that the:eof label was used extensively in the preceding instances. Internal and external commands are available in cmd.exe. Although cmd.exe has the goto command, the unique label:eof is a command extension.
The following techniques may be used to deactivate command extensions:
- Set HKCUSoftwareMicrosoftCommand ProcessorEnableExtensionsCommand to 0 in the DWORD registry entry.
- To deactivate command extensions, use the cmd.exe /e:off option.
- Run the command SETLOCAL DisableExtensions from a batch script.
The goto command will function in most situations, however the specific label:eof will no longer operate. You can get around this restriction by adding a goto label to the end of the batch script.
DisableExtensions SETLOCAL Echo The following will appear: Because the:eof label does not present, the following command will break the script: End Echo GOTO:eof GOTO This will be hidden:End
The:eof label causes the left-hand script to fail, but the right-hand script completes successfully.
When you deactivate command extensions, additional cmd.exe features such as dynamic variables and percent TIME percent stop working.
In an if Statement, Command Groups and goto Commands are used.
A command group is a collection of commands enclosed by parentheses. This enables you to run a whole collection of commands as a single command. If the command group contains a goto statement and an if statement, the remaining conditionals, such as the else statement below, are disregarded, and the contained code is run.
If percent ERRORLEVEL percent EQU 1 (GOTO:subsection:subsection ECHO), a call statement without subsequent spaces changes the percent ERRORLEVEL percent to 1. Since 1 Equals 1 in the if expression, this will display. otherwise (:: Even though it shouldn’t, this will appear) This shouldn’t be visible, but it is because GOTO inside a command group violates the if statements.)
An if statement is broken by a command group containing a GOTO statement.
This might, as you would expect, have unforeseen repercussions! This goto statement will disrupt the logic if, for example, you put a delete command in the otherwise section that was not supposed to execute.
Steps to Follow
Despite the existence of modern automation languages such as PowerShell, batch files are still used in a variety of applications, including package distribution. Understanding how to utilize goto statements to build a script and manage the script flow is essential for creating effective automations!
The “goto batch loop” is a command that allows users to jump to another batch. This can be helpful when you’re working on multiple batches and want to quickly switch between them.
- use goto in batch file
- cmd goto loop
- if goto batch
- batch goto vs call
- batch file goto return