Introduction to Batch Files
Download
1 / 76

Introduction to Batch Files - PowerPoint PPT Presentation


  • 139 Views
  • Uploaded on

Introduction to Batch Files. Overview. Will learn to create batch files to automate a sequence of commands to accomplish various tasks. Overview. The use of batch file subcommands will be discussed and then used with batch commands. Overview.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introduction to Batch Files' - herman-munoz


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Overview
Overview

Will learn to create batch files to automate a sequence of commands to accomplish various tasks.

Ch 10


Overview1
Overview

The use of batch file subcommands will be discussed and then used with batch commands.

Ch 10


Overview2
Overview

How to halt the execution of a batch file will be explained.

Ch 10


Overview3
Overview

Will write batch files using replaceable parameters.

Ch 10


Overview4
Overview

Learn how batch files can be used from the desktop.

Ch 10


Concepts of batch and interactive processing
Concepts of Batch and Interactive Processing

Batch file:

  • String together series of commands executed sequentially with one command without human interaction or interruption

Ch 10


Concepts of batch and interactive processing1
Concepts of Batch and Interactive Processing

Batch file:

  • Must be an ASCII file

  • Created with Edit, COPY CON, text editor, or word processor which has “Save as text file” option

  • Has .BAT or .CMD extension

  • Each line in batch file contains only one command

Ch 10


Concepts of batch and interactive processing2
Concepts of Batch and Interactive Processing

Why batch files used:

  • Minimize keystrokes

  • Automate frequent/consistent procedures

  • Do not need to interact with computer while batch files are running

Ch 10


Concepts of batch and interactive processing3
Concepts of Batch and Interactive Processing

Batch processing:

  • Automate frequent/consistent procedures

  • Batch files can be run at anytime

    • No user/computer interaction required

Ch 10


Concepts of batch and interactive processing4
Concepts of Batch and Interactive Processing

Interactive processing:

  • Also called online or real time mode

  • User interacts directly with computer

  • Information processed without delay

Ch 10


How batch files work
How Batch Files Work

The operating system determines if a file is a data file or a program file by its extension.

Ch 10


How batch files work1
How Batch Files Work

To locate file, operating system:

  • Looks internally for command

  • Using Extension Priority Rules, searches on default drive and directory

  • Searches search path as set in PATH statement following Priority Rules

Ch 10


How batch files work table 10 1 search order for extensions pp 485 486
How Batch Files WorkTable 10.1 Search Order for Extensions pp. 485-486

Ch 10


How batch files work2
How Batch Files Work

Priority rules are followed if there are files on a disk with the same file name but three different file extensions.

Ch 10


How batch files work3
How Batch Files Work

If file not found, get error message:

“Filename is not recognized as an internal or external command, operable program, or batch file.”

Ch 10


How batch files work4
How Batch Files Work

  • A batch file is a program.

  • Each line in a batch file must contain only one command.

Ch 10


Using edit to write batch files
Using EDIT to Write Batch Files

To write a batch file you must

use a program that creates

ASCII text files.

Ch 10


Using edit to write batch files1
Using EDIT to Write Batch Files

If a file is readable with

the TYPE command,

it is an ASCII text file.

Ch 10


Using edit to write batch files2
Using EDIT to Write Batch Files

Word-processing programs can create text files if they have a nondocument or text mode.

Ch 10


Using edit to write batch files3
Using EDIT to Write Batch Files

EDIT:

  • Simple text editor in Command Prompt window

  • Used to write batch files.

Ch 10


Using edit to write batch files4
Using EDIT to Write Batch Files

Notepad:

  • Window’s text editor

  • Used to write batch files

Ch 10


Activity writing and executing a batch file
Activity—Writing and Executing a Batch File

KEY CONCEPTS:

  • Create batch files with:

    • EDIT - tool to write batch files

    • Any text editor or word processor with ASCII output

  • Batch file extensions - .BAT or .CMD

  • Execute the batch file the same ways as you do any command

Ch 10


Writing and executing a batch file to save keystrokes
Writing and Executing a Batch File to Save Keystrokes

Batch files can save keystrokes.

Examples: DIR/AD

DIR/OS and DIR/O-S

Ch 10


Introduction to batch files

Writing and Executing a Batch File to Save Keystrokes

Sometimes easier to use COPY CON instead of EDIT to write simple batch file.

Syntax:

COPY CON filename

Ch 10


Writing and executing a batch file to save keystrokes1
Writing and Executing a Batch File to Save Keystrokes

Drawbacks to using COPY CON:

  • Cannot correct errors once <Enter> is pressed

  • Cannot correct errors in existing files

Ch 10


Introduction to batch files

Writing and Executing a

Batch File to Save Keystrokes

  • To correct errors need text editor like EDIT.

  • Nothing is faster than COPY CON.

Ch 10


Introduction to batch files

Activity—Writing & Executing

a One-Letter Batch File

KEY CONCEPTS:

  • Pressing <F6> and <Enter> signals completion

  • <Ctrl> +Z same as pressing <F6>

  • Displaying file with TYPE indicates it is ASCII file

  • Create one-line batch files

  • Directories have no size

Ch 10


Using batch files to alter your command line environment
Using Batch Files to Alter Your Command Line Environment

Open command line session by:

  • Clicking icon on Start menu

  • Clicking shortcut

    • Can be altered to run in customized way

Ch 10


Introduction to batch files

Activity - Creating a Batch File to Alter the Command Line Session Environment

KEY CONCEPTS:

  • Created shortcut to open command line session on desktop

    • Can decide what directory to be in

  • With /k parameter - include batch file that is executed each time command line session opened

    • Value limited on floppy

    • Valuable on hard drive

Ch 10


Introduction to batch files

Writing a Batch File to Session Environment

Load Application Program

  • To execute BOG game took three steps.

  • Create a batch file to simplify loading an application program.

Ch 10


Introduction to batch files

Activity—Writing a Batch Session Environment

File to Execute BOG Game

KEY CONCEPTS:

  • Create batch files with:

    • COPY CON

    • Any text editor or word processor with ASCII output (Example: Edit)

  • Why no conflict between BOG.BAT and BOG.EXE names

  • How to execute command that requires particular location

Ch 10


Introduction to batch files

Creating Shortcuts for Session Environment

Batch Files on the Desktop

To run a batch file from Windows:

  • Double-click file name in Windows Explorer/My Computer

  • Create shortcut for file and place on desktop and click icon

Ch 10


Introduction to batch files

Creating Shortcuts for Session Environment

Batch Files on the Desktop

Some things can be done with shortcut that cannot do in the command line interface.

  • Click shortcut to execute batch

  • Can change shortcut icon

Ch 10


Introduction to batch files

Activity—Creating a Session Environment

Shortcut on the Desktop

KEY CONCEPTS:

  • Clicking shortcut executes file

  • Can change icon so it is more distinctive

Ch 10


Introduction to batch files

Batch Files to Run Windows Programs Session Environment

Where Windows system files reside will vary depending on installation of Windows XP Professional.

Ch 10


Introduction to batch files

Batch Files to Run Windows Programs Session Environment

Windows “keeps notes” about itself in system environment.

  • Pathstored in environmental variable %Path%

  • Drive referred to in environment as %SystemDrive%

  • Directory where system files are located referred to as %SystemRoot%

Ch 10


Introduction to batch files

Batch Files to Run Windows Programs Session Environment

With Notepad:

  • Can create batch file that allows you to run program without returning to desktop

  • Can create log file that adds current date and time to file created with Notepad

Ch 10


Activity creating a batch file to run notepad
Activity—Creating a Batch Session EnvironmentFile to Run Notepad

KEY CONCEPTS:

  • %SYSTEMROOT% (Environmental variable)

    • Windows knows where Windows files are located and substitutes correct name

    • %% signs around name - OS substitutes value for that variable.

  • Opened Notepad without returning to desktop

  • Used Notepad to create log file (case sensitive)

  • Created log file

Ch 10


Activity creating a batch file to run notepad1
Activity—Creating a Batch Session EnvironmentFile to Run Notepad

KEY CONCEPTS:

  • START command:

    • Can start a program in new window while still executing batch program in previous window

    • If title following command is in quotes, will give window that title

Ch 10


Special batch file commands table 10 2 batch file commands pp 509 510
Special Batch File Commands Session EnvironmentTable 10.2 Batch File Commands pp.. 509-510

Ch 10


Special batch file commands table 10 2 batch file commands pp 509 5101
Special Batch File Commands Session EnvironmentTable 10.2 Batch File Commands pp.. 509-510

Ch 10


The rem command
The REM Command Session Environment

REM command (remarks)

allows user to place comments

in file that are displayed

but not executed

Ch 10


The rem command1
The REM Command Session Environment

REM allows file to be documented:

  • Gives explanation of the purpose of a program

  • Can include name of batch file, last time it was updated, and author of batch file

Ch 10


Activity using rem
Activity—Using REM Session Environment

KEY CONCEPTS:

  • Used Edit to write batch file

  • Purpose of REM - provide explanatory remarks about the batch file

  • If line begins with REM - explanatory text follows and no action taken

  • No more lines in batch file -OS returns to system level

Ch 10


The echo command
The Session EnvironmentECHO Command

ECHO ON command:

  • Displays command and the command’s output to the screen

  • Is the default value

Ch 10


The echo command1
The Session EnvironmentECHO Command

ECHO OFF command:

  • Minimizes screen clutter

  • User turns off display of command then only see command’s output

Ch 10


The echo command table 10 3 echo on or off p 512
The Session EnvironmentECHO CommandTable 10.3 ECHO ON or OFF p. 512

Ch 10


Activity using echo
Activity—Using ECHO Session Environment

KEY CONCEPTS:

  • ECHO ON - see remarks and commands

  • ECHO OFF - output displayed not actual commands

  • @ECHO OFF - @ suppresses display of ECHO OFF

  • Purpose of batch files defeated if interaction required by user

Ch 10


Activity using echo table 10 4 echo on and echo off a comparison of screen displays p 516
Activity—Using ECHO Session EnvironmentTable 10.4 Echo On and ECHO Off: A Comparison of Screen Displays p. 516

Ch 10


The cls command
The Session EnvironmentCLS Command

CLS command used to clear screen when it becomes full of no longer needed information.

Ch 10


Activity using cls
Activity - Using CLS Session Environment

KEY CONCEPTS:

  • After ECHO turned off screen cleared

Ch 10


The pause command
The Session EnvironmentPAUSE Command

PAUSE command instructs the batch file to stop executing until the user takes some action.

Ch 10


Activity using pause
Activity—Using Pause Session Environment

KEY CONCEPTS:

  • Batch file “paused” - stopped executing until some action is taken

  • Pause is not an order

Ch 10


Stopping a batch file from executing
Stopping a Batch File from Executing Session Environment

To interrupt a batch file during execution:

  • Press <Ctrl> + C

  • Press <Ctrl> + <Break>

Ch 10


Stopping a batch file from executing1
Stopping a Batch File from Executing Session Environment

Return to system prompt

when batch file interrupted.

Ch 10


Activity quitting a batch file
Activity—Quitting a Batch File Session Environment

KEY CONCEPTS:

  • Results of Pressing <Ctrl> + C

  • Because of speed of computer difficult to ascertain how many lines of batch file read when <Ctrl> + C pressed

Ch 10


Introduction to batch files

Replaceable Parameters Session Environment

in Batch Files

Can use fixed or variable parameters with batch files.

Ch 10


Introduction to batch files

Replaceable Parameters Session Environment

in Batch Files

Example of fixed parameter:

  • DIR A: /W

    Examples of variable parameter:

  • TYPE THIS.FIL

  • TYPE TEST.TXT

Ch 10


Replaceable parameters in batch files
Replaceable Parameters Session Environmentin Batch Files

  • Batch files can also use replaceable parameters.

  • Replaceable parameters also called dummy, substitute or positional parameters.

Ch 10


Replaceable parameters in batch files1
Replaceable Parameters Session Environmentin Batch Files

Can parse batch commands:

  • Keying in additional information on command line

  • Dividing computer language into parts that can be made useful to computer

Ch 10


Replaceable parameters in batch files2
Replaceable Parameters Session Environmentin Batch Files

User supplies markers (place holders) to let batch file know that variable will be keyed in with batch file name.

Ch 10


Replaceable parameters in batch files3
Replaceable Parameters Session Environmentin Batch Files

The place holder used in batch files is the percent sign (%), followed by a number (0-9).

Ch 10


Replaceable parameters in batch files4
Replaceable Parameters Session Environmentin Batch Files

  • The % sign is a signal to the operating system that a parameter is coming.

  • The numbers (0-9) indicate what position the parameter is on in the command line.

Ch 10


Activity using replaceable parameters
Activity—Using Replaceable Parameters Session Environment

KEY CONCEPTS:

  • Data still on disk when file deleted

  • To delete data overwrite file with new data

  • Create generic or “plain wrap” batch file

    • Supply specific parameter of file name of interest

    • Supply place (“replaceable parameters”)for name of file

    • View replaceable parameters as positional parameters

      • % sign followed by number

Ch 10


Multiple replaceable parameters in batch files
Multiple Replaceable Parameters in Batch Files Session Environment

Many commands used in

batch files may require

more than one parameter.

Ch 10


Multiple replaceable parameters in batch file
Multiple Replaceable Parameters in Batch File Session Environment

Batch files can have up to 10 replaceable (positional) parameters (%0-%9).

Ch 10


Multiple replaceable parameters in batch files1
Multiple Replaceable Parameters in Batch Files Session Environment

Example:

COPY MYFILE.TXT YOUR.FIL

Ch 10


Activity using multiple replaceable parameters
Activity—Using Multiple Replaceable Parameters Session Environment

KEY CONCEPTS:

  • Within batch files parameters do not have to be in order.

  • Order used on command line

    • %1 - refers to first position after command not first item on command line

Ch 10


Activity using multiple replaceable parameters table 10 5 positional parameters p 529
Activity—Using Multiple Replaceable Parameters Session EnvironmentTable 10.5 Positional Parameters p. 529

Ch 10


Activity using multiple replaceable parameters1
Activity—Using Multiple Replaceable Parameters Session Environment

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

Ch 10


Activity using multiple replaceable parameters2
Activity—Using Multiple Replaceable Parameters Session Environment

NOCOPY CLASS\*.ABC TRIP CLASS

Rest of lines executed in order

  • REM This batch file, NOCOPY.BAT, will hide specified files,

  • REM then copy all other files from one location to another,

Ch 10


Activity using multiple replaceable parameters3
Activity—Using Multiple Replaceable Parameters Session Environment

  • REM then unhide the original files.

  • ATTRIB +H CLASS\*.ABC

  • COPY CLASS\*.* TRIP

  • ATTRIB -H CLASS\*.ABC

Ch 10


Creating useful batch files
Creating Useful Batch Files Session Environment

Can create commands that are not provided with the

operating system.

Ch 10


Activity writing useful batch files
Activity—Writing Useful Batch Files Session Environment

KEY CONCEPTS:

  • Create batch file that will compare file names in directories or on disks

  • Add @ECHO OFF at beginning of batch file - see results of command not REM statements

Ch 10


Activity writing useful batch files1
Activity—Writing Useful Batch Files Session Environment

Examine each part of command line:

DIR /A-D /B /ON %1 > SOURCE.TMP

Ch 10