This tip continues the series about using batch files. It is suggested that you (re)visit the previous tips so that we can pick up where we left off. Thus far we've covered the following commands: DIR, REM, ECHO, FIND, TIME, and TIMEOUT. We've also used the following operators: output to file (>), remark (::), no echo (@), pipe (|), and concatenate (&). Finally, we know that we use parameters passed to a batch file via the combinations %1, %2, %3, etc.
This tip will focus on commands that are related to performing conditional processing in a batch file. These include IF, CHOICE, GOTO, and the special syntax of ":label".
I have a very handy batch file called Do.bat. It is little more than a "wrapper" that runs programs regardless of where these programs reside. So, for example, if I want to run the program called Prog1.exe I would simply type
C:\Bat> DO Prog1
If I wanted to run Prog2.exe I would type
C:\Bat> DO Prog2
The beauty of Do.bat is that when I created it, I entered the locations of all the programs that I want it to work with. So I don't have to remember, for example, that Prog1.exe lives at "C:\Programs\My Programs\Prog1.exe" and that Prog2.exe lives at "D:\Utilities\Barry\Prog2.exe". Plus, I never have to type those long path names again.
My Do.bat file looks like this:
@ECHO OFF GOTO %1 :Prog1 "C:\Programs\My Programs\Prog1.exe" GOTO Done :Prog2 "D:\Utilities\Barry\Prog2.exe" GOTO Done :Done
The first line of the file ensures that the other lines won't be displayed. This is followed by the GOTO line, which takes as a parameter the name of the label that control is to be transferred to. And it so happens that this label is also the parameter that was passed to Do.bat to begin with. So when you invoke Do.bat and pass "Prog1" as the parameter, the .bat file does a GOTO to a label named ":Prog1" (these are not case sensitive). Execution follows sequentially from top to bottom, so the next command to be executed is the one that invokes Prog1.exe. After this program is finished, the .bat file continues by executing the "GOTO Done" command. The ":Done" label is the last line of the file, so when it reaches this point, the .bat file exits. (There is an EXIT command, but if we were to execute that, it would terminate not only the .bat file but also the entire CMD session, which is probably not what you want to do.)
You could alternatively use the CHOICE command and perhaps create a simple menu from which to choose what program to run. In this case I might make a file called Choice.bat which would look like this:
@ECHO OFF ECHO Select 1 to run Prog1 ECHO Select 2 to run Prog2 ECHO Select Q to quit CHOICE /C 12Q /T 10 /D Q GOTO %ERRORLEVEL% :3 ECHO Quitting GOTO Done :2 "D:\Utilities\Barry\Prog2.exe" GOTO Done :1 "C:\Programs\My Programs\Prog1.exe" GOTO Done :Done
The CHOICE command provides the user with a prompt to enter a 1, 2, or Q (by default, it is not case sensitive). Any other character is ignored. The /T switch says to timeout in 10 seconds, at which time the procedure will behave as if the Q option was selected (/D Q says the Default option is Q). After the CHOICE command executes, the variable %ERRORLEVEL% is set to ordinal value of the option selected. So if the user selected 1, %ERRORLEVEL% is set to 1; if the user selected 2, %ERRORLEVEL% is set to 2; and if the user selected Q, %ERRORLEVEL% is set to 3. We then GOTO the label indicated by %ERRORLEVEL% and perform the requested action.
I could write an entire tip on the IF command, but the basics are that you do something like:
IF arg1 operator arg2 command
where 'arg1' and 'arg2' are arguments being tested, 'operator' is one of EQU, NEQ, LSS, LEQ, GTR, GEQ (i.e., EQUal, Not Equal, LeSS than, etc.), and 'command' is a command-line command.
A simple batch file to echo P1 if there is one might look like this:
@ECHO OFF IF [%1] EQU [] ECHO No Parameter passed IF [%1] NEQ [] ECHO Parameter P1 is '%1' GOTO:eof
(I got in the habit of using [] around my arguments to avoid certain complications.)
There are other flavors of IF too. For example, you can test an error level, test if a file exists, include an ELSE statement, etc.
At the command line, do an IF/? and you'll get a good idea of what all you can do.
This tip (13185) applies to Windows 7, 8, and 10.
This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special ...
Discover MoreSometimes your batch file needs to accept an unknown number of parameters. This is easy to deal with if you know about ...
Discover MoreIn another tip we were introduced to the various FOR loops that exist in Windows. The actual use of these loops was left ...
Discover MoreThere are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Copyright © 2024 Sharon Parq Associates, Inc.
Comments