Document 11475690

advertisement
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
Chapter 11
Advanced Batch Files
LECTURE NOTES
CHAPTER OUTLINE
Chapter Overview




Quick review of batch file commands learned
in earlier chapters.
Advanced features of these commands will be
explained and used.
Will explain the purpose and function of
remaining batch file commands and then will
use these commands to write sophisticated
batch files.
Will refine techniques in working with
environment.
BATCH FILE COMMANDS
 Batch File Commands
 Batch file rules.
 Has .BAT or .CMD as file extension.
 Must be ASCII file.
 Must include legitimate commands.
 Create generic batch files using
replaceable parameters.
 Are not case-sensitive.
 Can use any command in batch file that
can be used on the command line.
 Many special batch file commands.



TEACHING SUGGESTIONS
 Slides 2-5
 Chapter Overview duplicated in PowerPoint
slides.
 After completion of lecture, suggest to
students that they review Objectives and
Outcomes found on first page of chapter as a
check to see if they have mastered concepts.
SECTION 11.1 (pp. 548-549)

Slides 6-10
 Already used ECHO, PAUSE, and REM
 Stress that batch files are similar to
programming (logic) but not as powerful.
May find it useful to compare to macros in
applications.
See PowerPoint slides # for list of commands and
their purposes.
Batch files have a limited vocabulary,
syntax, and programming logic.
Limited in kind of programming they can
do.
 Not as versatile as "real" programming
languages.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 1
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
A REVIEW OF THE REM, PAUSE, AND
ECHO COMMANDS
 A Review of the REM, PAUSE, and ECHO
Commands
 REM.
 Used to document batch files.
 Up to string of 123 characters.
 ECHO on - displays but does not
execute what follows REM.
 Won't display if ECHO is off.
 Placing REM in front of a command
will disable but not delete that specific
line.
 Batch file or CONFIG.SYS file
will continue to execute.
 Disables line without deleting it.
 PAUSE.
 <Ctrl> + C or <Ctrl> + <Break> interrupts
program.
 Temporarily stops executing batch
file.
 Will not continue until user presses
a key.
 Will not do any conditional
processing.
 ECHO.
 Used on command line or in batch file.
 Controls printing of messages on
screen when batch file is run.
 ECHO ON
 (Default) displays all commands to
screen along with output.
 Useful when tracking operation of a
batch file
 Clutters screen when batch file runs
successfully
 ECHO OFF
 Displays only output of commands
to screen.
 ECHO <text string> displays text string
to screen.
 Precede ECHO OFF with @ and
“ECHO OFF” will not appear on the
screen.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Chapter 11
Advanced BATCH Files
SECTION 11.2 (p. 549)

Slides 11-15
 Document = Explaining the purpose a file
serves.
 Conditional processing = Comparing two
items that yields true or false value.
- Program can be directed to take action once
value is determined.
 PAUSE command.
- Stops execution of batch file itself – not
execution of .EXE or .COM program.
- If batch file called an external command &
OS in middle of executing command – will
finish job before exiting batch file.



Discussion Question (1) - What is the function
of the REM, ECHO, and PAUSE commands?
Discussion Question (2) - What happens in a
batch file if ECHO is set to OFF?
Discussion Question (3) - What happens in a
batch file if you precede the ECHO OFF switch
with the @?
Page 2
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
ADVANCED FEATURES OF ECHO AND
SECTION 11.3 (pp. 549-550)
REM
 Slides 16-20
 Advanced Features of ECHO and REM
 Double colon provides faster processing.
 REM slows processing of a batch file.
 Recognized by OS as command and
must be processed.
Discussion Question (4) - What is a NUL
 Replace REM with double colon (::) for
device? Why would you use a NUL device?
faster processing.
Discussion Question (5) - How can you place a
 Label - single colon followed by
blank line in a batch file?
anything.
 Using double colon (::) because
labels skipped by OS.
 With ECHO OFF, messages still come on
screen.
 Redirecting output to NUL device
eliminates standard output messages.
 Will not suppress messages like
“File not found”.
 To generate blank line on screen:
 Use ECHO followed by a period
(ECHO.).
 No space between ECHO and period.
 Using <Enter> will not work in batch
files.


ACTIVITY—USING ECHO AND NUL
 Using ECHO and NUL
 DATA disk in Drive A, A:\> displayed.
 Activity steps.

Use editor to create/save batch file called
ONE.BAT press enter only where indicated
:: This is a test of a batch file using
SECTION 11.4 (pp. 550-551)

 Pressing <Enter> in batch files demonstrates
<Enter>
:: different features. <Enter>
COPY CAROLYN.FIL BOOK.FIL
<Enter>
<Enter>
TYPE BOOK.FIL <Enter>
ECHO <Enter>
DEL BOOK.FIL <Enter>
COPY NO.FIL BOOK.FIL <Enter>
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Slide 21




that getting a blank line is not as obvious as
it seems. Nor can you simply place ECHO in
the batch file - reports status.
Use :: for faster processing.
Redirecting output to NUL device - no
messages shown on screen
Using @ before ECHO OFF = “ECHO OFF”
not shown.
Creating blank line in batch file with ECHO.
Page 3
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems


Close Editor and key in TYPE ONE.BAT
Key in: ONE

Edit/save ONE.BAT so it looks as follows:
@ECHO OFF
:: This is a test of a batch file using
:: different features
COPY CAROLYN.FIL BOOK.FIL >
NUL
ECHO.
TYPE BOOK.FIL
ECHO.
DEL BOOK.FIL
COPY NO.FIL BOOK.FIL > NUL
Key in: ONE

Chapter 11
Advanced BATCH Files
 May want to have a demo disk and write
the batch files prior to class. Are on the
Instructor’s Manual CD.
 Activity completed.
THE GOTO COMMAND
 The GOTO Command
 Use GOTO command to have batch file
constructed to behave like a program.
 Allows looping & branching within batch
files.
 Can stop loop
 Use IF statement or
 Break into batch file - <Ctrl> + C.
 Works in conjunction with a label to create
a loop.
 Processes command following label.
 Label.
 Do not confuse with volume label on a
disk.
 Name chosen to flag or identify the
location in batch file.
 Preceded by colon (:).
 Labels must be unique.
 Maximum length - 8 characters.
 Not a command.
 Batch file goes to label – carries out
command followed on line after label.
 Not case sensitive – but make cases
same.
 Has one parameter – GOTO label.
 Double colon ensures that OS will
disregard line since colon may not be used
as label name.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.5 (pp. 551-552)

Slides 22-25
 Loop = Operation that repeats steps until
loop is stopped.
 Label = Ignored by OS until called with
GOTO command.



Discussion Question (6) - How can you create a
loop in a batch file? How can you stop a loop
from processing in a batch file?
Discussion Question (7) - What is the purpose
and function of the GOTO command?
Discussion Question (8) - What is a label in a
batch file?
Page 4
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
ACTIVITY—USING THE GOTO
COMMAND
 Using the GOTO Command
 DATA disk in Drive A with A:\>
displayed.
 Activity steps.





Use text editor to create/save batch file called
REPEAT.BAT so it looks as follows:
REM This file displays many times the
contents
REM of a file
:REPEAT
TYPE %1
PAUSE
GOTO REPEAT
At system prompt, key in: REPEAT
ASTRO.TXT
Press <Enter> several times to loop through
file.
Press <Ctrl> + C then Y then <Enter>
Chapter 11
Advanced BATCH Files
SECTION 11.6 (pp. 552-554)

 Purpose of EDIT/any editor is to create batch




Using /Q parameter – DEL command does
not require Y or N
SECTION 11.7 (p. 554)



Key in: ECHO a b c d e
Use editor to create/save batch file called
ALPHA.BAT so it looks as follows:
@ ECHO OFF
ECHO %0 %1 %2 %3
SHIFT
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Slide 28
 To execute batch file – key in batch file
name followed by series of values.
OS looks to command line for values
needed to plug into batch file.
Does this based on position of particular
parameters in command line.


ACTIVITY—USING THE SHIFT
COMMAND
 Using the SHIFT Command
 DATA disk in Drive A with A:\>
displayed.
 Activity steps.
file.
Go over each line in Step 1 of activity on
page 552. Analysis of each line included at
the end of this Chapter Outline document.
Debug – see and repair any errors. label
must be preceded by a colon
To execute a batch file, must be at system
prompt - not in editor.
Usefulness of loops.
Delete all files from many floppy disks.
Power Point Slide # shows batch file that will
delete all files from many floppy disks.
Activity completed.
THE SHIFT COMMAND
 The SHIFT Command
 Number of parameters on command line
limited to 10 (%0 - %9).
 0% - represents batch file name itself.
 Really limited to 9 parameters.
 SHIFT moves parameters one position to
the left, decreasing their number by one.
 With SHIFT can have unlimited # of
parameters.
Slides 26-27
Discussion Question (9) - What is the purpose
and function of the SHIFT command?
Discussion Question (10) -Why is it useful to
shift parameters?
SECTION 11.8 (pp. 554-561)

Slides 29-35
 Most of batch files in this chapter are useful
batch files so students can not only see how
the principles work but also see practical
demonstrations of what one can do with
batch files.
- Also show the necessary analytical skills to
debug programs - problem-solving skills.
Page 5
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems














ECHO %0 %1 %2 %3
SHIFT
ECHO %0 %1 %2 %3
SHIFT
ECHO %0 %1 %2 %3
At system prompt key in: ALPHA a b c d e f
Create /save new batch file UPDATE.BAT so
it looks as follows:
:DOIT
COPY %1 /B + > NUL
SHIFT
PAUSE
GOTO DOIT
Key in: DIR APR.99
Key in: DIR APR.BUD
Key in: UPDATE APR.99 APR.BUD
Press <ENTER> twice then <Ctrl>+C then Y
Key in: DIR APR.99
Key in: DIR APR.BUD
Create/save a batch file called SIZE.BAT so it
looks as follows:
:TOP
DIR %1 | FIND "Directory" >> TEMP.FIL
DIR %1 | FIND “bytes” | FIND /V “free” >>
TEMP.FIL
SHIFT
GOTO TOP
TYPE TEMP.FIL
PAUSE
DEL TEMP.FIL
Key in: SIZE CLASS TRIP
Press <Ctrl> + C then Y
Key in: TYPE TEMP.FIL | MORE
Press <Ctrl> + C
Chapter 11
Advanced BATCH Files
 Can keep date log – not dependent on file
modification date.
 SHIFT command.







- Displays 5 + parameters and places echoing
parameters in batch file.
- Moves each parameter over by one
position.
ECHO command = Echoes what is keyed in.
+ sign tells OS to concatenate files.
Contents of file ended when see EOF.
- Typically <Ctrl> + Z.
- Use COPY command – places second
<Ctrl> + Z at end of file.
- This creates problem.
- Solve by copying file in binary mode.
/B switch – tells OS to copy file in binary
mode.
Concatenated files with no switches – files
copied in text mode.
>> used to see both name of directory and
bytes in directory.
<Ctrl> + C used to “break out”.
Activity completed.
THE IF COMMAND
 The IF Command
 Allows conditional processing of parts of
batch files.
 Compares two items - determines if
they are identical, or if one is greater
than the other.
 Comparison yields one of two values.
 True - items are identical
 False - items are not identical.
 Syntax: IF <condition> <command>.
 Condition true – Command executed.
 Condition false:
 Command not executed.
 Batch file falls through to next
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.9 (pp. 561-562)

Slides 36-39
 Conditional processing expressed:
- IF condition TRUE - DO something.
- IF condition FALSE – DO NOTHING.
 Syntax on power point slide #

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT – WXP carries out command if condition false
ERRORLEVEL number – true condition if last
program run returned exist code equal to or
greater than number specified
string1-==string2 – true condition if specified text
Page 6
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
strings match
EXIST filename – true condition if specified
filename exists
command line in batch file.

IF can check for three conditions.
 Whether two sets of characters are or
are not identical.
 Whether or not a file exists.
 Value of variable in ERRORLEVEL
- ERRORLEVEL is a number that a
program can set depending on the
outcome of a process.
THE IF COMMAND USING STRINGS
 The IF Command using Strings
 IF can be used to compare strings.
 Two equal signs (= =) separate what is
to be compared.
 Tell IF statement to GOTO a label or
perform an operation whether the condition
is true or false.
ACTIVITY—USING THE IF COMMAND
WITH STRINGS
 Using the IF Command with Strings
 DATA disk in Drive A with A:\>
displayed.
 Activity steps.



Use editor to create/save batch file called
GREET.BAT so it looks as follows: (No
spaces between two equal signs.)
IF %1==Carolyn GOTO Carolyn
IF %1==Bette GOTO Bette
ECHO Isn't anyone there?
GOTO FINISH
:Carolyn
ECHO Greetings, Ms. Carolyn.
GOTO FINISH
:Bette
ECHO Greetings, Ms. Bette.
:FINISH
Key in:
 GREET Carolyn then GREET Bette
 GREET JUAN then GREET BETTE



Discussion Question (11) - How can you
determine whether or not a file exists?
Discussion Question (12) - What is the purpose
and function of the IF command?
Discussion Question (13) - Give the syntax of
the IF command and explain each part of the
syntax.
SECTION 11.10 (p. 562)

Slides 40-41
 Condition true when strings match.
 Condition false when strings do not match.
SECTION 11.11 (pp. 562-565)

Slide 42
 Do not use ECHO OFF as students cannot
see the results of the tests/commands when
you execute the batch files.
 Note how case matters.
 Add /I parameter immediately following the
IF statement to ignore case.
 Remind students that batch file replaceable
parameters get value from position on
command line.
Activity completed.
TESTING FOR NULL VALUES
 Testing for NULL Values
 Activity 11.11 tested for exact match of
character strings.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.12 (p. 565)

Slides 43-47
Page 7
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
 Discuss results if value is true/not true.
See Section 11.9, p. 597.





If used SHIFT until all parameters used –
end up in endless loop.
Can test to see if string matches.
Can test for a null value = literally testing
for nothing (no data).
 Even though nothing is there, there
must be "something" to confirm the
"nothing."
To test for "nothing" must place a value in
test that will give you nothing.
Variety of methods for testing null values.
 Use quotation marks so that your
statement becomes:






Discussion Question (14) - What does it mean
to test for a null value?
Discussion Question (15) - How can you test for
a null value? Why would you test for a null
value?
IF "%1"= ="" GOTO LABEL
Above means, "If nothing there,
GOTO somewhere else."
Use void: (can use any word)
 IF %1void= = GOTO LABEL
Use backslash (\):
IF \%1\= =\\ GOTO LABEL
ACTIVITY—USING NULL VALUES
 Using NULL Values
 Note: DATA disk in Drive A with A: \>
displayed.
 Activity steps.





Edit and save file called UPDATE.BAT to
look as follows:
:DOIT
IF "%1"=="" GOTO END
COPY %1 /B + > NUL
SHIFT
PAUSE
GOTO DOIT
:END
Key in:
 DIR CAROLYN.FIL
 UPDATE CAROLYN.FIL
Press <Enter>
Key in: DIR CAROLYN.FIL
Edit/save SIZE.BAT file to look as follows:
:TOP
IF %1nothing==nothing GOTO END
DIR %1 | FIND "Directory" >> TEMP.FIL
DIR %1 | FIND "bytes" | FIND /V "free" >>
TEMP.FIL
SHIFT
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.13 (pp. 565-568)

Slide 48
 Testing for a null value can be a difficult
concept for students.
Point out that if you do not test for the end of
the data on the command line, you will create
an endless loop.
 There is also a device named NUL that is
different from a null value.
NUL device literally means nowhere thus, you can send the standard output of
commands (1 file copied) to the NUL device,
which means the message will not be
displayed.
NUL is a device name just as is CON,
PRN, LPT1, etc.
Page 8
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
GOTO TOP

TYPE TEMP.FIL
PAUSE
DEL TEMP.FIL
:END
 Key in:
 DEL TEMP.FIL
 SIZE CLASS TRIP
 TYPE TEMP.FIL
Activity completed.
THE IF EXIST/IF NOT EXIST COMMAND
 The IF EXIST/IF NOT EXIST Command
 Checks for existence/non-existence of a
specified file.
 Using IF EXIST command.
 If file exists
 Condition is true
 Processing passes to specified
GOTO location or to command
following IF statement.
 If file does not exist:
 Condition is false.
 OS ignores command in IF clause.
 Batch process reads next line in
file.
 Using IF NOT EXIST command.
 If file does not exist:
 Condition is true.
 Processing passes to specified
GOTO location or to command
following IF NOT statement.
 If file exists:
 Condition is false.
 Batch process reads next line in
file.
ACTIVITY—USING IF EXIST TO TEST
FOR A FILE
 Using IF EXIST to Test for a File
 Note: DATA disk in Drive A with A: \>
displayed.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.14 (p. 568)

Slides 49-51
 Batch file will process based on the
condition.
 IF EXIST/IF NOT EXIST works only with
file names and not with directory names.

Discussion Question (16) - What is the purpose
and function of the IF EXIST/IF NOT EXIST
command?
SECTION 11.15 (pp. 568-573)

Slides 52-56
Page 9
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems

Activity steps.








Chapter 11
Advanced BATCH Files
Use any text editor to create/save a file called
RENDIR.BAT to look as follows:
IF \%1\==\\ GOTO end
IF NOT \%2\==\\ GOTO next
ECHO You must include a destination name
ECHO for the new directory name.
GOTO end
:next
IF EXIST %1 GOTO message
REN %1 %2
GOTO end
:message
ECHO This is a file, not a directory.
:end
Key in: RENDIR JAN.99 LAST
Key in: RENDIR TEST OLDER
Edit RENDIR.BAT file so IF NOT
EXIST%1\NUL GOTO message replaces IF
EXIST %1GOTO message.
Key in: RENDIR TEST OLDER
Edit/save SIZE.BAT file to look as follows:
IF EXIST TEMP.FIL DEL TEMP.FIL
:TOP
IF %1nothing==nothing GOTO END
IF NOT EXIST %1\NUL GOTO NEXT
DIR %1 | FIND "Directory" >> TEMP.FIL
DIR %1 | FIND "bytes" | FIND /V "free" >>
TEMP.FIL
:NEXT
SHIFT
GOTO TOP
:END
TYPE TEMP.FIL
PAUSE
DEL TEMP.FIL
Key in: SIZE CLASS JUP.PAR TRIP
Press <Enter>
 Table on pages 569-570 analyzes batch file







one line at a time. This same table is
included at the end of this Chapter
Outline document.
Can express/reduce item to a true/false test.
Either it is true, or it is false.
Remind students that each IF is testing for a
true condition.
If statement is true, then command will be
processed.
If condition is false, command ignored and
the next line in the batch file will be
processed.
On the other hand, if the statement is an IF
NOT, you are testing for "truth" in reverse
- if the condition is false, then the test is true
and the command is processed. If the
condition is true, the command will be
ignored and the next line in the batch file will
be processed.
IF EXIST/IF NOT EXIST will test only for
the existence/non-existence of a file.
Can "fool" IF to test for a directory by using
IF %1\NUL. The NUL in the statement will
test for a directory.
- Null device does not exist in every
directory.
- NUL discards anything sent to it.

Activity completed.
IF ERRORLEVEL COMMAND TESTING
 The IF ERRORLEVEL Command Testing
 Program can set exit code when it finishes
executing.
 Batch file tests this exit code with IF
ERRORLEVEL statement.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.16 (p.573)

Slides 56-58
Page 10
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
 Students need to understand that exit codes

must be listed in descending order when
using IF ERRORLEVEL and in ascending
order when using IF NOT ERRORLEVEL.
Exit code doesn’t test for a match with
ERRORLEVEL, but determines if it is
greater than or equal to it.
 Test IF ERRORLEVEL 0 will always
be true – every possible exit code is
greater than or equal to 0.
ACTIVITY—USING IF ERRORLEVEL
WITH COPY
 Using IF ERRORLEVEL with COPY
 DATA disk in Drive A with A:\>
displayed.
 Activity steps.





Discussion Question (17) - Explain the purpose
and function of IF ERRORLEVEL command.
SECTION 11.17 (pp. 574-575)
Slide 59
Use any text editor to create/save a file called
ERROR.BAT to look as follows:
COPY %1 %2
IF ERRORLEVEL 1 GOTO NOTOK
IF ERRORLEVEL 0 GOTO OK
:NOTOK
ECHO There are no %1 files. Try again.
GOTOEND
:OK
ECHO You copied the %1 files
successfully.
:END
Key in: ERROR *.TXT OLDER
Key in: ERROR *.NON OLDER
Activity completed.
WRITING PROGRAMS TO TEST FOR KEY
CODES
 Writing Programs to Test for Key Codes
 Exit codes.
 Set by OS.
 Created by writing small programs that
are based upon some activity.
 Can write program that identifies which
key on a keyboard was pressed and report
which key it was.
 Can do this activity because each time
key is pressed- identified by a 1- or 2digit scan code.
 Two things reported when you press a key
on the keyboard:
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.18 (pp. 575-577)

Slides 60-69
 Scan code = Code computer receives when
key is pressed.
- Code translated by OS and application to
indicate which key was pressed.
 See Appendix G for list of scan codes for
all the keys.
 Script file = Set of instructions that can be
written in any ASCII editor.
- Redirect instructions into program such as
DEBUG to convert script file into executable
Page 11
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems





 First, that a key was pressed.
 Second, that the key was released.
Going to write a program that will report
the scan code for any key that is pressed.
 Once know reported code, can test for a
specific key by using ERRORLEVEL
in the batch file.
 Batch file then acts based upon the
reported code.
 Program must be written in 0's and 1's the "bits and bytes" the computer
understands.
Several ways to write a program.
 Use programming language.
 Use an OS utility program called
DEBUG.
DEBUG.
 Allows testing and debugging of
executable files - those with .COM or
.EXE file extensions.
 Small program that has its own
commands and syntax - you could write
a .COM program directly with
DEBUG.
Unless a student is a programming expert,
they probably don't want to do this.
Easiest way to use DEBUG is to create a
script file.
 Script = set of instructions that you can
write with any ASCII editor.
 Once the script is written, you can
"feed" it to the DEBUG program via
redirection (DEBUG < SCRIPT.FIL).
 DEBUG will then convert the script file
to an executable program with a .COM
file extension.
 This process is the easiest way to create
a file that will report the scan code for
any key that is pressed.
 The program that will be created will
be called REPLY.COM (Activity
11.19).
Chapter 11
Advanced BATCH Files
program.
- Has .COM file extension.
- Simplest way to create file that will report
scan code for any key that is pressed.


Discussion Question (18) - What is a script file?
How can you create one?
Discussion Question (19) -What is a scan code?
ACTIVITY—WRITING A SCRIPT FILE
 Writing a Script File
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 12
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems

DATA disk in Drive A with A:\>
displayed.
Chapter 11
Advanced BATCH Files
SECTION 11.19 (pp. 577-581)

Slide 70
 Script file converted into bytes that make it a
program by redirecting it to DEBUG.

Activity steps.






Use any text editor to create/save a file called
REPLY.SCR to look as follows:
e 100 b4 08 cd 21 3c 00 75 02 cd 21 b4 4c cd
21
rcx
e
n reply.com
w
q
Key in: DEBUG < REPLY.SCR
Key in: DIR REPLY.COM
Use any text editor to create/save a file called
KEYING.BAT to look as follows:
ECHO PRESS F1 TO CLEAR THE
SCREEN.
ECHO PRESS F2 TO DISPLAY THE
DIRECTORY.
ECHO PRESS ANY OTHER KEY TO
EXIT.
REPLY
IF ERRORLEVEL 61 GOTO END
IF ERRORLEVEL 60 GOTO F2
IF ERRORLEVEL 59 GOTO F1
GOTO END
:F1
CLS
GOTO END
:F2
DIR
:END
Key in:
 KEYING then <F1> then KEYING
then <F2>
 May want to create REPLY.COM file for
students and let them copy it or download it.
 Critical when writing script files that it is
exact, i.e., zeros not the letter o, etc.
 Remind students that error codes are tested
for equal to or greater than the value
specified.
 IF ERRORLEVEL - descending order.
IF NOT ERRORLEVEL - ascending order.
Activity completed.
THE ENVIRONMENT
 The Environment
 Area OS sets aside in memory where data
can be stored.
 Acts as a scratch pad where notes kept
about items OS system needs to know.
 OS places/keeps location of file
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.20 (pp. 580-581)

Slides 71-80
 Variable = Value that can change depending
on conditions or on information passed to
program.
Page 13
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems














Chapter 11
Advanced BATCH Files
COMMAND.COM in environment.
 DATA consists of constants or fixed values
Applications can read items in environment
that never change and variable values that do
and can post own messages there.
change.
Programs can get value of variable and use  Expression is any legal combination of
it to modify their operation.
symbols that represents a value.
OS can store data in memory.
 Two strings:
 Name of variable.
 Value of variable.
Environmental variable is name assigned to
string (value) of data.
User can set environmental variables.
Some common variables set when
Windows started.
 USERNAME, USERPROFILE, PATH,
PROMPT.
 Location of CMD.EXE file.
User can also leave messages in the
environment.
User leaves a message by using the SET
command.
Environmental variables set in OS remain
in effect for entire work session.
Environmental variables set in Command
Prompt window or in batch files executed
in the MS-DOS window will remain in
effect only during that command prompt
session.
While values are in effect, use the syntax
%variable%.
SET syntax: SET [variable=[string]]
 Variable - specifies environment variable name.
 String - specifies series of characters to
assign to the variable.
SET without parameters – displays current
environment variables.
SET command followed by a letter – lists
any environmental variables that begin
with that letter.
 Setting environmental values and using them
in batch files is extremely useful.
 Environmental variables commonly used in
all OS’s.
 By using ORIGPATH, even on a network,
students will be returned to network path.
 ADD very useful for adding new
subdirectories to existing path.

Discussion Question (20) - Give the syntax of
the SET command and explain each part of the
syntax.
ACTIVITY—USING SET AND THE
ENVIRONMENTAL VARIABLES
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 14
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems

Using SET and the Environmental Variables
 If environment display is too long to fit on
one screen use MORE filter.
Chapter 11
Advanced BATCH Files
SECTION 11.21 (pp. 581-585)

Slides 81-82
 Command processor must be in memory
when keying in command.

Activity steps.




Key in: SET
If used MORE filter return to prompt or
press Q
Key in:
 SET U then SET S
 ECHO %PATH%
 ECHO %SystemRoot%
 C: then CD %systemroot%
 CD %userprofile% then DIR/AH
 DIR “local settings”\TEMP\~*.tmp
 DEL %TEMP%\~*.tmp
 DIR %TEMP%\~*.tmp
 CD \ then A:
Activity completed.
USING SET AND THE ENVIRONMENT IN
BATCH FILES
 Using SET and the Environment in Batch
Files
 Can use built-in environmental variables
that Windows sets/uses.
 Can set own environmental variables.
 Can give them name/value in batch file
or command line.
 Set variables only good for that session
of Command Prompt Window.
ACTIVITY—USING SET AND THE
ENVIRONMENT IN BATCH FILES
 Using SET and the Environment in Batch Files
 DATA disk in Drive A with A:\>
displayed.
 Activity steps.



Close Command Prompt window
Reopen Command Prompt window and
return to A prompt.
Create/save TESTIT.BAT batch file that looks
as follows:
@ECHO OFF
ECHO %PATH%
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
 Using MORE.
- Pressing <Enter> - moves one line at a time in screen
display.
- Pressing <SpaceBar> - goes to end of file.
 SET with letter of alphabet – all
environmental variables beginning with letter
displayed.
 See value of environmental variable – ECHO
and enclose environmental variable name
seeking with percent signs.
 Environmental variable can be used with
commands.
 Use environmental variable to change
directories (Shortcut).
SECTION 11.22 (p. 585)

Slides 83-84
 Exit command prompt set values no longer
available next time command prompt
opened.
SECTION 11.23 (pp. 585-590)

Slide 85
 Can set environmental value and use it in a
batch file.
 Environmental variables set – deleted when
Command Prompt window is closed.
 Environmental variables – not case sensitive.
 To eliminate value – must set it at nothing.
Page 15
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems









ECHO.
Key in:
 TESTIT
 SET TODAY=C:\WUGXP\*.FP
 SET
Create/save SETTING.BAT batch file that
looks as follows:
DIR %today%
ECHO %TODAY%
Chapter 11
Advanced BATCH Files
 Adding directory to PATH statement.
Key in:
 SETTING
 SET today=C:\WUGXP\.TMP
 SETTING then SET TODAY=
 SET T then SET TODAY=MONDAY
 ECHO %TODAY% then SET T
Close Command Prompt window
Reopen Command Prompt window and
return to A prompt.
Key in: SET T
Create/save ADD.BAT batch file that looks as
follows:
IF "%1"=="" GOTO END
PATH > OLDPATH.BAT
:TOP
PATH %PATH%;%1
SHIFT
IF NOT \%1\==\\ GOTO TOP
:END
Key in:
 PATH > ORIGPATH.BAT
 ADD A:\ then ORIGPATH
 ADD A:\;A:\OLDER;A:\CLASS
 PATH then ORIGPATH
Activity completed.
THE DIRCMD ENVIRONMENTAL
VARIABLE

The DIRCMD Environmental Variable
 Can place/use variables in environment.
 DIRCMD environmental variable.
 With SET to preset DIR command
parameters/switches.
 With ERRORLEVEL to write batch
files to change way DIR displays
information for current MS-DOS work
session.
ACTIVITY—USING DIRCMD
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.24 (p. 590)

Slide 86
 Environment – area set-aside in memory.
 Keying in SET (alone) – displays what is in
environment.

Discussion Question (21) - Explain the purpose
and function of the DIRCMD environmental
variable.
Page 16
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems

Using DIRCMD
 DATA disk in Drive A with A:\>
displayed.
Chapter 11
Advanced BATCH Files
SECTION 11.25 (pp590-593)

Slide 87
 Using DIRCMD is an easy way to set
preferences for the default values of DIR.
Can eliminate the need for using
parameters.
Even easier than a batch file.

Activity steps.



Create/save batch file MY.BAT
@ECHO OFF
CLS
ECHO.
ECHO.
ECHO How do you want your directory
displayed?
ECHO.
ECHO 1. Files only arranged by file name.
A to Z
ECHO 2. Files only arranged by file name.
Z to A.
ECHO 3. Files only arranged by file
extension.
A to Z
ECHO 4. Files only arranged by file
extension.
Z to A
ECHO 5. Directory displays in default
mode.
ECHO.
ECHO PLEASE SELECT A NUMBER.
ECHO.
REPLY
ECHO.
 Stress the IF ERRORLEVEL/IF NOT
ERRORLEVEL to set specific range and
then execute command preference.
IF ERRORLEVEL 49 IF NOT ERRORLEVEL
50 SET DIRCMD=/ON/A-D
IF ERRORLEVEL 50 IF NOT ERRORLEVEL
51 SET DIRCMD=/O-N/A-D
IF ERRORLEVEL 51IF NOT ERRORLEVEL
52 SET DIRCMD=/OE/A-D
IF ERRORLEVEL 52 IF NOT ERRORLEVEL
53 SET DIRCMD=/OE/A-D
IF ERRORLEVE 53 IF NOT ERRORLEVEL 54
SET DIRCMD=
Key in:
 MY then 4 then SET D
 DIR CLASS then MY then 5
 SET D
Activity completed.
THE FOR…IN…DO COMMAND
 The FOR…IN…DO Command
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.26 (pp. 593-594)

Slides 88-93
Page 17
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems





Chapter 11
Advanced BATCH Files
Can be issued at command line or placed in  Difference between a variable and a
batch file.
parameter.
Allows repetitive processing within batch
 VARIABLE.
files.
- FOR statement – tells OS to get value from
FOR allows you to use a single command
set chosen.
to issue several commands at once.
- After executes command appearing after
DO, FOR looks for next value in set.
The command can DO something FOR
- - Finds another value, %% represents
every value in a specified set.
something new – command executed with
new value.
FOR…IN…DO syntax (at command line):
FOR %variable% IN (set) DO command
[command-parameters]





%variable - specifies a replaceable
parameter.
 (set) - specifies a set of one or more
files. Wildcards may be used.
 Command - specifies the command
to carry out for each file.
 Command-parameters - specifies
parameters or switches for the
specified command.
FOR…IN…DO syntax (batch program):
 Use %%variable instead of
%variable.
 Batch file variable - arbitrary single
letter.
 Double percent sign with letter
(%%a) distinguishes batch file
variable from replaceable variable
(%1).
Allows the user to loop through a file
for predefined list of values.
OS gets values from position in
command line.
The set enclosed in parentheses.
 Values in set used to DO some
command.
 Items in set separated by spaces or
commas.
 Wildcards may be used.
- - No more values in set – FOR stops processing
 PARAMETER.
- Set before batch file begins processing.
 GOTO loop as a vertical loop and
FOR…IN…DO as a horizontal loop.




Discussion Question (22) - What is the purpose
and function of the FOR…IN…DO command?
Discussion Question (23) - Name two
parameters you can use with the FOR…IN…DO
command.
Discussion Question (24) - Describe the purpose
of those parameters.
Discussion Question (25) - Give the syntax of
the FOR..IN..DO command and explain each
part of the syntax.
ACTIVITY—USING THE FOR…IN…DO
COMMAND
 Activity: Using FOR...IN...DO Command.
 DATA disk in Drive A and A:\>
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 18
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems

displayed.
Note 2: Look at command line going to
use in Step 1. In English, command says:
Using the variable %a to hold each value in
the set (what is in the parentheses), do the
command (Type) to each value in the set
(%a).
Chapter 11
Advanced BATCH Files
SECTION 11.27 (pp. 594-601)

Slides 94-95
 Space and comma between items in a set
work same way.
 Variable letter chosen not important
 Command line is case sensitive.
 Stress the following items.
- % is used at the command line.
- %% must be used in a batch file.

Activity steps.
 Key in: FOR %a IN (*.99) DO TYPE %a
 Key in: FOR %x IN (APR99 NOFILE.EXT
D.BAT) DO TYPE %x
 Key in: FOR %y IN
(APR.99,NOFILE.EXT,D.BAT) DO TYPE %y
 Key in: TYPE *.99 NOFILE.EXT
 Key in: FOR %a IN (*.99,NOFILE) DO IF
EXIST %a TYPE %a
 Create/save batch file DO.BAT that looks as
follows:
FOR %%v IN (Patty Nicki Sandy Brian)
DO ECHO %%v
 Key in: DO
 Create/save a batch file called PASS.BAT that
looks as follows:
FOR %%a IN (%USERS%) DO IF
"%1"=="%%a" GOTO OKAY
:NO
ECHO You, %1, are NOT allowed in the
system.
GOTO END
:OKAY
ECHO Welcome %1 to my world of
computers.
:END
 Key in: SET USERS=Carolyn, Bette
 Key in: PASS Bette
 Key in: PASS Denzel
 Edit/save UPDATE.BAT to look as follows:
:DOIT
IF "%1"=="" GOTO END
FOR %%v IN (%1) DO COPY %%v /b + >
NUL
SHIFT
PAUSE
GOTO DOIT
:END
 Key in: MD BATCH
 Key in: ADD A:\BATCH
 Key in: MOVE *.BAT BATCH
 Key in: MOVE REPLY*.* BATCH
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
 Advantage of FOR…IN…DO - can set




values in environment and then use them in a
batch file.
Stress that the %x can be any value, i.e., %j,
%y, etc. Can demonstrate with several letters
to show that it is an arbitrary assignment.
Explain that this is a horizontal loop process all items in the (set) rather than a
vertical loop as is the GOTO/label.
Point out that environmental variables can be
used with FOR…IN…DO.
Caution: If closed Command Prompt
window, have to issue following command to
include the A:\BATCH directory in your
path: A:\BATCH>A:\BATCH\ADD
A:\BATCH
Page 19
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files





Key in: DIR *.SWT
Key in: DIR *.CAP
Key in: UPDATE *.SWT *.CAP
Press <Enter> until returned to prompt
 Key in: DIR *.SWT *.CAP
Activity completed.
MORE FEATURES OF THE FOR…IN…DO
COMMAND
 More Features of the FOR…IN…DO
Command
 May list environmental variables so they
are divided and appear on separate lines.
 Use /R parameter (recursive).
 Command will search and perform
actions on all subdirectories beneath it.
 Use tilde operator (~) to:
 Strip a file name of quotation marks.
 Expand a variable.
 Select specific text from ASCII files.
ACTIVITY—USING THE ADDITIONAL
FEATURES OF THE FOR…IN…DO
COMMAND
 Using Additional Features of the
FOR…IN…DO Command
 Note: DATA disk in Drive A with A: \>
displayed.
 Activity steps.
 Key in:






SET PATHEXT
FOR %a IN (%pathext%) DO @
ECHO
%a
FOR %a IN (“Sandy and Patty.txt”,
MERCUYRY.TXT) DO
@ECHO %a
FOR %a IN (“Sandy and Patty.txt”,
MERCURY.TXT) DO
@ECHO %~a
FOR %a IN (“Sandy and Patty.txt”,
MERCURY.TXT) DO @REN
%a “CZG %a”
FOR %a IN (“Sandy and Patty.txt”,
MERCURY.TXT) DO
@ECHO %~a
 DIR CZG*.*
 DEL CZG*.*
 Key in following and then press <Enter>: FOR
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
SECTION 11.28 (p. 602)

Slides 96-97
SECTION 11.29 (pp. 602-611)

Slides 98-99
 In batch file, variable names need to be





preceded by two percent signs.
Use FOR…IN…DO to display one line at a
time.
Tilde strips file name of quotation marks.
Use quotation marks to add prefix or suffix
to long file name.
REN only needs file name.
- Options preceded by ~ permits this.
- n – forces variable to expand to only file
name.
- x – forces expansion of file extension.
Can strip out specific fields in a text file.
Page 20
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
%a IN (“Sandy and Patty.txt”, JUPITER.TXT)
DO REN %a “CZG %~a”
Chapter 11
Advanced BATCH Files
- /F parameter sets rules by which you extract
data.
 Can delimit data.
- Each field set off with a semicolon (could
have used any character).
 Key in:
 COPY C:\UGXP\*.TXT TRIP
 FOR /R %a IN (SANDY*.*) DO
@ECHO %a
 FOR /R %a IN (SANDY*.*) DO @ECHO
%~nxa
 In batch file directory create PREFIX.BAT batch
file. Use own name instead of “YourNameHere”
@ECHO OFF
REM YourNameHere
REM Purpose of batch file is to add a new prefix
to any file name.
IF”1”==””GOTOMESSAGE
IF”2”==””GOTOMESSAGE2
FOR /R %%a IN (%2) DO REN “%%~a” “%1
%%~nxa”
GOTOEND
:MESSAGE
ECHO You must include a prefix you wish to
use.
ECHO Syntax is PREFIX prefix filename
GOTOEND
:MESSAGE2
ECHO You must include a file name you wish to
rename.
ECHO Syntax is PREFIX prefix filename
GOTOEND
:MESSAGE2
ECHO You must include a file name you wish to
rename.
ECHO Syntax is PREFIX prefix filename
:END
 Be sure BATCH directory is in your path. Can
add ADD.BAT to include it.
 In root of A:\ key in: DIR Sandy*.* /S
 Key in:
 PREFIX RBP SANDY*
 DIR *SANDY* /S then DIR RBP* /S
 Create PERSON.BAT batch file in BATCH
directory that looks as follows:
@ECHO OFF
FOR /F “TOKENS=1,2,7” %%a IN (%1) DO
ECHO %%b %%a %%c
 Key in:
 CD \ then TYPE PERSONAL.FIL
 PERSON PERSONAL.FIL
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 21
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
 Edit PERSON.BAT file as follows:
@ECHO OFF
SORT < %1 > %2
FOR /F “TOKENS=1,2,7” %%a IN (%2) DO
ECHO %%b %%a, %%c
DEL %2


 Key in: PERSON PERSONAL.FIL TEMP.FIL
 Edit PERSONAL.FIL and select the first five
lines and copy them into a new file called
SHORT.FIL
 Exit Edit and key in: PERSON SHORT.FIL
TEMP.FIL
Edit SHORT.FIL file as follows:
Gillay;Carolyn;699
Lemon;Orange;CA;Professor
Panezich;Frank;689 Lake;Orange;CA;Teacher
Tuttle;Steven;356
Embassy;Mission
Viejo;CA;Juggler
Maurdeff;Kathryn;550
Traver;Ann Arbor;MI;Teacher
Maurdeff;Sonia;550
Traver;Ann Arbor;MI;Student
 Close SHORT.FIL Edit PERSON.BAT to look
as follows:
@ECHO OFF
SORT < %1 > %2
FOR /F ”usebackq delilms=;TOKENS=1,2,6”
%%a IN (%2) DO ECHO %%b %%a,
%%c
DEL %2
 Key in: PERSON SHORT.FIL TEMP.FIL
Activity completed.
THE CALL COMMAND
SECTION 11.30 (pp. 611-612)
 The CALL Command
 Slide 100
 CALL allows you to run one batch file
from within another.
 Second batch file is finished executing –
Discussion Question (26) - Explain the purpose
and function of the CALL command.
returns control to first batch file.
 Without CALL, the original batch file
will not continue after the second one is
SECTION 11.31 (pp. 612-619)
run.

ACTIVITY—USING CALL
 Using CALL
 DATA disk in Drive A with A:\>
displayed.
 You have executed command
A:\BATCH\>ADD A:\BATCH at some time
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1

Slides 101-102
 When executing a program within a batch
file, you are not returned to batch file unless
you use CALL.
 Breakdown of the HOME.DAT batch file
on page 617
Page 22
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
during current Command Prompt session.

Activity steps.
 Key in:
 CD BATCH
 COPY CON BELL.BAT
 ECHO <Ctrl> + G <Enter> 6 times
 <F6>
 TYPE BELL.BAT
 BELL
 Create/save a batch file called BELLING.BAT in
BATCH directory that looks as follows:
COPY \*.ABC *.XYZ
DEL *.XYZ
BELL
 Key in: BELLING
 Edit/save BELLING.BAT that looks as follows:
COPY \*.ABC *.XYZ
BELL
REM You are about to delete the *.XYZ files.
Are you sure?
PAUSE
DEL *.XYZ
 Key in: BELLING then DIR *.XYZ
 Edit/save BELLING.BAT that looks as follows:
COPY \*.ABC *.XYZ
CALL BELL
REM You are about to delete the *.XYZ files.
Are you sure?
PAUSE
DEL *.XYZ
 Key in: BELLING
 Press <Enter>
 Key in: DIR *.XYZ
 Use COPY CON to create/save a batch file
called HOME.DAT that looks as follows:
COPY CON HOME.DAT
SET HOME=<F6>
 Create/save HOMETO.BAT batch file in batch
directory that looks as follows:
COPY A:\BATCH\HOME.DAT
A:\BATCH\HOMESAVE.BAT
CD >> A:\BATCH\HOMESAVE.BAT
CALL HOMESAVE.BAT
DEL A:\BATCH\HOMESAVE.BAT
 Create/save a batch file HOME.BAT that
contains the following:
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Chapter 11
Advanced BATCH Files
Line 1 – Copies contents of data file to batch
file. Now HOME>DAT contains SET
HOME=. HOMESAVE.BAT not contains
SETHOME
Line 2 – takes whatever directory you are in
and appends it to HOMESAVE.BAT. If
your current directory is
BATCH.HOMESAVE.BAT now has the
contents of SET HOME=A:\BATCH
Line 3 – executes batch file.
HOMESAVE.BAYT now executes and sets
the variable of HOME to A:\BATCH
Line 4 – Deletes batch file. Now that
environmental variable is set, no longer need
batch file HOMESAVE.BAT
Page 23
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
CD %HOME%

 Key in:
 PATH then SET H then CD \
 CD WORK\ADS then HOMETO
 SET H then CD \MEDIA\TV
 HOME then CD \ then ORIGPATH
Activity completed.
Activity 11.6 - Using the GOTO Command
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
- REM This file displays many times the contents
- REM of a file
- :REPEAT
- TYPE %1
- PAUSE
-GOTO REPEAT
First two lines are remarks that will not execute.
Not including ECHO OFF because you want to see what is happening in batch file.
Omitting ECHOOFF is way to “debut a batch program
Third line : REPEAT) is a label – must be preceded by a colon
Fourth line simple TYPE Command with replaceable parameter
Fifth line - Pause command is placed here so you may see what is happening.
Sixth and last line is the loop.
The GOTO tells batch file to return to label. (:REPEAT)
It will then return to line 4 and execute the TYPE Com and
It will then read lines 5 and 6 and continually repeat the process.
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 24
Instructor: Prof. Michael P. Harris
ITSC 1405 – Intro to PC Operating Systems
Chapter 11
Advanced BATCH Files
Activity 11.15: Using IF EXIST to Test for a FILE
Batch File by Line Number
Test TRUE
Processing
Test FALSE
1. IF \%1\= =\\ GOTO end
User keys in
nothing for %1.
Since test is
true, action is
to go to line 12
Testing for
null value.
User keys in
value for %1.
Since test is
false, action is
to go to line 2.
2. IF NOT \%2\= =\\GOTO next
User keys in
nothing for %2.
Since test is
true, action is
to go to line 3.
Testing for
null value
User keys in
value for %2.
Since test is
false, action is
to go to line 6.
3. ECHO You must include a
destination name
Message for
user that he
or she did not
include a value.
4. ECHO for the new
directory name.
Continuation
of the message.
5. GOTO end
Falls through to
the GOTO end
statement. Action
is to go to line 12.
6. :next
Label referred
to in line 2.
7. IF EXIST %1 GOTO message
User keys in
file name for
%1. Since test
is true, action is
to go to line 10.
Testing for
value for %1.
Is it a file or
a directory?
8. REN %1 %2
Since %1 test is
false (not a file),
renaming directory
can proceed.
9. GOTO end
After directory
is renamed, falls
through to GOTO end.
10. :message
Label referred
to in line 2.
11. ECHO This is a file, not a
directory.
Message that
user used a file
name, not a
directory name.
User keys in
directory for
%1. Since test
is false, action
is to go to line 8.
12. :end
Carolyn Z. Gillay, Bette A. Peat, Windows XP Command Line
Franklin, Beedle & Associates ©2003 ISBN: 1-887902-82-1
Page 25
Download