410 likes | 688 Views
Chapter 5 Using Batch Files. Page 84. Terms and Concepts. Sequence of DOS Commands Text files Copy CON Batch File Name Internal or External command Name of program Another batch file. Terms and Concepts. Copy command Copy from console to file Autoexec.bat file
E N D
Chapter 5Using Batch Files Page 84
Terms and Concepts • Sequence of DOS Commands • Text files • Copy CON • Batch File Name • Internal or External command • Name of program • Another batch file Chapter 5 (39 slides)
Terms and Concepts • Copy command • Copy from console to file • Autoexec.bat file • Enter name of batch file to execute • Pass parameters after the batch file name • Conditional operation • Special commands: CALL, CHOICE, ECHO, FOR, GOTO, IF, REM, SHIFT and PAUSE Chapter 5 (39 slides)
Terms and Concepts • Use CTRL + C key combination to terminate • Use batch files to carry out common computer tasks • How is a batch file created? Can you use a word processing program to do it or do you have to use a programming language? • Why does a batch file have individual commands on separate lines? • How can a file be created using the DOS COPY command? Chapter 5 (39 slides)
Terms and Concepts • What is so special about the AUTOEXEC.BAT file? When is it used? • What are parameters and how are they used in a batch file? • What happens when you add the PAUSE command to a batch file? Chapter 5 (39 slides)
Objectives • Create batch files and define roles • Identify set of DOS batch commands • Use DOS COPY from the CONSOLE • Create an AUTOEXEC.BAT file Chapter 5 (39 slides)
Defining Batch Files • Batch files are text files • Use a word processor to create them • Or use DOS COPY CON • Each line needs to be: • A DOS batch command name • A DOS internal or external command name • The name of a program executable by your computer • Another batch file name Chapter 5 (39 slides)
Tryout: Creating a Batch File (Using Copy) Chapter 5 (39 slides) (Page 86) Change to the root of the O: Drive… Create a batch program named LOOK.BAT copy con look.bat What happened? Nothing seems to work now. Stopping a batch file (CTRL + C) Terminate batch job (Y/N) ? Type in the following for your batch file: dir tree
Tryout: Creating a Batch File (Using Copy) Chapter 5 (39 slides) Press the Enter key again End the input by using CTRL + Z (or F6) Look at the file you just created: Type LOOK.BAT Execute the batch file: Look <CR>
The AUTOEXEC Batch File • Special type of file • Starts when computer boots up • Used to carry out some series of tasks every time the machine starts Chapter 5 (39 slides)
Tryout: Creating an AUTOEXEC.BAT File Chapter 5 (39 slides) (Page 87) Start by entering COPY CON AUTOEXEC.BAT path n:\dos dir tree Store this file in the root directory of O: Drive Additional commands might be added... Time Date Path n:\dos tree
Tryout: An Example Batch File Chapter 5 (39 slides) (Page 88) Create a batch file named: LOAD.BAT dir *.txt Speller Editor
Passing Parameters to Batch Files • After the batch file name place your parameters separated by a space. • Do NOT use redirection symbols to get these parameters passed. • The SHIFT command is used in the batch file to shift through the parameters if needed • Parameters are accessed within the batch file by using the percent (%) and a number • As an example: %1 refers to the first one Chapter 5 (39 slides)
Passing Parameters to Batch Files • Conditional example: • If %1==start goto STARTLINE • %0 will be the name of the batch file Chapter 5 (39 slides)
Batch Command Summary DOS provides some special commands that can be used in batch files. They are summarized in the following sections. Complete descriptions of each of these commands can also be found in Chapter 7, DOS Command Reference. Chapter 5 (39 slides)
Call • Type: Internal • Syntax: CALL [d:][path]batchfilename [options] • The CALL command, added to DOS in Version 3.3, calls another batch file and then returns to the current batch file to continue. Chapter 5 (39 slides)
CHOICE • Type: Internal • Syntax: CHOICE [/C:[:]list of keys] [/N] [/S] [/T[:]choice,number] [text] • The CHOICE command is used to make your batch files interactive. It stops the batch processing and prompts the user to press a key. It displays a list of possible keys and provides a way to check to determine which key was pressed. Chapter 5 (39 slides)
ECHO • Type: Internal • Syntax: ECHO ON | OFF or ECHO (message) • The ECHO command turns on or off the display of commands in a batch file. Normally, all commands in a batch file are displayed as the batch file runs. ECHO OFF turns off this feature. ECHO ON turns the display feature back on. By using this command in your batch files you can turn the display feature on and off one or more times as the file runs. Chapter 5 (39 slides)
FOR • Type: Internal • Syntax: FOR %%(variable) IN set DO (command) • The FOR command performs repeated execution of commands (for both batch processing and interactive processing). It provides a way to repeat DOS commands in a predetermined way (usually to tell DOS to carry out a command on each of the files named in the set specifications). Chapter 5 (39 slides)
GOTO • Type: Internal • Syntax: GOTO (label) • The GOTO command causes an unconditional branch to the specified label and processing begins again with the line found immediately after the label. Chapter 5 (39 slides)
IF • Type: Internal • Syntax: IF [NOT] EXIST filename (command) or IF [NOT] (string1)==(string2) (command) or IF [NOT] ERRORLEVEL (number) (command) • When used in combination with other DOS commands, the IF command provides a way to conditionally carry out DOS commands. This command makes DOS batch files much more useful because it provides for options within the batch file. • Using the IF command, a condition can be tested to determine if it is true (or NOT true) and a DOS command can be issued depending on the outcome of the test. Chapter 5 (39 slides)
PAUSE • Type: Internal • Syntax: PAUSE [comment] • The PAUSE command suspends execution of a batch file until a key is pressed. Execution of the batch file is temporarily suspended at the point at which it appears in the file. After suspension of the batch file, DOS will display the message: Strike a key when ready ... Chapter 5 (39 slides)
REM (Remark) • Type: Internal • Syntax: REM [comment] • The REM command provides a way to insert remarks (that will not be acted on) into a batch file. During execution of a batch file, DOS will not act on text or commands which are entered on the same line as the REM command in a batch file. DOS will, however, display the remarks unless you use the ECHO OFF command to suppress this display. Chapter 5 (39 slides)
SHIFT • Type: Internal • Syntax: SHIFT • The SHIFT command provides a way to increase the number of replaceable parameters to more than the standard ten. Chapter 5 (39 slides)
Batch File Techniques • You might want to automate your world a bit • Create batch files for common tasks you do each day or week to reduce the time you spend typing or changing directories to get things done. • Doing this simple thing can save you potentially hours of time each week and month Chapter 5 (39 slides)
Useful Batch Files • The C.BAT batch file • Echo off • Cd \%1 • %2 • %3 • Example use: c letters edit Chapter 5 (39 slides)
Useful Batch Files • The D.BAT batch file • Echo off • dir %1 /p • Or…. • Echo off • Dir %1 /w • Example use: d \letters\*.txt Chapter 5 (39 slides)
Advanced Batch File Examples • The FORMAT.BAT batch file echo off if %1x==x goto MESSAGE if %1 == C: goto STOP if %1 == c: goto STOP if %1 == D: goto STOP if %1 == d: goto STOP formatx %1 Chapter 5 (39 slides)
Advanced Batch File Examples • The FORMAT.BAT batch file (continued…) goto END :STOP echo YOU CANNOT FORMAT THE HARD DISK (DRIVE %1) echo USING THIS PROGRAM goto END :MESSAGE echo No parameter entered. Enter FORMAT d: :END Chapter 5 (39 slides)
Advanced Batch File Examples • The BACK.BAT batch file echo off if %1x==x goto MESSAGE goto START :MESSAGE echo No parameter entered echo Enter a date (backup files created after this date) echo Use the format mm-dd-yy echo Terminating program goto END Chapter 5 (39 slides)
Advanced Batch File Examples • The BACK.BAT batch file (continued…) :START echo Ready to back up to drive A all files on the hard echo disk that were created or altered on or after the echo following date: [ %1 ] echo Press any key to continue or press Ctrl + C to cancel pause xcopy c:*.* /s /d:%1 a: :END Chapter 5 (39 slides)
Advanced Batch File Examples • Practical Use of BACK.BAT? Back 7-21-94 • This will backup all the files created after the date 7-21-94 to the disk in drive A. Chapter 5 (39 slides)
Interactive Batch Files • The GET.COM program is no longer available as of Windows 7 OS • We will not be able to do this section! Chapter 5 (39 slides)
Creating Menus with GET.COM • Not able to do… Chapter 5 (39 slides)
Using Interactive AUTOEXEC.BAT Files • Again…Not able to do this as of Windows 7 OS Chapter 5 (39 slides)
Creating Additional Batch Files Place your most useful batch files in one directory and then specify that directory using a PATH command on one line in your AUTOEXEC.BAT file. Then, you will be able use those batch files while working in any directory on any drive. Chapter 5 (39 slides)
Summary • Each line is acted upon in sequence • Command Name • Or Internal or external command • Or name of a program • Or another batch file name • AUTOEXEC.BAT is a special one • Create batch files using a ASCII text editor or COPY CON • Activate a batch file by using its name • Pass parameters by including after name Chapter 5 (39 slides)
Summary • DOS provides some special commands • CALL • CHOICE • ECHO • FOR • GOTO • IF • REM • PAUSE and • SHIFT Chapter 5 (39 slides)
Chapter 5 Assessments • Canvas (before class meets): • Pre Class Homework Questions • In-Class Lab Assignment 4 • Canvas (end of class time): • Windows/DOS Quiz 4 Chapter 5 (39 slides)