If Noti Batchfiles



  • Batch Script Tutorial

Useful Batch Files

I'm not so sure. I haven't really used an IF statement in a batch file in a while or even batch files at all lately really. I have a batch reference book at the house I could look it up in. According to a question on StackOverflow the actual syntax / formatting of the if statement itself is critical. @echo off if exist C: set2.txt echo 'File exists' if exist C: set3.txt (echo 'File exists') else (echo 'File does not exist') Output. Let’s assume that there is a file called set2.txt in the C drive and that there is no file called set3.txt. Then, following will be the output of the above code. 'File exists' 'File does not exist'.

  • Batch Script Resources
  • Selected Reading

The first decision-making statement is the ‘if’ statement. The general form of this statement in Batch Script is as follows −

The general working of this statement is that first a condition is evaluated in the ‘if’ statement. If the condition is true, it then executes the statements. The following diagram shows the flow of the if statement.

Checking Variables

One of the common uses for the ‘if’ statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the ‘if’ statement can be done for both strings and numbers.

Checking Integer Variables

The following example shows how the ‘if’ statement can be used for numbers.

Example

The key thing to note about the above program is −

  • The first ‘if’ statement checks if the value of the variable c is 15. If so, then it echo’s a string to the command prompt.

  • Since the condition in the statement - if %c% 10 echo 'The value of variable c is 10 evaluates to false, the echo part of the statement will not be executed.

Output

The above command produces the following output.

Checking String Variables

The following example shows how the ‘if’ statement can be used for strings.

Example

The key thing to note about the above program is −

  • The first ‘if’ statement checks if the value of the variable str1 contains the string “String1”. If so, then it echo’s a string to the command prompt.

  • Since the condition of the second ‘if’ statement evaluates to false, the echo part of the statement will not be executed.

Output

The above command produces the following output.

Note − One key thing to note is that the evaluation in the ‘if’ statement is 'case-sensitive”. The same program as above is modified a little as shown in the following example. In the first statement, we have changed the comparison criteria. Because of the different casing, the output of the following program would yield nothing.

Checking Command Line Arguments

Another common use of the ‘if’ statement is used to check for the values of the command line arguments which are passed to the batch files. The following example shows how the ‘if’ statement can be used to check for the values of the command line arguments.

Example

The key thing to note about the above program is −

  • The above program assumes that 3 command line arguments will be passed when the batch script is executed.

  • A comparison is done for each command line argument against a value. If the criteria passes then a string is sent as the output.

Output

If the above code is saved in a file called test.bat and the program is executed as

Following will be the output of the above program.

Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  1. Conditional Processing with If
< BackPage 7 of 13Next >
This chapter is from the book
Windows XP Under the Hood: Hardcore Windows Scripting and Command Line Power

This chapter is from the book

This chapter is from the book

Windows XP Under the Hood: Hardcore Windows Scripting and Command Line Power

Conditional Processing with If

One of the most important capabilities of any programming language is the ability to choose from among different instructions based on conditions the program finds as it runs. For this purpose, the batch file language has the if command.

The Basic If Command

In its most basic form, if compares two strings and executes a command if the strings are equivalent:

This is used in combination with command-line variable or environment variable substitution, as in this example:

If and only if the batch file's first argument is the word ERASE, this command will delete the file somefile.dat.

The quotation marks in this command aren't absolutely required. If they are omitted and the command is written as

Batch File If Not Defined

the command will still work as long as some command-line argument is given when the batch file is run. However, if the batch file is started with no arguments, then %1 would be replaced with nothing, and the command would turn into this:

This is an invalid command. CMD expects to see something before the part of the command and will bark if it doesn't. Therefore, it's a common practice to surround the items to be tested with some character—any character. Even $ will work, as shown here:

If the items being tested are identical, they will still be identical when surrounded by the extra characters. If they are different or blank, you'll still have a valid command.

The if command also lets you reverse the sense of the test with the not option:

Checking for Files and Folders

The exist option lets you determine whether a particular file exists in the current directory:

Of course, you can specify a full path for the filename if that's appropriate, and you can use environment variables and % arguments to construct the name. If the filename has spaces in it, you'll need to surround it with quotes.

The not modifier can be used with exist as well.

Checking the Success of a Program

When a command line or even a Windows program exits, it leaves behind a number called its exit status or error status value. This is a number that the program uses to indicate whether it thinks it did its job successfully. An exit status of zero means no problems; larger numbers indicate trouble. There is no predetermined meaning for any specific values. The documentation for some programs may list specific error values and give their interpretations, which means that your batch files can use these values to take appropriate action. How? Through the errorlevel variation of the if command.

After running a command in a batch file, an if statement of the form

will execute the command if the previous program's exit status value is the listed number or higher. For example, the net use command returns 0 if it is able to map a drive letter to a shared folder, and it will return a nonzero number if it can't. A batch file can take advantage of this as follows:

You can also use not with this version of the if command. In this case, the command is executed if the error status is less than the listed number. The error testing in the previous example can be rewritten this way:

In this version, the flow of the batch file is a bit easier to follow. However, even this can be improved upon, as you'll see next.

Performing Several Commands After If

Often, you'll want to execute several commands if some condition is true. In the old days, before the extended CMD shell came along, you would have to use a goto command to transfer control to another part of the batch file, as in the if exist example given in the previous section. With the extended version of if, this is no longer necessary.

The extended if command lets you put more than one statement after an if command, by grouping them with parentheses. For example, you can place multiple commands on one line, as shown here:

Or you can put them on multiple lines:

I recommend the second version, because it's easier to read. Look how much clearer the network file copying example becomes when parentheses are used instead of goto:

You can also execute one set of commands if the if test is true and another if the test is false by using the else option, as in this example:

You can use else with parentheses, but you must take care to place the else command on the same line as if, or on the same line as the closing parenthesis after if. You should write a multiple-line if...else command using the same format as this example:

Extended Testing

Noti

The extended if command lets you perform a larger variety of tests when comparing strings, and it can also compare arguments and variables as numbers. The extended comparisons are listed in Table 12.3.

Table 12.3. Comparison Operators Allowed by the if Command

Variation

Comparison

ifstring1EQUstring2

Exactly equal

ifstring1NEQstring2

Not equal

ifstring1LSSstring2

Less than

ifstring1LEQstring2

Less than or equal to

ifstring1GTRstring2

Greater than

ifstring1GEQstring2

Greater than or equal to

if /i(comparison)

Case-insensitive

if definedname

True if there is an environment variable name.

if cmdextversionnumber

True if the CMD extensions are version number or higher.

As an added bonus, if the strings being compared contain only digits, then CMD compares them numerically. For example, you could test for a specific exit status from a program with a statement like this:

Related Resources

  • Book $47.99
  • eBook (Watermarked) $38.39
  • Book $23.99