Chapter 10 Introduction to Batch Files
Total Page:16
File Type:pdf, Size:1020Kb
Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files Chapter 10 Introduction to Batch Files LEARNING OBJECTIVES 1. Compare and contrast batch and interactive processing. 2. Explain how batch files work. 3. Explain the purpose and function of the REM, ECHO, and PAUSE commands. 4. Explain how to stop or interrupt the batch file process. 5. Explain the function and use of replaceable parameters in batch files. 6. Explain the function of pipes, filters, and redirection in batch files. STUDENT OUTCOMES 1. Use Edit to write batch files. 2. Use COPY CON to write batch files. 3. Write and execute a simple batch file. 4. Write a batch file to load an application program. 5. Use the REM, PAUSE, and ECHO commands in batch files. 6. Terminate a batch file while it is executing. 7. Write batch files using replaceable parameters. 8. Write a batch file using pipes, filters, and redirection. CHAPTER SUMMARY 1. Batch processing means running a series of instructions without interruption. 2. Interactive processing allows the user to interface directly with the computer and update records immediately. 3. Batch files allow a user to put together a string of commands and execute them with one command. 4. Batch files must have the .BAT or .CMD file extension. 5. Windows looks first internally for a command, then for a .COM files extension, then for a .EXE file extension, and finally for a .BAT or .CMD file extension. 6. Edit is a full-screen text editor used to write batch files. 7. A word processor, if it has a means to save files in ASCII, can be used to write batch files. ASCII files are also referred to as unformatted text files. 8. Batch files must be in ASCII. 9. A quick way to write an ASCII file is to use COPY CON. You copy from the console to a file. 10. Batch files are executed from the system prompt by keying in the batch file name. Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 1 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files 11. Batch files are used for many purposes, such as to save keystrokes. 12. To “document” means to explain the purpose a file serves. 13. REM allows the user to document a batch file. 14. When the operating system sees REM, it displays on the screen whatever text follows REM. REM is not a command that executes. 15. ECHO OFF turns off the display of commands. Only the messages from the commands are displayed on the screen. 16. PAUSE allows the user to take some action before the batch file continues to execute. 17. PAUSE does not force the user to do anything. The batch file just stops running until the user presses a key. 18. To stop a batch file from executing, press the <Ctrl> key and the letter C (<Ctrl> + C). 19. Replaceable parameters allow the user to write batch files that can be used with many different parameters. The replaceable parameters act as place holders for values that the user will substitute when executing the batch file. 20. Replaceable parameters are sometimes called dummy, positional, or substitute parameters. 21. The percent sign (%) followed immediately by a numerical value, 0 to 9, indicates a replaceable parameter in a batch file. KEY TERMS batch file environmental variable replaceable parameter batch processing interactive processing substitute parameter documented positional parameter variable dummy parameter LECTURE NOTES CHAPTER OUTLINE Chapter Overview Will learn to create batch files to automate a sequence of commands to accomplish various tasks. The use of batch file subcommands will be discussed and then used with batch commands. How to halt the execution of a batch file will be explained. Will write batch files using replaceable parameters. Learn how batch files can be used from the desktop. CONCEPTS OF BATCH AND INTERACTIVE PROCESSING Concepts of Batch and Interactive Processing Batch file. Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 2 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files Write command sequence that automates sequence of commands. Contains one or more commands. Write text file using Edit, COPYCON, or text editor such as Notepad. Can use word processor if it has a “Save as text file” option. File extension .BAT if using version earlier than Windows 2000 Pro. Beginning with Windows 2000 Pro may also use .CMD File must be an ASCII file. Once command file written - execute it by keying in name of batch file. OS reads and executes each line of batch file. Once running – attention or input not needed until batch file finished executing. Why batch files are used. Minimize keystrokes. Minimize possibility of errors. Complex sequence of commands put together and stored under one name. Automate frequent/consistent procedures that you always want to do in same manner. Do not need to interact with computer while they are running. Execute application programs by calling them with a batch file. “Batch” old data-processing term. Submitted job (all instructions needed to run job) to data-processing department – run jobs in batches. No one interacted with program. Job done – output delivered. Run a batch job – running a computer routine without interruption. Batch processing. Used to run one or more commands. Allows the computer to execute several commands without human interaction or interruption. Programs to be executed collected and placed into prioritized batches. Programs processed one after another without interaction or intervention. Payroll processing – issuing paychecks. Computer program calculates/prints paychecks without interruption. Can determine when to run data. Interactive processing. Also called online/real-time mode. User interacts directly with computer. Data processed without delay. Information processed as it occurs. Usually requires connection to another computer--often over phone lines. Example: ATM machines. Reviewing airline schedules. HOW BATCH FILES WORK How Batch Files Work Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 3 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files OS looks for programs in order. Looks internally for command. Looks at file extensions on default drive and directory .COM - command file. .EXE - executable file. .BAT or .CMD - batch file. If OS finds match, file is executed. File not found continues search using Priority Rules (other extensions valid). File not found – searches search path as set in PATH statement following Priority Rules. File not found – get error message “filename is not recognized as an internal or external command, operable program or batch file.” Batch file is a program. .BAT must be on default drive and directory or path must be set to location of batch file so you may invoke it. Each line in a batch file must contain only one command. USING EDIT TO WRITE BATCH FILES Using Edit to Write Batch Files Batch file is an ASCII text file. Any ASCII or text editor can be used to write batch files. Word processing programs can create text files if they have a nondocument or text mode. But programs are large. Take a long time to load into memory. OS provides simple editor. EDIT - a full screen editor. Creates file. Does not run or execute files. Execute files by keying in file name at system prompt in MS-DOS window. Batch files. Each line must contain only one command. Can have any legal name but must have extension .BAT. or .CMD Text Editor in Windows interface is Notepad. Need graphical user interface to use Notepad. Edit can work at command line. ACTIVITY—WRITING AND EXECUTING A BATCH FILE Writing and Executing a Batch File DATA disk in Drive A, A: \ displayed. Instructions show keystrokes, not mouse steps. In some systems, mouse will not work in Edit unless Edit properties changed to open Edit in full-screen mode. Amount of space shown as remaining on disk will vary – depends on size/placement of batch files on disk. Activity steps. Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 4 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files Key in: EDIT EXAMPLE.BAT DIR *.99 DIR C:\*.99 Press <Alt> + F then X then Y Key in: DIR EXAMPLE.BAT EXAMPLE Activity completed. WRITING AND EXECUTING A BATCH FILE TO SAVE KEYSTROKES Writing and Executing Batch Files to Save Keystrokes Batch files can save keystrokes. Use batch files as shortcuts. Following are examples. DIR /AD. Shows the subdirectories on DATA disk. Can do this task by pressing one key instead of seven. /A switch means attribute – attribute want displayed is D for directories. DIR/OS and DIR/O-S. O stands for order. S stands for sort. Reverse order--placing smallest files at end of listing. Can do either of these tasks by pressing one key instead of eight. EDIT too powerful so use COPY CON command instead. Syntax – See PowerPoint slide #26. Press <F6> and then <Enter> when finished keying in text. Drawbacks of using COPY CON. Cannot correct errors once <Enter> is pressed at end of command line. Cannot correct errors in an existing file. To correct errors need editor such as EDIT. Nothing faster than using COPY CON. ACTIVITY—WRITING AND EXECUTING A ONE-LETTER BATCH FILE Writing and Executing a One-Letter Batch File DATA disk in Drive A, A: \> displayed For examples, COPY CON shown. To correct errors rekey data or use MS-DOS editor. Can correct COPY CON with DOSKEY or function keys. Activity steps.