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

4.3 Batch Script Files

In 4.2.2 Lab: Macros, we discussed the macro facility that exists in many applications for ensuring that a user can define a shortcut for a set of actions that are used frequently. The same facility is often required when interacting with the operating system's user interface (often called the "command interpreter" or "shell"). For example, you may want to set up a daily routine that copies all word processing files to a removable disk and deletes all the files in the "Temp" folder on the hard drive. In addition, perhaps you want these actions to be performed in the middle of the night when there is nobody around to initiate them.

If you are using an operating system with a GUI interface such as Windows, there is no facility to record the sequence of actions you perform using a mouse to be repeated later. The solution is to create a text file (called a "batch file" under DOS and Windows, and a "script" under UNIX) that uses the operating system's command line interface to achieve these actions.

Reading Sequence:

  • 4.3.1 Advanced Command-Line FunctionsLearning Goal: Knowledge of syntax, wildcards, redirection, piping, and frequently used file commands used within batch files.

  • 4.3.2 Batch File CommandsLearning Goal: More knowledge about the use of batch files and of some DOS commands specific to batch files. In Addition: Check out Rob van der Woude's Scripting Page. This page contains information on scripting languages in general and batch files in specific.

  • 4.3.3 Lab: Creating a Batch FileLearning Goal: Practical experience in creating and running DOS batch files.

         

Assessments:

  • Multiple-Choice Quiz 14

4.3.1 Advanced Command-Line Functions

  • DOS Command Syntax

  • Review of File System Commands

  • Wildcard Characters

  • Redirection and Piping

You have already been exposed to the DOS command line in 4.2.1 Lab: DOS Commands. It is probably more proper to call it the "Microsoft command line," as it is not limited to DOS. Virtually the same command-line interface is used in all versions of the Windows operating system.

With today's GUI environment, most computer users rarely need to use the command line. Initial installation of Windows, and recovery from problems that prevent the Windows GUI from executing, are two situations where a user would need to rely on the command line, but neither is a common occurrence. However, one common task where the command line is the preferred solution is the creation of scripts called "batch files". Batch files are text files containing DOS commands used to run programs and manipulate files.

In the remainder of this page, we will discuss some advanced features of the command line. Then, in the following two pages, we will see how batch files are constructed.

Dos Command Syntax

DOS commands have a specific syntax, which you have already had a glimpse of earlier in the course. Each line begins with a command name or program name. (Many DOS commands are actually programs.) After the command name may come one or more switches. A switch is written as a slash followed by a letter. For example, dir /w tells the directory command to use a "wide" list format. Switches can also take parameters, which are separated from the switch by a colon. dir /o:s tells the dir command to order the files by size, while dir /o:-g tells it to put directories at the end of the listing instead of at the beginning.

Commands can also take arguments, such as filenames or paths. Other types of arguments are possible; the exact form is determined by the syntax of the command. The command dir /w c:\ has the path c:\ as its argument; it prints a wide listing of the files in c:\.

The /? switch tells a command to display its Help entry, which includes the command's syntax, any switches it accepts, and what the command does. If you know the name of the command you want to use but do not remember the syntax or what switches are available, you can use the /? switch to get help. Typing del /? on the Windows Me command line causes the following to display:

C:\> del/? Deletes one or more files. DEL [drive:][path]filename [/P] ERASE [drive:][path]filename [/P]   [drive:][path]filename  Specifies the file(s) to delete. Specify multiple                          files by using wildcards.   /P            Prompts for confirmation before deleting each file.

The above example illustrates some of the special conventions used by the DOS Help facility for precisely describing the syntax of a command:

  • Words appearing in uppercase—such as "DEL" and "ERASE"—should be typed in literally, as they are shown in the Help entry.

  • Words appearing in lowercase denote variable information to be supplied by the user. For example, "drive:" indicates that a disk drive letter (such as A: orC:) should be supplied, "path" indicates that a path should be supplied (such as \Windows\bin), and "filename" indicates that a file name should be supplied (such as program.exe).

  • Anything enclosed within square brackets ( "[" and "]" ) is optional. If the user does not specify a value for an optional item, the command processor uses a reasonable default value. For example, if the drive letter or the pathname is not supplied, the command processor assumes that the file is located in the current working directory, as indicated in the prompt. The dir command uses *.* as the default filename, meaning it will list all files in the working directory. But, for the del command, while a drive and path are optional, the file name must be supplied by the user.

  • An ellipsis ( ... ) indicates that the immediately preceding item can be repeated any number of times. This does not appear in the del example, but if it is stated filename ..., any number of files could be deleted at the same time. (In some versions of Windows, the del command does accept multiple file name arguments.)

  • With the exception of using multiple switches on a command line, all components must be typed in the exact order in which they appear in the Help entry's syntax description. Switches can appear in any order including in between other arguments.

The command processor prompts the user for keyboard input when it is waiting for a new command. The prompt often appears as a drive letter, a path, and a right angle bracket ( > ). For example, the prompt might be "C:\Windows>". The drive and path are referred to as the "working directory." If you specify a file path that does not start with the root folder indicator ( \ ), the command processor assumes that the path begins in the current working directory. It is best to provide a complete path when specifying a file, in order to avoid unintended consequences. What you type can be in uppercase or lowercase, as the command-line processor is not case sensitive.