Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

xxxx / Lab2

.docx
Скачиваний:
8
Добавлен:
25.02.2016
Размер:
324.3 Кб
Скачать

Laboratory work activity 2. Using environmental variables, loops and program call in batch programs.

Using environmental variables

In order to display, set or remove environment variables use set command. The syntax is

SET [variable=[string]]

where variable specifies the environment variable name and string specifies a series of characters to assign to the variable.

Type set without parameters to display the current environment variables. Set command invoked with just a variable name, no equal sign or value will display the value of all variables whose prefix matches the name given to the set command. For example:

set p

would display all variables that begin with the letter ‘p’.

Surround the variable name with %’s and the variable’s value will be used.

We can set environmental variables as numerical expressions using the /a switch.:

set /a expression

The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence:

Grouping

()

arithmetic operators

*, /, %

arithmetic operators

+, -

logical shift

<<, >>

bitwise and

&

bitwise exclusive or

ˆ

bitwise or

|

assignment

=, *=, /=, %=, +=, -=, &=, ˆ=, |=, <<=, >>=

expression separator

,

Example 1. Setting a variable.

Create vars.bat file and type the following commands:

@echo off

set a=one

echo a

echo %a%

The result is:

a

one

First echo displays the variable name, the second - variables value.

Example 2. Setting a numeric variable.

Create math.bat file and type the following commands:

@echo off

set /a m=1+7

echo m

echo %m%

The result is:

m

8

First echo displays the variable name, the second - variables calculated value.

Pause program execution and clear screen commands

This example batch program displays “Hello World!” prompts and waits for the user to press a key, and then terminates (refer to Figure 1).

@echo off

echo Hello World!

pause

Figure 1 – Pause program execution

The interpreter executes each line in turn, starting with the first. The @ symbol at the start of the line prevents the prompt from displaying that command. The command ECHO off turns off the prompt permanently, or until it is turned on again. Then the next line is executed and the ECHO Hello World! command outputs Hello World!, as only off and on have special functions. Then the next line is executed and the PAUSE command displays Press any key to continue . . . and pauses the script's execution. After a key is pressed, the script terminates, as there are no more commands.

echo echo Hello world!>>test1.bat

To clear the screen cls command is used at the beginning of the program.

Loops in batch programs

Loop is organized using FOR … In ... Do ... command. The syntax is:

FOR %variable|%%variable IN (set) DO command [command-parameters]

Where

%variable specifies a single letter replaceable parameter (when used in command prompt)

%%variable is used in command files

Variable names are case sensitive, so %i is different from %I.

The SET parameter in the FOR command specifies one or more strings separated by a comma. Command specifies the command to carry out for each string in the SET. Command-parameters specify parameters or switches for the specified command.

Example 3. Create a batch file loop.bat:

@echo off

for %%i in (One,Two,Three) do echo %%i

The result is:

One

Two

Three

Example 4. If the string contains a comma, that string should be putted in quotes.

@echo off

for %%i in (“One, two”,Three) do echo %%i

The result is:

One, two

Three

To avoid confusion between command parameters do not use numbers 0-9 as variables.

Example 5.

@echo off

for %%i in (1,2,3,4,5) do echo %%i

The result is:

1

2

3

4

5

The SET parameter can specify a set of one or more files (wildcards may be used). In this case command is carried out for each file in the SET.

Example 6. Create a batch file that outputs the names of all *.txt and *.bat files in the current directory to the file list.txt.

@echo off

for %%f in (*.txt,*.bat) do echo %%f >>list.txt

Calling programs

There is a possibility to call one batch program from another just indicating its name.

Example 7. Batch program proc.bat displays all txt-files in the current directory, and then calls another batch program:

@echo off

rem Display list of txt-files

dir *.txt

rem Call batch file

report.bat

echo Filenames are in the txt-file

pause

Batch program report.bat outputs names of all txt-files in the current directory into the report.txt file:

@echo off

for %%f in (*.txt) do echo %%f >>report.txt

The result is shown on Figure 2.

Figure 2. Result for report.bat

However in this case after the called batch program is executed control is not passed back to the main program and the statement after the name of the batch program will not be executed. In order to return back use CALL command as shown in example 8.

Example 8. Batch program proc-call.bat

@echo off

rem Display list of txt-files

dir *.txt

rem Call batch file

call report.bat

echo.

echo Filenames are in the txt-file

echo.

Pause

The result is shown on Figure 3.

Figure 3. Result for proc-call.bat

Here is another example.

Example 9. Files.bat:

@echo off

echo Write file %1.txt

echo Call parameter: %1>%1.txt

main.bat:

@echo off

for %%i in (one,two,three) do call files.bat %%i

Files.bat is called three times. As a result three file are created: one.txt, two.txt, three.txt. In one.txt there is a string “Call parameter: one”, in two.txt - “Call parameter: two”, in three.txt - “Call parameter: three”.

Соседние файлы в папке xxxx