Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lectures_SSD2_Yermakova / Lectures_SSD2 Yermakova.doc
Скачиваний:
226
Добавлен:
25.02.2016
Размер:
3.16 Mб
Скачать

4.3.2 Batch File Commands

  • Batch Files

  • Commands

Like Macros, which were discussed in 4.2.2 Lab: Macros, batch files automate tasks. This section gives an overview of batch files and covers some commands that can be used in batch files.

Batch Files

The term batch file originally meant non-interactive. In early computer systems, users would submit programs on decks of punched cards, which would be collected into batches and run. The output was then printed and returned to the users. As operating systems developed further, batch processing came to include a scheduling function, whereby the system would choose which job to run next based on priority level and resources required. It was also possible to specify that a job should run at a particular time of day (for example, do disk backups at night), or repeatedly, such as a weekly inventory report or monthly payroll processing at a company. The central idea, though, was that batch programs were self-contained and did not rely on user interaction.

A batch file under Windows, or a "shell script" in UNIX, performs a similar function. It consists of a sequence of commands to run programs and manipulate files. It is possible to construct completely autonomous batch files that require no user interaction to run, but this is not strictly necessary. The DOS command-line processor, which executes batch files, does provide for some simple forms of user interaction, as you will see.

A batch file under Windows is a plain text file (not a Word file), with the extension .BAT. A batch file contains DOS commands and names of programs to execute. The file may also contain flow control commands that tell the command processor to loop (repeat certain instructions) or to handle error conditions in a particular way.

A common use of batch files is to write new commands. For example, here is a batch file SWAP.BAT that swaps two files by renaming them:

REM Here is the source to SWAP file1 file2 @ECHO OFF REM Remember that / precedes a switch. REM Use HELP DEL or DEL /? to find out about /Q DEL/Q TEMP MOVE %1 TEMP MOVE %2 %1 MOVE TEMP %2

The notation %1, %2, etc., refers to the arguments to the swap command. If the user types "SWAP A.TXT B.DOC" to the command line, then SWAP refers to a new command defined by our batch file, and while the batch file is executing, the %1 is equivalent to A.TXT, and the %2 is equivalent to B.DOC. It is also possible to introduce named variables in a batch file, such as %NAME%, by using assignment statements.

Creating a batch file is actually a type of programming—using a "language" that is very limited. One unusual feature of this language is that a syntax error in one line does not stop the execution of the file. If a syntax error is encountered, an error message is displayed and the batch file continues with the next command. This could have unintended consequences. Just as with other types of programs, it is important to test a batch file thoroughly before using it in any critical application.

Commands

The following is a list of command-line features commonly used in batch files:

rem remark  This designates anything that follows the rem command on the line (remark in our example) as a comment.

echo message  This command displays message to Standard Output. The echo command can also be used with output redirection to send a message to a file. For example, echo starting stage three.

echo.  Notice the period ( . ) at the end of the echo command. This is a special variation of the echo command, which displays a blank line to Standard Output.

@echo off  This stops commands in the batch file from being displayed to Standard Output. By default, each line of text in the batch file encountered by the command processor is displayed to Standard Output. While this default mode is useful for debugging purposes, the resulting display often confuses users, so most commands written as batch files begin with @ echo off.

pause  This command interrupts the execution of the batch file and displays the message "Press any key to continue...." Execution resumes when a single keyboard key is pressed. This command is useful if you want to make a batch file stop and wait for the user to perform some action (such as inserting a floppy disk) before it continues executing.

: label  Any line in the batch file that starts with a colon ( : ) is considered a label. Labels are used by certain flow control commands to repeat or skip over certain lines in the file. After the colon ( : ), provide some appropriate name as the label name. The name should not contain any embedded spaces.

goto label  This command causes the command processor to execute the first command line after the label referenced by the command.

if exist filename command  This command evaluates the current working directory for a file or subdirectory. In the example above, if the statement is true (that is, if a file named filenameexists), then the command (command in our example) is executed. Therefore, a command that reads if exist index.html del index.html determines if a file named index.html exists and, if it does, deletes it.

The lab in the next section gives examples of using these commands in the context of a batch file.