|
Subcommands in this section are generally used to create batch programs. As such, they are a bit more complex than the simply ones studied on the last page. IFThe IF subcommand allows you to make decisions within your batch file. Its syntax is:
Unlike programming languages, which allow many logical tests in an IF statement, the batch IF statement is limited to only the three above. Condition 1: ERRORLEVEL is a number that indicates to DOS whether the last program run was successful.
You can create small programs to capture the keyboard output and report it as an error level (see Batch Sample #3 later). Condition 2: String comparison is indicated by double equal signs:
compares the two strings. This comparison is often used with parameters and markers to check for a particular entry. For example,
checks parameter one for 40 and, if found, changes the display to 40-columns wide. Condition 3: The logical test checking for a file has the format:
You can use this test to check and see if a DOS disk is in the active drive (as one example). Another use might be to check for a particular file to see if the user has a valid disk in the drive. Pathnames are NOT allowed. Password ExampleThe following batch file can be used to establish a password for running a program. The batch file is named START.BAT and calls the program named WP.COM.
Below you'll see the response of the computer to various commands... First the bad password. At the prompt type START ABC.
Now use the correct password. Type the correct command at the prompt.
At this point the WP program starts. You don't see the command because echo is off. SHIFTThe limit of 10 parameters for a batch file can be raised through use of the SHIFT command. The syntax is the single word:
When that subcommand is encountered, all parameter/marker pairings are shifted one to the left. Whatever was assigned to %0 is lost, the contents of %1 are moved to %0, %2 moves to %1 ... %9 moves to %8 and a new parameter from the command line is moved into %9. While this brings in a new parameter, all have shifted and you must anticipate the effect on your batch file "program." The effect of SHIFT:
Remember, this command seems very simple, but its effects are far ranging and the logical consequence of mismatching parameters and markers and be disastrous! FOR..IN..DOThis command is similar to the programmer's FOR..NEXT loop in that a particular action can be repeated a given number of times. The syntax (which may look a bit complicated) is:
In effect, what this subcommand does is cause %%Variable to be an index into the (Set) for use by Command. An example: Problem: Compare all files on the disk in drive A: with those on the disk in drive B: and report those that match. Answer: The following two-line batch file run from the A: drive will do that task:
Let's see how... The first line turns command display off to clear the clutter. The second line is executed as many times as there are files on the disk in A: [the set (*.*) assures this]. Each of those filenames are assigned to %%Z in turn and then checked for presence on drive B: with the EXIST logical statement. If EXIST is true, then the message at the end of the IF subcommand is sent to the screen, otherwise nothing is printed and the next file on drive A: is assigned and checked. The following will diagram this process... Files on drive A:
Files on drive B:
The batch subcommand we are investigating is:
Each filename on A: is substituted in the IF subcommand and then executed. To get the same effect you would have to type:
In the case of the example above, the first two would have a positive response and the last would not print anything. Study it carefully before going on! Another example: Files on drive A:
Files on drive B:
OK, told you to study the example. Let's see if you remember. What is the one line batch file command to find and report out all files starting with an "F" on drive A:?
In this case you see that the A: disk is checked for any file starting with the letter "F" and that name is substituted in the variable %%Z. The appropriate message is then printed. This is not an easy concept. It will require some study and practice to master. Let's now look at using DOS commands in (Set). The (Set) can contain DOS commands instead of filenames and these commands will then be executed in sequence (to include running large programs under the control of the FOR..IN..DO loop). Let's say you want to sequentially:
You could do all that in a one line batch file with the following command in it:
When using DOS commands in (Set) you must use the space as a delimiter and cannot have the asterisk (*) or question mark (?) in any command. Use a colon (:) instead of a space when passing parameters to programs (i.e., DBASE:FILE instead of DBASE FILE). [The colon trick does not always work; you have to experiment.] It is possible to issue the FOR..IN..DO command at the DOS prompt by dropping one of the percentage signs (%) on the variable. CALLSometimes it's handy to be able to run one batch file from another. Until DOS 3.3 you had to resort to tricks to do that; without the tricks when you called a second batch file from a first then control would transfer to the second and you'd never get back to the first. The command: CALL d:path\FILENAME parameters can be used in DOS versions 3.3 and later to start a second batch file from a parent and then return to the parent after the second finishes. Note: Do not use pipes or redirection on the second file. The FILENAME is the name of the second batch file; the parameters are any options that the second file requires to properly run. When the second batch file terminates, control is returned to the first batch file on the line following the CALL command. You can simulate CALL in DOS versions lower than 3.3 by using:
@In DOS 3.3 and later you can selectively cause lines in a batch file from being displayed. To cause this, place an "@" sign in front of the line in question. One use of this would be to suppress showing the "ECHO OFF" command which is the starting command of many batch files. Until the ECHO OFF command is actually executed, ECHO is ON and the command shows. To stop even this from showing make the first command: @ECHO OFF. If you need to use the "@" feature with a command that already starts with @ (e.g., @WIP.EXE) then use a double @ (e.g., @@WIP) in the batch file (this should be rare). |
Please use the contact
form for questions or comments about this web site.
Copyright © 2001
Computer Knowledge, All Rights Reserved
Pray the Rosary for peace.