| |
If you are familiar with other programming languages then you surely know about functions and subroutines. These are really great ways of minimizing code. The GOTO command in batch is very similar to subroutines and functions however the GOTO command will not minimize code. Instead it will produce larger amounts of code in your batch script. This command is like subroutines and functions in that we can have smaller programs sectioned off that can be accessed by the larger program.
The reason why it doesn't really make coding shorter is that unless you have a very large batch file, you, typically, won't need to use the same sub-program more than once. The GOTO command is great for giving the user possible choices to select from. The syntax is GOTO followed by the name of the line. At the line you need to have a colon, however when using the GOTO command you do not use the colon to specify the line. The named line can be above or below the GOTO statement. Study the following example to get a good grasp of this concept. Using the GOTO command you have the possibility to create much more sophisticated programs.
| :: LOOP.BAT :: A simple batch file example using a batch loop :TOP DIR GOTO TOP  |
|
|
|
|
|
|
|
|
|
The first two lines are remark statements giving the details of the program including the filename and a brief description of the program. The third line marks the line that a GOTO command can possibly link to. The fourth line will perform a directory listing. This is just an example command to illustrate a task that can be performed using the batch loop. You might like to indent the DIR command to emphasize that it is inside a loop. The last line is the command to jump to the line with the name TOP and to continue processing. Since there is no IF test to jump out of this loop this batch file will continue until you press the Ctrl+C or the Ctrl+Break combinations.
| :: LOOP2.BAT :: Loop which reads multiple replaceable parameters :: with a SHIFT command inside the loop @ECHO OFF :TOP COPY %1 A: SHIFT GOTO TOP  |
|
|
|
|
|
|
|
|
|
This is a more useful program that copies a list of files to the A: drive. When the LOOP2.BAT file runs it will copy a list of files to the A: drive. When the LOOP2.BAT program runs out of files to copy, the program will not stop. The batch script will attempt to continue until you use Ctrl+C or Ctrl+Break to terminate the program.
The first three lines are remark statements indicating the name of the batch file and a brief description of the batch file. The fourth line turns off command echoing. Remember that the @ sign turns off the echoing of the ECHO OFF command. The fifth line is the marked line that the GOTO command will jump to. The sixth line copies the file defined by the first replaceable parameter to the A (Floppy) drive. The seventh line utilizes the SHIFT command to replace %0 with %1, %1 with %2, and so forth. The last line, GOTO TOP, is used to jump to the line labeled TOP and continue processing. Obviously the downside to this looping structure is that it needs more control. One way to add this control is through the IF statement.
| |
|
| |
|