The Ultimate Guide To Batch File Programming PART- 7

Comparing Strings to Validate Parameters

The basic syntax is:

IF [NOT] string1==string2 Command

Now let's make our scripts intelligent and make them perform a task according to what parameter was passed by the User. Take the following snippet of code for example,


@ECHO off
Copy %2 a:
Del %2

This example too is pretty much self explanatory. The IF Statement compares the first parameter to cp, and if it matches then DOS is sent to read the COPY label else to the DEL label. This example makes use of two parameters and is called by passing at least two parameters.
We can edit the above example to make DOS check if a parameter was passed or not and if not then display an error message. Just add the following lines to the beginning of the above file.

IF "%1" == "" ECHO Error Message Here

If no parameter is passed then the batch file displays an error message. Similarly we can also check for the existence of the second parameter.
This command too has the NOT clause.

The CHOICE Command (Doesnt work on some OS's) :
Before we learn how to make use of the CHOICE command, we need to what error levels really are. Now Error levels are generated by programs to inform about the way they finished or were forced to finish their execution. For example, when we end a program by pressing CTRL+C to end a program, the error level code evaluates to 3 and if the program closes normally, then the error level evaluates to 0. These numbers all by themselves are not useful but when used with the IF ERROR LEVEL and the CHIOCE command, they become very kewl.

The CHOICE command takes a letter or key from the keyboard and returns the error level evaluated when the key is pressed. The general syntax of the CHOICE command is:


The string part is nothing but the string to be displayed when the CHOICE command is run.
The /C:keys defines the possible keys to be pressed. If options are mentioned then the default Y/N keys are used instead.
For example, The command,


Defines A, 1, T and O as the possible keys. During execution if the user presses a undefined key, he will hear a beep sound and the program will continue as coded.
The /S flag makes the possible keys defined by the CHOICE /c flag case sensitive. So it means that if the /S flag is present then A and a would be different.
The /N flag, if present shows the possible keys in brackets when the program is executed. If the /N flag is missing then, the possible keys are not shown in brackets. Only the value contained by STRING is shown.
/T:key,secs defines the key which is taken as the default after a certain amount of time has passed.

For Example,

CHOICE Choose Browser /C:NI /T:I.5

The above command displays Choose Browser[N,I] and if no key is pressed for the next 5 seconds, then it chooses I.
Now to truly combine the CHOICE command with the IF ERROR LEVEL command, you need to know what the CHOICE command returns.
The CHOICE command is designed to return an error level according to the pressed key and its position in the /C flag. To understand this better, consider the following example,


Now remember that the error level code value depends on the key pressed. This means that if the key A is pressed, then the error level is 1, if the key N is pressed then the error level is 2, if 1 is pressed then error level is 3 and if 2 is pressed then error level is 4.
Now let us see how the IF ERROR LEVEL command works. The general syntax of this command is:

IF [NOT] ERRORLEVEL number command.

This statement evaluates the current error level number. If the condition is true then the command is executed. For Example,


The above statement prints Yes on the screen if the current error level is 3.
The important thing to note in this statement is that the evaluation of an error level is true when the error level us equal or higher than the number compared.
For Example, in the following statement,


The condition is true if the error level is > or = 2.
Now that you know how to use the CHOICE and ERROR LEVEL IF command together, you can now easily create menu based programs. The following is an example of such a batch file which asks the User what browser to launch.

ECHO Welcome to Browser Selection Program
ECHO 1. Internet Explorer 5.5
ECHO 2. Mozilla 5
ECHO x. Exit Browser Selection Program
CHOICE "Choose Browser" /C:12x /N
IF ERRORLEVEL 2 START C:\progra~1\Netscape
IF ERRORLEVEL 1 start c:\progra~1\intern~1\iexplore.exe

NOTE: Observe the order in which we give the IF statements.

AbhiShek SinGh
Founder of 'TheHackingArticles'. Cyber Security Analyst, Cyber Security Researcher, and Software Engineer. Follow 'AbhiShek SinGh' on Facebook , Twitter or Google+ or via Email

Subscribe to stay up to date