<<

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, , and PAUSE commands. 4. Explain how to stop or interrupt the 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. 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 . 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 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 key and the letter C ( + 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 (%) 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 (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.  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 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 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 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  *.99  DIR C:\*.99  Press + 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 and then when finished keying in text.  Drawbacks of using COPY CON.  Cannot correct errors once 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.  Key in:  COPY CON D.BAT  DIR /AD  Press  Key in: Press after each line.  TYPE D.BAT  D  COPY CON S.BAT

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 5 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 DIR /O-S   TYPE S.BAT  S  Activity completed.  USING BATCH FILES TO ALTER YOUR COMMAND LINE ENVIRONMENT  Using Batch Files to alter Command Line Environment  Open command line session by:  Clicking icon on Menu  Clicking shortcut  Shortcut can be altered to run in a customized way.  Suggested (Ch 8) to create Batch subdirectories to hold batch files and Utils to hold utility files.  Above user created directories not part of normal path in command line window

ACTIVITY: CREATING A BATCH FILE TO ALTER THE COMMAND LINE SESSION ENVIONMENT  Activity – Creating a batch file to alter the command line session environment.  DATA disk in Drive A , A: \> displayed.  Activity steps.  Key in:  MD Batch  MD Utils  DIR /AD  REN Batch BATCH  REN Utils UTILS  DIR /AD  EDIT A\BATCH\SETPATH/BAT  In Edit screen key in:  PATH = %PATH%;A:\BATCH;A:\UTILS  Press + F then X then Y  Close Command line window  Point to Programs then Accessories then Command Prompt icon  Drag command icon onto the desk  Click Copy Here  Right-click shortcut icon  Click Properties then Shortcut tab  Click in Target text box  Press key then  Key in /k A:\BATCH\SETPATH.BAT  Click Apply then OK  Double-click shortcut to Command line icon on desktop  Key in from default prompt A:  Key in PATH  Close command line window

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 6 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 Remove DATA disk from A drive  Double-click shortcut icon  Insert DATA disk into A drive  Click Try Again  Close All open windows  Drag shortcut to Recycle bin to delete it.  Activity completed.

WRITING A BATCH FILE TO LOAD AN APPLICATION PROGRAM  Writing a Batch File to Load an Application Program  Took three steps to execute BOG game.  Change directory where program file was located.  Loaded BOG.EXE  Exited game and returned to root directory.  Put these commands in a batch file.

ACTIVITY: WIRITNG A BATH FILE TO EXECUTE THE BOG GAME  Writing a Batch File to Execute BOG game  Any text editor may be used. If you have more than one line using an editor is easier than using COPY CON  Edit program will be used.  Instructions for keyboard will be used. Can use mouse if you wish.  Activity steps.  Key in:  EDIT BOG.BAT  /D C:\WUGXP\GAMES\BOG2  BOG  A:  Press + F then X then Y  Key in:  TYPE BOG.BAT  BOG  Click Program button  Activity completed.

CREATING SHORTCUTS FOR BATCH FILES ON THE DESKTOP  Creating Shortcuts for Batch Files on the Desktop  Any batch file can be run from the Windows environment.  To run batch file from Windows:  Double-click file name in Windows Explorer/My Computer.  Create shortcut for file and place it on desktop.  Can do some things with shortcut that cannot do in the command line interface.  Click shortcut to execute batch  Can change shortcut icon.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 7 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

ACTIVITY—CREATING A SHORTCUT ON THE DESKTOP  Creating a Shortcut on the Desktop  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in: EXIT  Right-click blank area on desktop  Point to New then Click Shortcut  In location box, key in C:\WUGXP\GAMES\GOB2\BOG.EXE  Click Next  In name box key in My BOG Game  Click Finish then Right-click shortcut  Click Properties then Shortcut tab then Change Icon button then Browse then down arrow of Look for icons in this file text box then C drive icon then Windows directory then System32 directory then shell32.dll then Open  Select icon  Click Apply then OK  Double-click icon  Click Exit Program  Drag shortcut to BOG to Recycle Bin  Activity completed.

BATCH FILES TO RUN WINDOWS PROGRAMS  Batch Files to Run Windows Programs  Where W system files reside will vary depending on installation of WXP.  WINNT directory – upgrade to W2K  Windows directory – upgrade from W98 or new installation.  Windows “keeps notes” about itself in system environment  Path stored in environmental variable %Path%  Drive referred to in environment as %SystemDrive%  Directory where system files are located is referred to as %SystemRoot%  Can use above information to create batch file to run small programs that come with Windows such as Notepad or Calculator  Using Notepad  Can create batch file that allows you to run program without having to return to the desktop.  Can create log file that adds current date and time to file created with Notepad.

ACTIVITY—CREATING A BATCH FILE TO RUN NOTEPAD  Creating a Batch File to Run Notepad  Shelled out to Command Line  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in each of the following lines. After each line press .  EDIT N.BAT  %SYSTEMROOT%\NOTEPAD.EXE  A:  Key in:

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 8 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

+F then X then Y then N then .LOG.  Click File/Save As.  In File name text box key in: A:\Log.TXT  Click Save/File/Exit  Key in each of the following lines. After each line press .  EDIT log. bat  %systemroot%\notepad log.txt  DIR*.AAA  Press + F then X then Y  Key in: LOG  In Notepad key in: My first entry in my log file.  Press + F then X then Y  Edit and save LOG.BAT file so it reads: START notepad log.txt START “THE 99 FILES WINDOW” DIR*.AAA  Notepad window active key in: The second entry in my log file.  Click File/Save/File/Exit.  Key in: EXIT twice.  Activity completed.

SPECIAL BATCH FILE COMMANDS  Special Batch File Commands  Ten commands designed to be used in batch files.  Make batch files – versatile.  Commands and their Purpose – See PowerPoint slides #41-42.  Will concentrate on REM, ECHO, and PAUSE.

THE REM COMMAND  The REM Command  Stands for “Remarks”.  Allows user to place explanatory text in files that are displayed but not executed.  If line begins with REM –  Anything that follows is not a command.  Will not be executed.  Documents a batch file.  Gives an explanation about the purpose of a program.  Documentation can also include name of batch file, time and date it was last updated, and author.

ACTIVITY—USING REM  Using REM  Shelled out to Command Line window  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in each of the following lines. After each line press .  COPY CLASS\JUP.*  EDIT TEST2.BAT

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 9 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 REM This is a test file  REM to see how the REM  REM command works  TYPE JUP.BUD  COPY JUP.BUD JUP.XYZ  Press + F then X then Y  Key in:  TYPE TEST2.BAT  TEST2  Activity completed.

THE ECHO COMMAND  The ECHO Command  Means display to the screen.  Normally, all command and command outputs go to the screen.  Default is ECHO ON.  ECHO OFF.  Turns off display of commands but not of output of commands.  Must be turned off by user.  Minimizes clutter.  Purpose of ECHO command.  Redirect a special character to printer so printer would eject a page.  Minimize screen clutter.

ACTIVITY—USING ECHO  Using ECHO  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in:  COPY TEXT2.BAT TESTING.BAT  EDIT TESTING.BAT  ECHO OFF  Press + F then X then Y  Key in:  TYPE TESTING.BAT  TESTING  EDIT TESTING.BAT  @ (do not press  Press + F then X then Y  Key in  TESTING  EDIT TEXTING.BAT  Place the cursor under the C in COPY and press key  Place cursor in blank line and key in ECHO ON  Press + F then X then Y  Key in TESTING

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 10 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 Activity completed.

THE CLS COMMAND  The CLS Command  CLS (Clear Screen) –  Can be used to clear screen when there is a lot of information on screen that is no longer needed.

ACTIVITY: USING CLS  Activity – Using CLS  Activity steps.  Key in: EDIT TESTING.BAT  Place cursor under R in first REM command and press key  In blank line created key in CLS  Press + F then X then Y  Key in: TESTING  Activity completed.

THE PAUSE COMMAND  The PAUSE Command  Stops execution of batch file pending user keystroke.  Does not force user to do anything.  Waits for the user to take some action.  Action is pressing any key.

ACTIVITY—USING PAUSE  Using PAUSE  DATA disk in Drive A,A: \> displayed.  Activity steps.  Key in: EDIT TEST2.BAT  Press +  Key in:  PAUSE You are going to delete JUP.XYZ  DEL JAN.XYZ  Press + F then X then Y  Key in:  TYPE TEST2.BAT  TEST2  Press  Key in: DIR JUP.XYZ  Activity completed.

STOPPING A BATCH FILE FROM EXECUTING  Stopping a Batch File from Executing  Can press

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 11 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

+ C.  + .  Return to system prompt when batch file interrupted.  Rest of batch does not execute.

ACTIVITY—QUITTING A BATCH FILE  Quitting a Batch File  DATA disk in Drive A, A: \> displayed.  Activity steps.  Key in: TEST2  Press + C then Y  Key in: DIR JUP.XYZ  Activity completed.

REPLACEABLE PARAMETERS IN BATCH FILES  Replaceable Parameters in Batch Files  Can use fixed or variable parameters with batch files.  DIR - /W parameter is fixed - cannot choose another letter.  TYPE uses variable or replaceable parameters.  TYPE requires one parameter - a file name.  File name used will vary.  Since batch files are also commands, can use replaceable parameters.  Also called positional, dummy, or substitute parameters.  Parsing:  To analyze something in orderly way.  Linguistics – to divide words/phrases into different parts to understand relationships and meanings.  Computer – to divide computer language into parts that can be made useful to computer.  Can parse batch commands.  Keying in additional information on command line that batch file can use.  When writing batch files user supplies markers (place holders) to let batch file know that variable will be keyed in with batch file names.  Placeholder in percent sign (%) followed by a number from 0 – 9.  % sign indicates parameter is coming.  Numbers indicate what position the parameter is on the command line.  %0 – Usually command itself.  Power of batch files is their ability to use replaceable parameters.  Replaceable parameters allow much greater strength in batch files.

ACTIVITY—USING REPLACEABLE PARAMETERS  Using Replaceable Parameters  DATA disk in Drive A, A: \> displayed.  Activity steps.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 12 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 Key in:  TYPE JUP.XYZ  DIR JUP.XYZ  ECHO > JUP.XYZ  TYPE JUP.XYZ  DIR JUP.XYZ  EDIT KILLIT.BAT  ECHO > JUP.XYZ  TYPE JUP.XYZ  DEL JUP.XYZ  Press + F then X then Y  Key in:  TYPE KILLIT.BAT  KILLIT  EDIT KILLIT.BAT  REM This batch will make  REM the data in a file difficult to recover.  DIR %1  PAUSE You are going to kill the file, %1. Are you sure?  Replace ECHO > JUP.XYZ with ECHO > %1  Delete TYPE JUP.XYZ  Replace DEL JUP.XYZ with DEL %1  Press + F then X then Y  Key in: TYPE KILLIT.BAT then KILLIT JUP.BUD  Press  Activity completed.

MULTIPLE REPLACEABLE PARAMETERS IN BATCH FILES  Multiple Replaceable Parameters in Batch Files  Many commands such as COPY and REN require more than one parameter.  Batch files can contain up to 10 replaceable parameters, %0-%9.  Replaceable parameters are sometimes called positional parameters.  OS uses position number in command line to determine which parameter to use.  Parameters are placed in order from left to right.  See PowerPoint slide #69 COPY MYFILE.TXT YOUR.FIL  COPY first position (%0), MYFILE.TXT second position (%2), etc.

ACTIVITY—USING MULTIPLE REPLACEABLE PARAMETERS  Using Multiple Replaceable Parameters  Note: DATA disk in Drive A with A: \> displayed.  Activity steps.  Key in:  EDIT MULTI.BAT  REM This is a sample batch file  REM using more than one replaceable parameter.  TYPE %3  COPY %1 %2

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 13 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 TYPE %1  Press + F then X then Y  Key in:  TYPE MULTI.BAT  COPY CLASS\VEN.*  MULTI VEN.ABC JUP.AVC FILE2.SWT  EDIT NOCOPY.BAT  REM This batch file, NOCOPY.BAT, will hide specified files,  REM then copy all other files from one location to another,  REM then unhide the original files.  ATTRIB +H %1  COPY %3\*.* %2  ATTRIB -H %1  Press + F then X then Y  Key in:  TYPE NOCOPY.BAT  NOCOPY CLASS\*. ABC TRIP CLASS  DIR CLASS\*.ABC  DIR TRIP\*.ABC  Activity completed.

CREATING USEFUL BATCH FILES  Creating Useful Batch Files  Automates processes that take numerous commands in succession.  Can create commands that are not provided with the operating system.  Next batch file will solve a problem.

ACTIVITY—WRITING USEFUL BATCH FILES  Writing Useful Batch Files  DATA disk in Drive A, A: > displayed.  Activity steps.  Key in:  EDIT DELTREE.BAT  REM This file will delete entire directory structures  REM making use of the newer command, RD /S with the  REM old command name, DELTREE  RD /S %1  Press + F then X then Y  Key in:  TYPE DELTREE.BAT  MD TRYIT  MD TRYIT\AGAIN  COPY ASTRO\*.* TRYIT\AGAIN  DELTREE TRYIT  Y  DIR TRYIT  MD ASTRO2  COPY ASTRO\*.* ASTRO2  EDIT DCOMP.BAT

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 14 Instructor’s Manual Chapter 10 Lecture Notes Introduction to Batch Files

 REM This batch file will compare the file names  REM in two directories  DIR /A-D /B /ON %1 > SOURCE.TMP  DIR /A-D /B /ON %2 > OTHER.TMP  FC SOURCE.TMP OTHER.TMP | MORE  PAUSE You are about to delete SOURCE.TMP and OTHER.TMP  DEL SOURCE.TMP  DEL OTHER.TMP  Press + F then X then Y  Key in: DCOMP ASTRO ASTRO2  Press  Key in:  DEL ASTRO2\ZODIAC.FIL  DCOMP ASTRO ASTRO2  Press  Key in:  DELTREE ASTRO2  Y  Activity completed.

Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line, Instructor’s Manual Franklin, Beedle & Associates © 2003 Page 15