If and goto command

You can use the IF command to carry out different commands under different conditions.

It lets a batch file take one of two paths. The basic syntax is

If < this statement is> true <do this action>

<do this action if the statement is> false

If the statement being tested is false, then the batch file will skip to the next line and continue processing.

The if command can be used to test for 3 conditions. It can test for the system ERRORLEVEL, it can compare two strings to see if they are equal, and it can test to see if a file exists. It also has one switch (not) which reverses the meaning of the If command so that <do this action> is carried out only if <this statement is> false

Example 1. Test to see if two strings are equal

runit.bat

@echo off

if "%1" =="C" echo you entered a C

echo you did not enter a C

If you type in runit C at the prompt you will see on the screen:

you entered a C

you did not enter a C

If you type anything but runit with the C parameter you will see

you did not enter a C

 

You can also test to see if two replacable parameters are equal.

If "%1"=="%2" echo %1 and %2 are identical

You can test to see if a replacable parameter is empty.

If "%1"=="". echo Need to specify parameter

 

 

 

Example 2. (if filename exists)

Backup.bat

@echo off

echo Backup Procedure

echo Make sure a floppy is in drive a

pause

if exist a:%1 echo a:% exists, Press Ctrl-C to cancel, or

pause

copy %1 a:%1

If the FileName you entered exists on the A-Drive then display the file name and the words "Press Ctrl-C to cancel or Press any key to continue (generated by the pause command)",. If the user presses Ctrl-C the batch file will cancel. if the user presses any other key then the batch file is temporarily halted and the words "Press any key to continue (generated by the pause command)", when the user presses a key then the FileName that you entered as a parameter will be copied to the A-Drive

If the FileName you entered does not exists on the A-Drive then the batch file is temporarily halted and the words "Press any key to continue (generated by the pause command)", when the user presses a key then the FileName that you entered as a parameter will be copied to the A-Drive

 

Example 3. (ERRORLEVEL)

Many DOS commands provide exit status values to indicate whether they were successfully executed, and if they weren’t the cause of the error. For example the DISKCOPY command returns one of the following exit-status value:

Value

Meaning

0

Successful disk copy

1

Copy unsuccessful due to nonfatal disk error

2

Copy incomplete because the user pressed Ctrl-C

3

Copy unsuccessful due to fatal disk error

4

Insufficient memory or invalid drive

Format

If ERRORLEVEL Value DOS command

When DOS encounters an If ERRORLEVEL command, it examines the exit-status value of the preceding command. If the exit-status value is greater than or equal to the specified value. DOS executes the specifed command. If the exit-status is less than the specified value, DOS continues processing with the next command in the batch file.

 

Goto command

The goto command gives you the ability to direct which set of commands are executed, and which set of commands are skiped within your batch file. It does this by a process called branching (branching from one section to another). The goto command works with labels. Labels are names for different sections of your batch file.

The format for labels are

:LabelName

DOS Command

DOS Command

etc

etc.

The format for the goto command is

GOTO LabelName

Testgoto.bat

If "%1"=="C" goto cat

goto dog

:dog

echo you did not press a C you dog

goto done

:cat

echo you pressed a C

:done

Testerro.bat

@echo off

diskcopy a: a:

if errorlevel 4 goto need_memory

if errorlevel 3 goto no_read

if errorlevel 2 goto user_cntrlc

if errorlevel 1 goto read_error

goto done

:need_memory

echo Insufficient memory invalid drive or syntax error

goto done

:no_read

echo unable to read source or format target disk.

goto done

:user_cntrlc

echo diskcopy due to user interuption

goto done

:error

echo Nonfatal read or write error

:done

if errorlevel 4 goto need_memory

is e-status >= 4, if true goto need_memory, else do next if

if errorlevel 3 goto no_read

is e-status >= 3, if true goto no_read, else do next if

if errorlevel 2 goto user_cntrlc

is e-status >= 2, if true goto user_cntrlc, else do next if

if errorlevel 1 goto read_error

is e-status >= 1, if true goto read_error, else do next if

 

The if command has an addtional option the not switch. It changes the result of the condtional statement. For example if the condition is true, using the not changes it to a false. If the condition is false using the not changes the condition to true.

Experiment with these batch files during lab, create a text file called myfile.bat (enter the text "This is a test"). Use that file as a parameter for testnot.bat, testnot1.bat, and testnot2.bat.

 

Testnot.bat

@echo off

If exist %1 type %1

if not exist %1 echo %1 does not exist.

testnot1.bat

If exist %1 type %1

testnot2.bat

If not exist %1 type %1