xxxx / Lab3
.docxLaboratory work activity 3. Using conditions and organizing dialog in batch programs.
Conditions
We can use labels and GOTO command to transfer control to the labeled line. Type a label on a line by itself, beginning with a colon.
Example 8. Cond.bat
@echo off
copy %1 %2
goto Label1
echo This string will never be executed
:Label1
rem From here program is continued
type %2
Next is IF command, that performs conditional processing in batch programs. It is used in three ways, as shown below.
-
IF [NOT] string1==string2 command
-
IF [NOT] EXIST filename command
-
IF [NOT] ERRORLEVEL number command
Where NOT specifies that Windows should carry out the command only if the condition is false string1==string2 specifies a true condition if the specified text strings match
EXIST filename specifies a true condition if the specified filename exists
ERRORLEVEL number specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
command specifies the command to carry out if the condition is met.
In the first IF case we check the value of the variable.
Example 9.
@echo off
if %1==%2 echo Parameters are equal!
if %1==Alice echo Hi, Alice!
echo The end
Run the program and do not specify arguments in the command line.
When you compare strings there is a possibility that the parameter is an empty string. Or when a batch program processes command line arguments there is a possibility that the argument is not specified. In these cases the program will be abnormally terminated. To avoid such situation we need to add some symbol, for instance ‘-’, as shown in Example 10.
Example 10.
@echo off
if -%1==-%2 echo Parameters are equal!
if -%1==-Alice echo Hi, Alice!
echo The end
If you run the program without arguments in the command line, the program will assume that the argument (arguments) is an empty string.
With IF and SHIFT commands we can process all the arguments in the command line, even if we do not know how many arguments are there.
Example 11. Display file name and all its arguments in the command line.
@echo off
echo File %0 is running
echo.
echo Parameters of the file are:
rem Begin cycle
:BeginLoop
if -%1==- goto ExitLoop
echo %1
rem Shifting parameters
shift
rem Goto the beginning of the cycle
goto BeginLoop
:ExitLoop
rem Exit cycle
echo.
echo The end
In the second IF case we check the existence of the file, see example 12.
Example 12. Programs checks existence of file, which name is specified as an argument in the command prompt. If the filename is not specified, or if file exists, or do not exist – the corresponding message is displayed.
@echo off
if -%1==- goto NoFile
if not exist %1 goto FileNotExist
echo File %1 found
goto Exit
:NoFile
echo No file indicated
goto Exit
:FileNotExist
echo File %1 not found
:Exit
For the result refer to figure 3.

Figure 3. Check file existence
In the third IF case we check an exit code of the last program or command run.
Example 13. This program tries to copy a file. Result of command returned as an exit code.
@echo off
xcopy hi.txt D:\
rem Check exit code returned by command
if errorlevel 1 echo An error occurred
if not errorlevel 1 echo File copied successfully
Organizing dialog with a user
While batch program is running there is no possibility to input strings from the keyboard using standard commands. However there is a CHOICE command in Windows, which allows organizing some kind of a dialog with the user. This tool displays a list of choices and returns the index of the selected choice. The command is used only in batch program. The syntax is:
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Where:
/C choices - specifies the list of choices to be created. Default list is "YN".
/N - hides the list of choices in the prompt. The message before the prompt is displayed and the choices are still enabled.
/CS - enables case-sensitive choices to be selected. By default, the utility is case-insensitive.
/T timeout - the number of seconds to pause before a default choice is made. Acceptable values are from 0 to 9999. If 0 is specified, there will be no pause and the default choice is selected.
/D choice - specifies the default choice after nnnn seconds. Character must be in the set of choices specified by /C option and must also specify nnnn with /T.
/M text - specifies the message to be displayed before the prompt. If not specified, the utility displays only a prompt.
The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
Example 14.
@ECHO OFF
CHOICE /C YNC
Result:
[Y,N,C]?
Example 15.
@ECHO OFF
CHOICE /C YNC /M "Yes, No, or Cancel"
Result:
Yes, No, or Cancel [Y,N,C]?
Example 16.
@ECHO OFF
CHOICE /C YNC /M "Yes, No, or Cancel" /T 4 /D C
Result:
Yes, No, or Cancel [Y,N,C]?C
Example 17.
@ECHO OFF
ECHO Menu
ECHO.
ECHO A - choice A
ECHO B - choice B
ECHO C - choice C
ECHO.
CHOICE /C ABC /M Select
IF ERRORLEVEL 3 GOTO ChoiceC
IF ERRORLEVEL 2 GOTO ChoiceB
IF ERRORLEVEL 1 GOTO ChoiceA
:ChoiceA
ECHO Choice A is selected
GOTO Done
:ChoiceB
ECHO Choice B is selected
GOTO Done
:ChoiceC
ECHO Choice C is selected
:Done
