CS140M Fall 2014
Overview
• Will learn to create batch files to automate a sequence of commands to accomplish various tasks. Intro to Batch Files • 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. By Al Lake • Will write batch files using replaceable parameters. Fall 2014 LBCC • Learn how batch files can be used from the desktop. CS140M
Fall 2014 CS140M Lake 2
Concepts of Batch and Concepts of Batch and Interactive Processing Interactive Processing Batch file: Batch file: • Must be an ASCII file • String together series of commands • Created with Edit, COPY CON, text editor, or executed sequentially with one command word processor which has “Save as text file” without human interaction or interruption option • Has .BAT or .CMD extension • Each line in batch file contains only one command
Fall 2014 CS140M Lake 3 Fall 2014 CS140M Lake 4
ASCII Chart Concepts of Batch and Interactive Processing Why batch files are used: • Minimize keystrokes • Automate frequent/consistent procedures • Do not need to interact with computer while batch files are running
Fall 2014 CS140M Lake 5 Fall 2014 CS140M Lake 6
Lake 1 CS140M Fall 2014
Concepts of Batch and Concepts of Batch and Interactive Interactive Processing Processing Batch processing: Interactive processing: • Automate frequent/consistent • Also called online or real time mode procedures • User interacts directly with computer • Batch files can be run at anytime • Information processed without delay • No user/computer interaction required
Fall 2014 CS140M Lake 7 Fall 2014 CS140M Lake 8
How Batch Files Work How Batch Files Work
•The operating system determines if a file is a data file or a program file by the file extension. •File extensions: – .BAT batch file – .DOC MS Word file – .XLS MS Excel file
Fall 2014 CS140M Lake 9 Fall 2014 CS140M Lake 10
How Batch Files Work How Batch Files Work
To locate file, operating system: If file not found, get error message: • Looks internally for command • Using Extension Priority Rules, “Filename is not recognized as an searches on default drive and directory internal or external command, • Searches search path as set in PATH operable program, or batch file.” statement following Priority Rules
Fall 2014 CS140M Lake 11 Fall 2014 CS140M Lake 12
Lake 2 CS140M Fall 2014
How Batch Files Work Using an Editor to Write Batch Files
• A batch file is a program. Word‐processing programs can create text files if they have a • Each line in a batch file must nondocument or text mode. contain only one command.
Fall 2014 CS140M Lake 13 Fall 2014 CS140M Lake 14
Using an Editor to Write Batch Activity—Writing and Files Executing a Batch File KEY CONCEPTS: • Create batch files with any text editor or Notepad: word processor with ASCII output • Window’s text editor • Batch file extensions ‐ .BAT or .CMD • Execute the batch file the same ways as • Used to write batch files you do any command
Fall 2014 CS140M Lake 15 Fall 2014 CS140M Lake 16
Writing and Executing a Batch Writing and Executing a Batch File to Save Keystrokes File to Save Keystrokes
Batch files can save keystrokes. Sometimes easier to use COPY CON instead of EDIT to write simple batch file. Examples: DIR/AD Syntax: DIR/OS and DIR/O‐S COPY CON filename
Fall 2014 CS140M Lake 17 Fall 2014 CS140M Lake 18
Lake 3 CS140M Fall 2014
Writing and Executing a Activity—Writing & Executing Batch File to Save Keystrokes a One‐Letter Batch File
Drawbacks to using COPY CON: KEY CONCEPTS: • Cannot correct errors once • Pressing
Fall 2014 CS140M Lake 19 Fall 2014 CS140M Lake 20
Activity ‐ Creating a Batch File to Alter Using Batch Files to Alter Your the Command Line Session Environment Command Line Environment Open command line session by: KEY CONCEPTS: • Created shortcut to open command line • Clicking icon on Start menu session on desktop • Clicking shortcut • Can decide what directory to be in • Can be altered to run in customized way • With /k parameter ‐ include batch file that is executed each time command line session opened • Value limited on floppy • Valuable on hard drive
Fall 2014 CS140M Lake 21 Fall 2014 CS140M Lake 22
Creating Shortcuts for Creating Shortcuts for Batch Files on the Desktop Batch Files on the Desktop To run a batch file from Windows: Some things can be done with • Double‐click file name in shortcut that cannot do in the command line interface. Windows Explorer/My Computer • Click shortcut to execute batch • Create shortcut for file and place on desktop and click icon • Can change shortcut icon
Fall 2014 CS140M Lake 23 Fall 2014 CS140M Lake 24
Lake 4 CS140M Fall 2014
Batch Files to Run Windows Activity—Creating a Programs Shortcut on the Desktop KEY CONCEPTS: •Where Windows system files • Clicking shortcut executes file reside will vary depending on • Can change icon so it is more installation of Windows XP distinctive Professional.
Fall 2014 CS140M Lake 25 Fall 2014 CS140M Lake 26
Batch Files to Run Windows Activity—Creating a Batch Programs File to Run Notepad Windows “keeps notes” about itself in KEY CONCEPTS: system environment. • %SYSTEMROOT% (Environmental variable) o Windows knows where Windows files are located • Path stored in environmental variable and substitutes correct name %Path% o %% signs around name ‐ OS substitutes value for that variable. • Drive referred to in environment as • Opened Notepad without returning to %SystemDrive% desktop • Directory where system files are located •Used Notepad to create log file (case referred to as %SystemRoot% sensitive) •Created log file
Fall 2014 CS140M Lake 27 Fall 2014 CS140M Lake 28
Activity—Creating a Batch File to Run Notepad Batch File Commands KEY CONCEPTS: • START command: o Can start a program in new window while still executing batch program in previous window o If title following command is in quotes, will give window that title
Fall 2014 CS140M Lake 29 Fall 2014 CS140M Lake 30
Lake 5 CS140M Fall 2014
Batch File Commands The REM Command
REM command (remarks) allows user to place comments in file that are displayed but not executed
Fall 2014 CS140M Lake 31 Fall 2014 CS140M Lake 32
The REM Command Activity—Using REM KEY CONCEPTS: REM allows file to be documented: o Purpose of REM ‐ provide explanatory o Gives explanation of the purpose of a remarks about the batch file program o If line begins with REM ‐ explanatory text follows and no action taken o Can include name of batch file, last time it was updated, and author of o No more lines in batch file ‐OS returns to system level batch file
Fall 2014 CS140M Lake 33 Fall 2014 CS140M Lake 34
The ECHO Command The ECHO Command
ECHO ON command: ECHO OFF command: o Displays command and the o Minimizes screen clutter command’s output to the screen o User turns off display of command o Is the default value then only see command’s output
Fall 2014 CS140M Lake 35 Fall 2014 CS140M Lake 36
Lake 6 CS140M Fall 2014
The ECHO Command Activity—Using ECHO KEY CONCEPTS: o ECHO ON ‐ see remarks and commands o ECHO OFF ‐ output displayed not actual commands o @ECHO OFF ‐ @ suppresses display of ECHO OFF o Purpose of batch files defeated if interaction required by user
Fall 2014 CS140M Lake 37 Fall 2014 CS140M Lake 38
Activity—Using ECHO The CLS Command
CLS command used to clear screen when it becomes full of no longer needed information.
Fall 2014 CS140M Lake 39 Fall 2014 CS140M Lake 40
Activity ‐ Using CLS The PAUSE Command KEY CONCEPTS: PAUSE command instructs the o After ECHO turned off screen cleared batch file to stop executing until the user takes some action.
Fall 2014 CS140M Lake 41 Fall 2014 CS140M Lake 42
Lake 7 CS140M Fall 2014
Activity—Using Pause Stopping a Batch File from Executing KEY CONCEPTS: To interrupt a batch file during o Batch file “paused” ‐ stopped execution: executing until a key is pressed ‐ action is taken o Press
Fall 2014 CS140M Lake 43 Fall 2014 CS140M Lake 44
Stopping a Batch File Activity—Quitting a Batch from Executing File KEY CONCEPTS: o Results of Pressing
Fall 2014 CS140M Lake 45 Fall 2014 CS140M Lake 46
Replaceable Parameters Replaceable Parameters in Batch Files in Batch Files
Can use fixed or variable Example of fixed parameter: parameters with batch files. o DIR A: /W Examples of variable parameter: o TYPE THIS.FIL o TYPE TEST.TXT
Fall 2014 CS140M Lake 47 Fall 2014 CS140M Lake 48
Lake 8 CS140M Fall 2014
Replaceable Parameters Replaceable Parameters in Batch Files in Batch Files Can parse batch commands: • Batch files can also use replaceable parameters. • Keying in additional information on command line • Replaceable parameters also called • Dividing computer language into dummy, substitute or positional parts that can be made useful to parameters. computer
Fall 2014 CS140M Lake 49 Fall 2014 CS140M Lake 50
Replaceable Parameters Replaceable Parameters in Batch Files in Batch Files
User supplies markers (place holders) to let batch file know The place holder used in batch that variable will be keyed in files is the percent sign (%), with batch file name. followed by a number (0‐9).
Fall 2014 CS140M Lake 51 Fall 2014 CS140M Lake 52
Replaceable Parameters Activity—Using Replaceable in Batch Files Parameters KEY CONCEPTS: • The % sign is a signal to the operating • Data still on disk when file deleted system that a parameter is coming. • To delete data overwrite file with new data • Create generic or “plain wrap” batch file • The numbers (0‐9) indicate what o Supply specific parameter of file name of interest position the parameter is on in the o Supply place (“replaceable parameters”) for name of file command line. o View replaceable parameters as positional parameters o % sign followed by number
Fall 2014 CS140M Lake 53 Fall 2014 CS140M Lake 54
Lake 9 CS140M Fall 2014
Multiple Replaceable Multiple Replaceable Parameters in Batch Files Parameters in Batch File
Many commands used in Batch files can have up to 10 batch files may require replaceable (positional) more than one parameter. parameters (%0‐%9).
Fall 2014 CS140M Lake 55 Fall 2014 CS140M Lake 56
Multiple Replaceable Activity—Using Multiple Parameters in Batch Files Replaceable Parameters
Example: KEY CONCEPTS: • Within batch files parameters do not COPY MYFILE.TXT YOUR.FIL have to be in order. • Order used on command line o %1 ‐ refers to first position after command not first item on command line
Fall 2014 CS140M Lake 57 Fall 2014 CS140M Lake 58
Activity—Using Multiple Activity—Using Multiple Replaceable Parameters Replaceable Parameters KEY CONCEPTS: • Think of them as positional not replaceable parameters • Copy files selectively (all except ones hidden) • Do not use < > or | in batch files as OS reads them as redirection or pipe symbols
Fall 2014 CS140M Lake 59 Fall 2014 CS140M Lake 60
Lake 10 CS140M Fall 2014
Activity—Using Multiple Creating Useful Batch Files Replaceable Parameters
NOCOPY CLASS\*.ABC TRIP CLASS Can create commands that • REM This batch file, NOCOPY.BAT, will hide specified files • REM then copy all other files from one location to another are not provided with the • REM then unhide the original files. operating system. • ATTRIB +H CLASS\*.ABC • COPY CLASS\*.* TRIP • ATTRIB ‐H CLASS\*.ABC
Fall 2014 CS140M Lake 61 Fall 2014 CS140M Lake 62
Activity—Writing Useful Questions? Batch Files
Examine each part of command line:
DIR /A‐D /B /ON %1 > SOURCE.TMP
Fall 2014 CS140M Lake 63 Fall 2014 CS140M Lake 64
Assignment
• Create the following batch files: – Hello World –hello.bat • Clear screen • Print out “Hello World” – Open MS Word – word.bat • Change into another directory/folder or create a director/folder • Execute MS Word – Create a list of program file in a text tile • Change into a directory • Create a text file with all of the program files listed
Fall 2014 CS140M Lake 65
Lake 11