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

4.3.3 Lab: Creating a Batch File

  • Example of a Batch File

  • Example of a Batch File with Arguments

The following are examples of batch files using some of the commands and functions described in the preceding section. Try to follow each example and determine what actions are being performed by the commands in the batch file.

Example of a Batch File

The following is a simple batch file called COPYWP.BAT that copies all Microsoft Word and PowerPoint files from the C:\data\smith folder to one or more floppy disks. A batch file normally terminates after executing the last command in the file, but this example will not allow terminate, because of the goto command. Hence, we must employ another method for terminating a batch file: press CTRL+C on the keyboard (that is, hold down the CTRL key and press the "C" key). The command processor will then ask if you really want to terminate the batch job, just in case you pressed this key sequence by accident.

To run this batch file, a user would type COPYWP at the command prompt. Follow the execution of the DOS commands and batch file commands to determine what actions are being performed. If this was something you were doing frequently using Windows Explorer, imagine how many point and click operations that would involve overtime. Instead of all of those interactions, a user could accomplish the same goal by just typing the batch file name at the command prompt.

REM Batch file created by John Smith @ ECHO OFF ECHO Copying Word and PowerPoint files to a floppy :LOOP ECHO Please insert a floppy into drive A: PAUSE COPY C:\data\smith\*.doc a:\ COPY C:\data\smith\*.ppt a:\ GOTO LOOP

Example of a Batch File with Arguments

The previous example always copied Word and PowerPoint files from the C:\Data\smith directory. What if Jane Jones wanted to use this batch file, but on her directory that is called C:\data\jones? Either another version of the batch file would have to be created under a different filename, or arguments could be used. In this example, the user must type on the command line not only the name of the batch file, but also the user's directory name. For example, type "COPYWP smith" to backup John Smith's files (which will cause all references to %1 in the batch file to be literally replaced by "smith"), and "COPYWP Jones" to backup Jane Jones' files (which will cause all references to %1 in the batch file to be literally replaced by "Jones").

REM Batch file created by John Smith @ ECHO OFF ECHO Copying Word and PPT files :LOOP ECHO Please insert floppy into drive A: PAUSE COPY C:\data\%1\*.doc a:\* COPY C:\data\%1\*.ppt a:\* GOTO LOOP

Batch files may take more than one argument. The first argument is %1, the second %2, the third %3, and so on. In this example, the user must type not only the previous information, the batch filename, and the user's directory, but also the extension of the file to be copied. This batch will be able to copy from any user's directory any group of files to the floppy drive.

REM Batch file created by John Smith @ ECHO OFF ECHO Copying files :LOOP ECHO Please insert floppy into drive A: PAUSE COPY C:\data\%1\*.%2 a:\* GOTO LOOP

Learning Exercise:

This exercise will give you practice creating and executing batch files using the examples that appear in this page.

  • Use Microsoft Word to create files C:\data\smith\file1.doc and C:\data\smith\file2.doc. You can put whatever you like in these files. Note: if you are unable to create files with this specific path, then choose a path that works and modify the remaining steps appropriately.

  • Start the Windows Notepad text editor (located in the Accessories menu).

  • Type "REM " followed by your name. Be sure to include a space after "REM".

  • On the File menu (of Office Button menu if you are using Word 2007), click Save As.... In the Save as Typedrop-down list, choose "All Files". This is necessary to create a text file with an extension that is not ".txt". Save this file in the C: drive's root folder with the name "copywp.bat".

  • Copy the example batch file that appears in the first section of this page, "Example of a Batch File," and then paste it into your newly created copywp.bat. Check your spelling, syntax, and spaces, and then save your work.

  • Start the DOS Command Line program (typically found in the Programs menu). Run the batch file by typingcopywp on the command line. Note that you do not actually have to insert a floppy disk in the A: drive. If you do not do this, the copy command will report an error; click on "Ignore" to proceed. Determine if you have any syntax or logic errors in your batch file.

  • Go to the Notepad window. Modify copywp.bat with the modifications shown in the first example in the section "Example of a Batch File with Arguments," and then save your work.

  • Go to the Command Prompt window. Run the batch file by typing "COPYWP smith". Determine if you have any syntax or logic errors.

  • Go to the Notepad window. Modify your copywp.bat with the modifications shown in the second example in the section "Example of a Batch File with Arguments" and save your work.

  • Go to the Command Prompt window. Run the batch file by typing "COPYWP smith doc". Determine if you have any syntax or logic errors. Try the various menu choices.

  • Close both the Notepad and Command Prompt windows.