IF and IF NOT Commands
There are three variations of the IF and IF NOT commands.
-
IF EXIST: Execute the commandline only if a particular file exists:
IF EXIST some.txt COPY c:/some.dll %windir%/SYSTEM/some.dll
-
Compare two text strings, and execute the commandline only if they are identical.
IF %HostWinBootDrv%==C SET WinDir=C:\WINDOWS
-
Error testing: Check the exit code of the most recently run program. If it is equal to or greater than the number specified, execute the command:
IF ERRORLEVEL 4 ERASE trashfile.tmp /P
GOTO Command
You can set a label in a batch file by beginning a line with a colon. You can then go directly to that label with the GOTO command. The GOTO command searches both forward and backward in the batch file; that is, it simply goes to the label location, regardless of where it is in the file.
For example, in my batch file for removing the Happy99 virus, UNHAPPY.BAT, the following code was used to make sure a file was not deleted unless the original form of it (backed up by the virus under the name WSOCK32.SKA) is present:
IF NOT EXIST WSOCK32.SKA GOTO SavedIt
DEL WSOCK32.DLL
RENAME WSOCK32.SKA WSOCK32.DLL
:SavedIt
FOR Command
The syntax for this command is: FOR variable in (set list) DO command
The variable must be in the form of one alphabetic character preceeded by %%; e.g., %%v.
NOTE: The %% is necessary because this is in a batch file which, otherwise, would give a special meaning to a single %. However, if you run the FOR command outside of a batch file, simply from the system prompt, just use a single % in the variable name. (Tip from Steve Wisdom)
The set list is enclosed within parentheses. These values will be assigned to the variable successively. You can use any text enclosed in quotes, batch file commandline parameters, environment variables, or DOS file wildcard expressions.
The command can be any valid command that otherwise could be entered into the batch file as a line of its own. example:
FOR %%D in (SYSTEM, COMMAND, SHELLNEW, "Start Menu") DO DIR "%windir%\%%D" /W
Menu Creation
Sometimes you may want to let a batch file branch one way or another based on user input. This is especially helpful when you have several related batch processes and would like to combine them into a single application.
Back in DOS days, a common approach was to construct menus with multiple batch files. For example, you could create one batch file called MENU.BAT that ed the menu (a series of text lines), inviting a user to type a 1, 2, 3, etc. (or A, B, C, etc.) to choose an option (a program to run, or archiving process, or whatever). That menu batch file would end, delivering the user back to a command prompt. You would then have a series of other batch files called 1.BAT, 2.BAT, 3.BAT, etc. so that, when the user typed (for example) 2 and pressed Enter, it would run 2.BAT. (This is way easier to understand if you walk through making one! It’s really terribly simple.)
Today, when users don’t live in a DOS command prompt world, we want something slightly more sophisticated — and, fortunately, we have it. There is a pretty cool way to allow user input in Windows 2000 and XP, and even better ways that work in Windows Vista.
In Windows 2000 or XP, the SET command has new /A and /P flags that allow user input. The latter is especially helpful for our present purposes. You can accept user input and assign it to a system variable with the following code:
SET /P variable=PromptString
The PromptString is optional. If you include one, it will be ed on the screen. (Don?t forget a space at the end of the prompt if you want one!) For example,
SET /P M=Type 1 or 2, then press ENTER.
will on the monitor the phrase “Type 1 or 2, then press ENTER.” It will then wait for the user to type something and press Enter. It will then assign whatever character the user types to the system variable %M%, which you can use in other batch file commands.