Learning Objectives ECHO commands. command. 10. Explain the

SA
Learning Objectives
After completing this chapter you will be able to:
1. List commands used in batch files. .
2. List and explain batch file rules.
3. Explore the function of the REM,
ECHO commands.
4. Explain the use of batch files with shortcuts.
5. Explain the purpose and function of the
GOTO command.
6. Explain the purpose and function of the
SHIFT command.
7. Explain the purpose and function of the IF
command.
8. Explain the purpose and function of the IF
EXIST /IF
9. Explain the purpose and function of the IF
ERRORLEVEL command.
10. Explain the purpose and function of writing
programs.
11. Explain the purpose and function of the
environment and environmental variables.
12. Explain the use of the SET command.
13. Explain the purpose and function of the
FOR... IN... OO command.
14. Explain the purpose and function of the
CALL command.
Student Outcomes
1. Use the ECHO command to place a blank
line in a batch file.
2. Use the GOlO command in conjunction with
a label to create a loop.
3. Use a batch file with a shortcut.
4. Use the SHIFT command to move param-
eters.
5. Use the IF command with strings for conditional processing.
6. Test for null values in a batch file.
7. Use the IF EXIST /IF
test for the existence of a file or a
subdirectory.
8. Use the SET command.
9. Use the environment and environmental
variables in batch files.
10. Use the IF ERRORLEVEL command
XCOpy to write a batch file for testing exit
codes.
11. Use the FOR... IN... OO command for repetitive processing.
12. Use the CALL command in a batch file.
Chapter Overview
You learned in Chapter 10 how to write simple
batch files and use replaceable parameters. Some
commands allow you write even more powerful
batch files that act like sophisticated programs.
This chapter focuses on the remaining batch
file commands, which will allow you to write
sophisticated batch files. You
your techniques in working with the environment.
547
", .
- ~.,
~~,
548
CHAPTER 11
ADVANCED BATCH FILES
;~~::::?0:mt":::%:':~:i:~,,m;8';1:::
III)
Batch File Commands
A quick summary of batch file rules tells us that any batch file must have the file
extension of . BAT or . CMD, it must always be an ASCII file, and it must include
legitimate commands. In addition, you can use replaceable or positional parameters
to create generic batch files. Batch file commands are not case sensitive. You may use
any command in a batch file that you can use on the command line , as well as some
specific batch file commands. See Table
Command
urp ose
Calls one batch program from another without causing the first
batch program to stop. Beginning with Windows
CALL
sional, CALL accepts labels as the target of the call.
ECHO
Displays or hides the text in batch programs while the program is
running. Also used to determine whether commands will be
echoed" to the screen while the program file is running.
ENDLOCAL
Ends localization of environment changes in a batch file, restoring
environment variables to their values before the matching
SETLOCAL command. There is an
of the batch file.
FOR
Runs a specified command for each file in a set of files. This
command can also be used at the command line.
GOTO
Directs the operating system to a new line that you specify with a
label.
Performs conditional processing in a batch program, based on
whether or not a specified condition is true or false.
PAUSE
Suspends processing of a batch file and displays a message
prompting the user to press a key to continue.
REM
Used to document your batch files. The operating system ignores
any line that begins with REM, allowing you to place lines of
information in your batch program or to prevent a line from
running.
SETLOCAL
Begins localization of environmental variables in a batch file.
Localization lasts until a matching ENDLOCAL command is
encountered or the end of the batch file is reached.
SHIFT
Changes the position of the replaceable parameter in a batch
program.
Table
11.
Batch File Commands
You have already used ECHO, PAUSE, and REM. You will now
ing batch file commands, which allow you to create complex batch files. Using these
commands is similar to using a programming language. Batch
vocabulary (the commands listed above), a syntax (punctuation and grammar
rules), and a programming logic. Batch files are also limited
gramming they can do. They do not have the power or the flexibility
real"
,;
11. 3
ADVANCED
549
programming language such as Visual
plish many things in the Windows environment.
, accom-
~~,~~;N~~~~t.:;::
t:,::,':::.:i:=;:',
A Review of the
and
The REM command in a batch file indicates to the operating system that whatever
text follows is to be displayed, but only if ECHO has
not
been turned off. If a
command follows REM, it will be displayed but not executed. Remarks can be a
string of up to 123 characters , and typically they document batch files. Placing REM
in front of a command will allow you to execute a batch file or the CONFIG. SYS file
without executing the command that follows it. This allows you to disable a line or
lines without having to actually delete them.
The PAUSE command stops a batch file from
press any key. It tells you to press any key to continue, but does not do any
conditional processing.
Remember, to interrupt a batch file, you can always press
IBreakl. Pressing this key combination will interrupt
ICtr11
+ C or lCtrl1
There is one warning-if the batch file has called an external command and the
operating system is in the middle of executing a command such as
DISKCOPY, it will finish
C stops the execution of the batch file
ICtrll
itself-it will not stop the execution of a . EXE
or . COM program.
The ECHO command can be used either on a command line or in a batch file. It is
a special command that turns on or turns off the echoing of commands to the screen.
If you key in the command ECHO by itself on the command line or include it in a
batch file, it will return the status of ECHO: either ECHO on or ECHO
ECHO is on , all the commands in a batch file are displayed on the screen. When
ECHO is off, you see the output of the command, but not the command itself.
Normally ECHO is on, which is particularly useful when you want to track the
operation of a batch file. However, when a batch file runs successfully, the display of
commands can clutter the screen.
In a batch file , if you do not wish to see each command 9n the screen, you can
issue the command ECHO OFF. The batch file commands are not
messages that a command such as COpy issues will be displayed, for example,
1 file(s) copied. " Depending on your preferences, you can key in ECHO ON or
ECHO OFF within the batch file to display or not display the
tion, if you precede ECHO OFF with ~, the
OFF, will not appear on the screen.
Advanced Features of
There are some interesting features and variations you can implement with both
REM and ECHO. One problem with REM is that the operciting system recognizes it
as a command and must take time to process it. A shortcut is to use a double colon
(::) instead of REM in front of a remark, or documentation line. This will save
~t
valuable processing time because the operating system treats all lines beginning
550
CHAPTER 11
ADVANCED BATCH FILES
with a colon as a label and ignores them unless they are " called" elsewhere in the
batch file. This will be further explained later.
of messages such as " 1 file(s)
When you turn ECHO off, you still get the display
see
messages.
You can use redirection with
you
to
copied. " Sometimes
do not wish
standard output to redirect the output
of
a command
to
a device called NUL. As the
name implies, NUL me~s send to nothing. " When you send the output to NUL,
it goes nowhere, and it is not displayed on the screen.
Although redirecting the output of a command to the NUL
messages such as " 1 file(s) copied " it will not suppress a message that is generated
by the operating system like " File not found.
You often want to place a blank line in a batch file for aesthetic purposes or
highlight particular commands. There is,
of
course, no such thing as a blank line. In
Enter I
key,
the word-processing world, when you want a blank line, you press the
which places a carriage return in the document and prints as a blank line. This does
not work in batch files. In a batch file, the operating system simply ignores it when
you
you
use REM,
Enter Pressing
Enter I does not leave a blank line. If
you
press
you
place the word ECHO in the batch file, it will
will see nothing if ECHO is off. If
to key in
to get a blank line is
report whether ECHO is on or off. An easy method
ECHO followed by a period. There can be no space between ECHO and the period.
~::~x.r::~~,,~,~~:~~~*~.
Activity:
The DATA disk is in Drive A. Open a Command Prompt window. A:
Note:
displayed.
Use an editor to create and save the following batch file called ONE. BAT,
pressing the
IEnterl key only where indiCated.
:: This is a test of a batch file
IEnterl
:: different features. IEnterl
COpy CAROLYN. FIL BOOK. FIL IEnterl
Enter I
TYPE BOOK. FIL IEnterl
IEnterl
DEL BOOK. FIL IEnterl
COpy NO. FIL BOOK. FIL IEnterl
ECHO
Close the editor and then key in the following:
A:\::-TYPE ONE. BAT
of
a batch file using
:: This is a test
:: different features.
COpy CAROLYN. FIL BOOK. FIL
TYPE BOOK. FIL
ECHO
DEL BOOK. FIL
COpy NO. FIL BOOK. FIL
A:\::-
"i
I!: '
BAT
IEnterl
::
:::;::;~~,~~
,~'~::~::,,;~,::;~,~
11. 5 THE
Key in the following: A: \::.ONE IEnterl
A: \::oONE
A: \ ::oCOPY CAROLYN.
1 file (s) copied.
A: \::oTYPE BOOK. FIL
Hi, my name is Carolyn.
What is your name?
A:\::oECHO
ECHO is on.
A:\::oDEL BOOK.
FIL
A: \::oCOPY NO. FIL
The system cannot find the file
A: \::0
WHAT'S
lli.$'fl
Edit and save
~ECHO
You see messages as well as the output.
OFF
ONE. BAT
so it looks as follows:
This is a test of a batch file using
different features.
COpy CAROL YN. FIL BOOK. FIL
ECHO.
TYPE BOOK. FIL
ECHO.
DEL BOOK. FIL
COpy NO. FIL BOOK. FIL
Key in the following: A: \::.ONE IEnterl
Hi, my name is Carolyn.
What is your name?
A: \::0
WHAT'S
1iffi5'E
blank lines. The COpy command could not find NO. FIL and the error message was
redirected to the NUL device and was not displayed to the user on the screen.
::,::~::::~~~~w:
lID
The GOTO Command
By using the GOTO command , a batch file can be constructed to behave like a
program written in a programming language such as
command will branch to a specific part within a batch file. By using the GOTO
loop
is an operation that will repeat steps until
statement, you can create a loop. A
551
~.
552
CHAPTER 11
you stop the loop either by using an IF statement or by breaking into the batch file
with
; I;,
f:)
ICtr11 + C.
The GOTO command works in conjunction with a label. This label
confused with a volume label on a disk. A label is any name you choose to flag a
particular line , or location in a batch file. A label is preceded by a colon (:) and is
ignored by the operating system until called with the
colon (::), used earlier for REM statements , ensures that the operating system will
always disregard the line, even in conjunction with the
colon may not be used as a label name. A label can be no longer than eight characters. The label itself is not a command, it just identifies a location in a batch file.
When a batch file goes to a label, it carries out whatever command follows on the
line after the label.
label.
Although it is not
necessary that labels be exactly the same (i. , the same case) it is still wise to make
them the same case.
:;:::;t;:'Wl~:::::;:,8%::;:::-::::x;:;:,::t's~~~~
Activity: Using the
Note:
The DATA disk should be in Drive
Use any text editorto create and save a batch
BAT. Key in
the following using exactly the same
REM This file displays. many times
REM of a file. IEnterl
: REPEAT
IEnterl
IEnterl
TYPE %1 lEnterl
PAUSE
IEnterl
GOTO REPEAT
WHAT'
",:"ii"'''?''' ?'Yii
fflit'IiWj HAPPENi'N
G1
that will not execute. You are not including ECHO
what is happening in your batch file. Omitting ECHO OFF
debug " a
means to see and repair any errors. The third line
(:REPEAT) is a label, which must be preceded by a colon. The fourth line is a simple
TYPE command with a replaceable parameter, The
the fifth line so you may see what is happening. The sixth and last line is the loop.
The GOTO tells the batch file to return to the label
line 4 and execute the TYPE command. It will then
ally repeat the process.
batch file program.
Debug
You must be at the system prompt, not in the editor. Key in the following:
A: \::-REPEAT ASTRO. TXT IEnterl
A:\~REPEAT ASTRO. TXT
A: \~REM This
A:\~REM of a
A: \ ~TYPE
uu.
11.
ACTIVITY: USING THE GOTO COMMAND
The study of Astronomy came from Astrology.
Most scientists no longer believe in
Astrology. The
changing every day.
A: \=-PAUSE
Press any key to continue
WHAT'S
NUi",.
_HAPPENING? The first two lines displayed are remarks. The
but the TYPE command was executed. The next line has paused the batch file. You
IEnterl to continue.
need to press
Enter I
Press
A: \=-PAUSE
Press any key to continue
A: \=-GaTa
A: \=-TYPE ASTRa. TXT
The study of Astronomy came from Astrology.
Most scientists no longer believe in
Astrology. The
changing every day.
A: \
Press any key to continue
WHAT' S HVUNiN
iW1iiIi
was sent back to the label
which was the TYPE command, then the next line , which was PAUSE. When you
press a key, you will again be returned to the label. Now you see a loop in action.
IEnterl a few times to see the loop in action. Then press
ICtr11 + C to break
out of the batch file. It will ask you if you want to terminate the batch job. Key in
V, then
IEnterl for yes. You will be returned to the A:\;:. prompt.
Press
WHAT'S PV'f'Hf"'V
ffiit1Im HAPP'E'i;JING7 A loop can be very useful. For instance ,
if you wanted to delete all
the files from many floppy disks , you could write a batch file that would look like
this:
~ECHO OFF
:TOP
CLS
ECHO Place the disk with the files you no longer want in
ECHO Drive A.
PAUSE
DEL
IQ
ECHO Press Ctrl + C to stop executing this batch file.
ECHO Otherwise, press any key to continue deleting files.
PAUSE;:. NUL
GOTO TOP
553
554
CHAPTER 11 . ADVANCED BATCH
The /Q parameter makes it so the DEL command
!fi:
did not want to see the output of the
NUL device. Remember, a file like the above which deals with deletion of multiple
files can be both convenient and dangerous.
DIJ
The SHIFT Command
When you have written a batch file with positional parameters, you key in the batch
file name followed by a series of values. When the batch file is executed, the operating system looks to the command line for the values it needs to plug into the batch
file. It does this based on the position of particular parameters in the command line.
In earlier versions of Wmdows, the TYPE command was limited to one parameter.
Thus , if you wanted to use replaceable parameters, you could create a batch file
called LIST. BAT
whose contents would be:
TYPE %1
. TYPE %2
TYPE %3
Then with this batch file , called LIST. BAT, you would key in
line:
LIST APPIL.TXT MAY.TXT JUNE. TXT
With this generic batch file, you could key in only three file names. If you wanted
more file names , you would have to re-execute the batch file.
parameters on a command line-%O through
batch file name itself, you can have only nine parameters. The SHIFT command
allows you to shift the parameters to the left, one by one , making the number of
parameters on a line limitless. As the SHIFT command shifts the contents of the
parameters to the left, parameter 2 becomes parameter 1 , parameter 3 becomes
and so on. This allows the batch file to process all the parameters on
parameter 2,
the command line.
~:::=X%~?::i~::m::::::~:%t;~M;:;;%:
lID
Activity:
Note:
The DATA disk should be in Drive A
Keyin the following: A: \;:.ECHO a
IEnterl
A:\;:.ECHO a
a bcd
A:\;:.
WHA T' SDWH*%;!W
iJlMHAPPENiNG7 ECHO on a
echoed" what you keyed in. Thus,
the parameters a, b , c, and d on the command line were repeated on the screen. If
you wanted to display more than five parameters and place the echoing parameters
in a batch file, you would need to use the SHIFT command.
11.
555
ACTIVITY: USING THE SHIFT COMMAND
Use any text editor to create and save the file
ALPHA. BAT
as follows:
(9)ECHO OFF
ECHO
%0 %1 %2 %3
SHIFT
ECHO
%0 %1 %2 %3
SHIFT
ECHO
%0 %1 %2 %3
SHIFT
ECHO
%0 %1 %2 %3
WHAT'S
rMW!
purpose of the batch file is to demonstrate the SHIFT command. Remember that
.i~
ECHO just echoes what you keyed in. In your command line, however, even though
you have only four parameters (0 through 3), you want to key in more than four
values.
Remember, you must be at the system prompt, not in the editor. Key in the
lEnterl
following:
A: \ "ALPHA a
ALPHA a b c
a bed
bed
cdef
A:\:;.
WHAT'S I;;;Y";
mmmHAPPE'
, it
iNG1 Notice the output. In
moved each parameter over by one position.
Batch File
~ECHO OFF
Supplied Value from Command Line
Screen Displa
ECHO %0 %1 %2 %3
ALPHA is %0
ALPHAabc
a is %1
bis%2
cis%3
SHIFT
ALPHA is dropped as %0
a becomes %0
b becomes %1
c becomes %2
d becomes %3
ECHO %0 %1 %2 %3
ais%O
b is %1
cis%2
dis%3
SHIFT
a is dropped as %0
b becomes %0
abcd
556
CHAPTER 11
ADVANCED BATCH FILES
c becomes %1
d becomes %2
e becomes %3
ECHO %0 %1 %2 %3
bis%O
bcde
c is %1
dis%2
eis%3
SHIFT
b is dropped as %0
c becomes %0
d becomes %1
e becomes %2
f becomes %3
ECHO %0 %1 %2 %3
cis%O
cd e f
d is %1
eis%2
Us %3
You should see that you are indeed shifting parameters, but how is this useful?
You will write some batch files that use SHIFT so you can see how this technique
can be used.
As you know, the operating system stamps each file with the current date and
time when it is created or modified. Most often, this means that each file has a
unique time and date based on the last time you modified or created the file.
Sometimes, you want to place a specific time and date stamp on a file or group of
files. For example if you sell software and you have customers to whom you send
files, you might like to ascertain which version of the file they have. By having a
particular date and/or time on the file, you
dependent on the file modification date.
Commands such as XCOPY can back up files after or before a certain date. To
ensure that you are backing up all the files you want, you can set the date and
update the date stamp on your files. Then you can backup from that date. You need
a way to update the dates.
COpy
filename Ib
Remember, the + sign tells the operating system to concatenate files. The first
thing that happens when copying files is a file name is
and date in the destination directory. At first, the new
specific destination file name, COpy will default to the source file name. It then
proceeds to concatenate (add) the existing file to the "new " file name and the new
date and time. In essence , it is copying a file onto itself. Since it is a new entry in the
directory table , it has the current date and time.
The /B switch tells the operating system to copy the file in binary mode. When
you concatenate files with no switches , the files are copied in text mode. The COpy
command knows the contents of the file have ended when it sees a special mark
called an
EOF (end-of-file) mark.
Typically, the EOF mark is
ICtr11
+ Z. The instant
COpy sees this special signal, it thinks there is no more information to copy and will
place its own EOF mark at the end of the
ICtr11 + Z. Unfortunately, this
extra " EOF mark is sometimes interpreted by a program or a data file as something
ji
11.
ACTIVITY: USING THE SHIFT COMMAND
557
other than the end of the file. Thus, you could be in the situation of not copying the
entire file. The alternative is to copy the file in binary mode. When you choose this
option-the /B switch-COPY will not read the
ICtr11
file, ensuring that the entire file contents are copied without adding an extra
Z. An extra
ICtr11
+ Z can ,create problems when you are trying to use the copied file.
Now that you know how and why to update file dates and times, it
place these commands in a batch file. Since you may have more than one file you
wish to " stamp, " you want to allow for many file names by using the SHIFT command.
Use any text editor to create and save a new 'file. Name the file
and then key in the following:
:DOIT IEnterl
IEnterl
COpy %1
SHIFT
IEnterl
PAUSE
IEnterl
BA T
GOTO DOlT
Key in the following: A: \
Key in the following: A:
P AR
IEnter I
BUD
Enter I
A: \::-DIR JUP.
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory of A:
05/07/2002
190 JUP. PAR
07:41 AM
1 File (s)
190 bytes
918, 016 bytes free
0 Dir (s)
A: \::-DIR
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
I'
Directory of A:
10/31/2001
07: 08
478 VEN
1 File (s)
478 bytes
918, 016 bytes free
0 Dir(s)
A:\::WHAT'S YH!HN"".
BUD to have today s date
Wif%J1HAPPENING? You
and time on them. Remember, the number of bytes free on your DATA disk will not
necessarily match the number shown in the directory displays in the book.
Key in the following: A: \:::-UPDATE JUP. PAR VEN. BUD
IEnterl
A:\::-UPDATE JUP. PAR VEN. BUD
A:\::-COPY JUP. PAR /B +
l::-NUL
~iI
\ '
558
~M
CHAPTER 11
i A:
,T
A: \ ~PAUSE
Press any key to continue
WHAT' S
Wi!!iwn+nC
W1WiHAPPENiNG? The
, went to SHIFT, and is now going to
copy the next parameter it shifted, VEN. BUD.
IEnterl
Press
A:\~UPDATE JUP. PAR VEN. BUD
A: \~COPY JUP.
1 ~NUL
A: \~SHrFT
A: \~PAUSE
Press any key to continue
A: \~GOTO DOrT'
A:\~COPY VEN. BUD /B +
1 ~NUL
A: \ ~SHrFT
A: \ ~PAUSE
Press any key to continue
WHAT' S
!iiiWiniM%W
It copied VEN. BUD.
W1Wi
Press
Enter I
A: \~GOTO DorT
A: \~COPY
/B +
bNUL
A: \~SHrFT
A: \~PAUSE
Press any key to continue
WHAT' S iiTiNNin;;?
W1WiHAPPENING1 This
it. It copied JUP.PAR. It then shifted over to VEN. BUD and copied that. However
you created an endless loop. When the batch file finished copying VEN. BUD
, it
again shifted parameters, but there was nothing to shift to. The batch file is continually going to the label and then trying to execute the command. There is something
missing here: a condition that you need to insert. First, though, you must break into
the batch file.
10
i': "
Press
ICtr11
+ C and answer Y to the prompt.
11 Key
JUP. PAR
IEnterl
12
VEN. BUD
IEnterl
Key in the following: A: \::.DIR
11.
ACTIVITY: USING THE SHIFT COMMAND
Terminate batch job
A:\::.DIR JUP, PAR
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory of A:
05/05/2002
190 JUP. PAR
190 bytes
12:31 PM
1 File (s)
918, 016 bytes free
0 Dir (s)
A:\::.DIR VEN. BUD
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory of A:
05/05/2002
478 VEN. BUD
12:32 PM
1 File (s)
478 bytes
918, 016 bytes free
0 Dir (s)
A:\::'
WHAT'S
HAPPENING? The file worked-the
13
date and time should be the current date
and time. You can create a batch file and use SHIFT to identify the size and
of files in a directory so you can determine whether the files will fit on a floppy disk.
SIZE. BAT that contains the
Use any text editor to create and save a file named
following:
:TOP
Directory
DIR
%1
I FIND
DIR
%1
: FIND " bytes"
I FIND
TEMP. FIL
free " ~~ TEMP. FIL
SHIFT
GOTO TOP
TYPE TEMP . FIL
PAUSE
DEL TEMP . FIL
WHAT'
t'iiiW'iWi%W
\I.\'iiif&
and the directory they are in, you filtered the output from the DIR command to
include only the items that you wanted. In a normal directory display such as the
one shown below, you want to capture the highlighted lines:
Volume in drive C has no
Volume Serial Number is 07D1- 080F
10/30/2001
10/31/2001
10/30/2001
10/31/2001
01: 46p
07: 08p
03: 42p
01 :
148 AST. 99
478 VEN.
190
406 MER.
68, 145, 774, 592
free
559
~, ': , .
560
CHAPTER 11
In order to do so, you had to filter the output. The line in the batch file, DIR %1 :
FIND " Directory" :;.:;. TEMP. FIL , found the first highlighted item. It is the only line
with " Directory " in it. The line in the batch file, DIR %1 : FIND "bytes " : FIND
"free " :;.:;. TEMP.FIL , was looking for a line with the word "bytes " in it. There are
two lines with "bytes. " You only want the first line, 4
222 bytes, so you
piped the output e~inating the
word " free " in it (0 Dir(s) 68, 145, 774 592 bytes free ). You then used
would see both the name of the directory and the bytes in the directory. Had you not
used :;.:;. ,
you would have
TEMP.FIL. At the end of
overwritten
TEMP.FIL so it will not take
14
Key in the following: A: \::-SIZE
IEnterl
CLASS TRIP
A: \ ::-GOTO TOP
A: \::-DIR TRIP I
TEMP. FIL
A:\::-DIR TRIP I
Iv
"free"
FIL
A: \::-SHIFT
A: \::-GOTO TOP
A: \::-DIR
I FIND "Directory"
A: \::-DIR
I FIND "bytes"
WHAT' S
f#HtWfimmc
I!!iMm
FIL
IV
"free"
FIL
Your batch file is running endlessly. You again created an
loop.
15
Press
ICtr11
IBreakl and answer V to the prompt.
A: \ ;SHIFT
A: \ ::-GOTO TOP
A: \::-DIR
: FIND "Directory"
A: \::-DIR
: FIND "bytes"
Terminate batch job
TEMP. FIL
Iv
"free"
FIL
(Y/N)?
A:\::WHAT' S
::'9159&;""::
I!!iMm
into the batch file.
16
\~i
Key in the following: A: \::-
TYPE TEMP. FIL :
IEnterl
::,:::~~~
11. 9 THE
A:\~TYPE TEMP. FIL
Directory of A:
14 File(s)
Directory of A:
19 File (s)
Directory of A:
71 File (s)
Directory of A:
71 File (s)
Directory of A:
71 File(s)
Directory of A:
71 File (s)
Directory of A:
71 File (s)
Directory of A:
71 File (s)
Directory of A:
71 File (s)
Directory of A:
71 File (s)
Directory of A:
71 File (s)
209 bytes
860 bytes
26, 504 bytes
26, 570 bytes
26, 636 bytes
26, 702 bytes
26, 768 bytes
26, 834 bytes
26, 900 bytes
26, 966 bytes
27, 032 bytes
- More 17
Press
ICtrll
WHAT' S
+ C to stop the processing, if necessary.
iiiWftWKW.
Wi!%! HAPPE NINCJ?
Your file may be shorter or longer, depending on the length of time
rsreakl. On some systems ICtr11 + C will stop
ICtr11 IBreakl is necessary. In any
case, you got more information than you wanted. You now know the size of
CLASS and TRIP directories, but the other information is useless. What you are
missing is conditional processing. (The size of your directories may be different,
before you " broke out" with
ICtr11
this file from executing, and on other systems,
depending on the work you have done on your DATA disk.)
),M~:::::;:::J~x;::;;~:::m::.:::~::::~
lIE
The IF Command
The IF command allows for conditional processing. Conditional processing is a
powerful tool in programming. Conditional processing allows a comparison between two items to determine whether the items are identical or whether one is
greater than another. A comparison test will yield one of only two values-true or
false. If the items are identical, the condition is true. If the items are not identical, the
condition is false. Once you establish a true or false value, you can then direct the
program to do something based on that value. Conditional processing is often
expressed as IF the condition is true , THEN do something; IF the condition is false
THEN do nothing.
In batch files, the IF command will test for some logical condition and then, if the
condition is true, the batch file will execute the command. If the test is false, the
command will not be executed and the batch file will fall through to the next
command line in the batch file. The IF command in batch file processing can check
for three conditions:
561
562
CHAPTER 11
1. Whether two sets of
a string, as in a string of data (sometimes referred to as a character string).
2. Whether or not a file
3. The value of the variable in
program can set depending on the outcome of a process , such as checking a
true! false condition. ERRORLEVEL
Here is the syntax for IF/IF NOT
Appendix H):
, see
Performs conditional processing in batch programs.
IF
IF
IF
NOT
Specifies that Windows XP should carry out
the command only if the condition is
ERRORLEVEL number Specifies a true condition if the last program
run returned an exit code equal to or greater
than the number specified.
stringl==string2
Specifies a true condition if the specified text
strings match.
EXIST filename
Specifies a true condition if the specified
filename exists.
DIm
You can use the IF command with character strings to test
is exactly the same as another. You can tell the IF statement to GOTO a label or to
perform an operation when the strings match and the condition is true.
you can tell the IF statement to
strings do
not
match and the condition is false. What is to be compared is separated
by two equal signs (==).
,8'~':,~*:O:*::,~j.
~B~::::M;:::-::'mm;~::
DDI
Activity:
Command with Strings
Note:
The DATA disk should be in Drive A. The default drive and directory
should be A:
Use any text editor to create and save a file called GREET. BAT.
nO' spaces
IF %1 ==Carolyn
IF %1 = = Bette
ECHO Isn t anyone there?
GOTO FINISH
:Carolyn
) Key in the following:
IEnterl
IEnterl
Enter I
IEnterl
IEnterl
ECHO Greetings, Ms. Carolyn. IEnterl
GOTO FINISH
f V:
: :i
Enter I
(Note:
There are
11.
: Bette
11
563
Enter I
Ms. Bette. IEnterl
ECHO Greetings,
: FINISH
WHA T'5 W'miiW&L
!tIi1M1
ter strings. You did not place ECHO
OFF
at the beginning
of
the file so you can see
what happens when it
Key in the following: A: \::-GREET
IEnterl
Carolyn
A: \ ::-GREET
A:\::-IF Carolyn
A:\::-ECHO Greetings , Ms. Carolyn.
Greetings, Ms.
A: \::-GOTO FINISH
A:\::WHA T'5
G7
11!I!!!W HAPPENI'N'
executed. When Carolyn took the place of %1 , the line read IF Carolyn==CaroIyn,
which is a true statement because the strings
of
data matched exactly. Since it is true
it performed the GOTO Carolyn command. The line after the label :Carolyn was
then displayed: Greetings, Ms. Carolyn. The line following said GOTO FINISH
which it did. After the label :FINISH, there were no more lines, and you were
returned to the system prompt.
Key in the following: A: \::-GREET
Bette
IEnterl
A: \::-GREET
A: \::-IF Bette
A: \::-IF Bette
A: \ ::-ECHO
Greet ings ,
Ms. Bet
A:\::WHA 1' 5
MA\\;:W1nii
i8HAPPENIf.,iG7 When you keyed in GREET Bette, it read the first line as IF
Bette==CaroIyn GOTO Carolyn. Bette does not equal Carolyn, so it is a
statement. Therefore , the batch file did not go to the label :Carolyn but fell through
to the next line. The line then read as IF Bette==Bette, which is a true statement
because the strings
of data match exactly. Since it is true , it performed the GOTO
Bette command. The line after the label :Bette was then displayed: Greetings, Ms.
Bette. The line following said :FINISH , which it did. After the label :FINISH , there
were no more lines, and you were returned to the system prompt.
Key in the following: A: \::-GREET
Juan
Enter I
~iI
.j
'11
564
CHAPTER 11
ADVANCED BATCH FILES
A: \ ::-GREET Juan
A: \::-IF Juan
A: \::-IF Juan
A: \ ::-ECHO I sn '
Bette
GOTO Bette
t
Isn' t
A: \::-GOTO FINISH
A:\::WHAT'S
fimlflliHAPPENiNG1 You
Juan==CaroIyn GOTO Carolyn. Juan does not equal Carolyn, so it
ment. The batch file did not go to the label :CaroIyn but fell through to the next line.
The line then read as IF Juan==Bette. This is another false statement, so the batch
file did not go to the label :Bette but fell through to the next line. The line following
said ECHO Isn t anyone there? Thus , Isn t anyone there? was displayed (echoed)
to the screen. It then fell through to the next line , which was GOTO FINISH. After
the label :FINISH, there were no more lines , and you were returned to the system
prompt.
Key in the following: A: \::-GREET
BETTE
IEnterl
A: \ ::-GREET BETTE
A: \::-IF
A: \::-IF
t anyone there?
t anyone there?
A: \::-ECHO Isn'
Isn'
A: \::-GOTO FINISH
A:\::WHAT'S
fimlflliHAPPENiNG? You
BETTE==Carolyn GOTO Carolyn.
statement. The batch file did not go to the label :CaroIyn but fell through to the next
line. The line then read IF BETTE==Bette, which is another false statement. Even
though the word is the same , the case is different. Both sides of == must match
exactly.
Because it was not an exact match, the batch file did not go to the label
:Bette, but fell through to the next line. The line following said ECHO Isn t anyone
there? Thus , Isn t anyone there? was displayed (echoed) to the
through to the next line, which was GOTO FINISH. It did. After the label :FINISH
there were no more lines, and you were returned to the system prompt. If you wish
to ignore case , you can add a parameter, the /1, which when included, tells the batch
file to ignore case. The command would be written as
,"
11.
13
%1= =Carolyn GOTO Carolyn
%1= =Bette GOTO Bette
and so on. The /1 must immediately follow the IF statement.
IF
II
IF
II
:::::'$::::;:;s:::~~:::;~~~:t.::;.-:t:::':~,~:&":;.~~:::;:::::;::-.;:;:-
DIfJ
In the above example, you tested for an exact match of character strings. What if
you have nothing to test for? For example, in the batch files you wrote
UPDATE. BAT and SIZE. BA'I: you used SHIFT. SHIFT kept shifting parameters
all of them were used. When there were no more parameters , you were in an
endless loop. You can test to see if a string matches, but
This is called testing for a
null value.
You are literally testing for nothing. You must
have " something " to test for " nothing. " Thus, you place a value in the test that will
give you nothing.
There are a variety of methods for testing for null values. One method is to use
quotation marks so that your statement becomes IF " %1"== "" GOTO LABEL. The
second set of quotation marks is keyed in with no spaces. This statement says
nothing is there, GOTO somewhere else. " You may also make the line read IF
%lvoid==void GOTO
then look like Carolynvoid==void. This is not true, so it would proceed to the next
line. If there was no value, your line would look like void==void. Now this is true,
and the GOTO label would execute. You may use any word; " void" was used in this
example. Another method is to use \ so that the statement would become IF
\ %1 \ == \ \
look like \Carolyn \== \ \. This is not true, so it would proceed to the next line. If
there were no value, your line would look like \ \== \ \. Now this
is true and the
GOTO label would execute.
~*,'W';::E%~~:mmm::~::m~~::::::~;:.;:~::;:;%i"
III:i) Activity:
Note:
The DATA disk should be in Drive A. The default drive and
should be A:
Edit and save the file called UPDATE. BAT to look as follows:
: DOlT
IF
%1"
COpy %1
"11
GOTO END
IB + ~
NUL
SHIFT
PAUSE
GOTO DOlT
:END
Key in the following: A: \::-DIR
CAROLYN. FIL IEnterl
A: \~DIR
Volume in drive A is DATA
Volume Serial Number is 30BB- DAID
565
,,"-.:~: ::.:,j! ':
566
CHAPTER 11
ADVANCED BATCH FILES
!1
Directory of A:
1.
07/31/1999
12 : 53
47 CAROLYN. FIL
1 File (s)
47 bytes
915, 96~ bytes
0 Dir(s)
A:\"'
xnw%c'w,
WHAT'S
The file called CAROLYN. FIL has a date of 7-31- 99. You are going
to update only the date on this file. SHIFT
nothing " to
, but now you are testing for a null value. Once th~ file is updated , you will go to
END.
OOJWJ1HAPPENING'
Key in the following: A: \
:-UPDATE CAROL YN. FIL IEnterl
A: \
A: \"'IF "CAROLYN. FIL"
A: \ ",COpy
1 ",NUL
A: \ ",SHIFT
A: \ ",PAUSE
Press any key to continue
WHAT' wnxxwxv
OOJWJ1HAPPENING7 The batch
FIL Prior to your
testing for a null value, the file looped endlessly. Now you will see if your test for
nothing " works. Remember, there is a SHIFT that will shift over nothing.
IEnterl
Press
A: \ ",PAUSE
Press any key to continue
A: \ ",GOTO DOlT
A: \"'IF "" ==
A:\"'
WHAT'S JWwtx!?' ;n;w
, was there, it was a true condition,
00JWJ1
and GOTO told it to go to the label called :END. Thus, it skipped the lines and went
directly to the end of the batch file.
Key in the following: A: \:-DIR
CAROLYN. FIL IEnterl
A: \ ",DIR
Volume in drive A is DATA
Volume Serial Number is 30BB- DA1D
Directory of A:
"~I
i~_
I ;
' r~ '
: I
11.
05/06/2002
11:34 AM
13
567
47 CAROLYN. FIL
1 File (s)
0 Dir(s)
47 bytes
915, 968 bytes free
A:\::WHAT'S ;WWghY
I1MillI
ent), and you were not in an endless loop. You are now going to try another technique to test for a null value.
SIZE. BAT
Edit and save the file called
to look as follows:
:TOP
IF %1nothing==nothing
DIR %1 :
Directory " ~~ TEMP. FIL
DIR %1 :
bytes
free "
" : FIND
~~ TEMP. FIL
SHIFT
GOTO TOP
TYPE TEMP . FIL
PAUSE
DEL TEMP. FIL
:END
TEMP. FIL IEnterl
Key in the following: A: \::-DEL
WHAT'S :nn' %Mn+:
:mmHAPPENING? You wanted
, because the last time you ran
this batch file, you were stuck in a loop and TEMP.FIL did not get deleted. You
never reached that line in the batch file.
Key in the following: A: \:.SIZE
IEnterl
CLASS TRIP
A: \::-SIZE
A: \ ::-IF CLASSnothing ==
A: \ ::-DIR CLASS
I FIND " Directory"
A: \ ::-DIR CLASS
I FIND "bytes"
1::-::-TEMP. FIL
: FIND /v "free"
1 ::-::-TEMP.
A: \ ::-SHIFT
A: \ ::-GOTO
A: \::-IF TRIPnothing
A: \::-DIR TRIP
I FIND " Directory
A: \ ::-DIR TRIP
I FIND "bytes"
A: \ ::-SHIFT
A: \::-GOTO TOP
A: \::-IF nothing
A:\::-
1::-::-TEMP.
I FIND /v "free"
1::-::-TEMP.
FIL
568
CHAPTER 11
WHA rStNNHWiWWf
WWfflHAPPENING? You did not have
, when you
tested for a null value and there was a null value
, you told the batch file to
GOTO
END. It did so, but, by going to the label :END
, it never processed the other three
lines in the batch
, PAUSE
writing batch files (and programs) is a complicated task. You , and DEL. This is why
what you are trying to do and what consequences your instructions will have.
Key in the following: A: \=-TYPE TEMP.
FIL
IEnterl
A: \~TYPE
Directory of A:
14 File (s)
Directory of A:
209 bytes
19 File(s)
860 bytes
A:\~
WHAT'
W't"1WW*,
~HAPPENING7 The batch file
, to some degree. You got the
information in the file TEMP.FIt
, but the file was never displayed or deleted. Thus
you must find another solution to the problem.
~:"'::::::%:::::~:%:::-.::::m~~~:';-':0~::;:;::~WM~%:
DBD
The IF EXIST command uses a file
condition is true. Processing then passes to the specified
GOTO
location or to the
the IF statement. If the file does not exist
and the operating system ignores the command in the IF , the condition is
command that
false
follows
process then reads the next line in the file. When you use IF NOT
does not exist, then the condition is true. Processing then passes to the specified
GOTO
location or to the command that follows the IF NOT statement. If the file
does exist, the condition is false and the batch process will fall through to the next
line in the batch file. An important part of the IF EXIST
NOT EXIST command is
IIF
with directory names.
that it works only with file names and not
~;,~~::~":~.;:it&"'#?=-"%::'=%;:::M~i,?:::;:f;:::~t-
o:m
Note:
The DATA disk should be in Drive A. The displayed
Use any text editor to create and save a file called RENDIR.
following:
IF \%1\==\\
IF NOT \%2\==\\
IEnterl
IEnterl
ECHO You must include a destination name
ECHO for the new directory name.
GOTO end
:next
IEnterl
IEnter)
IF EXIST %1
REN %1 %2
Ji...
Enter I
IEnterl
IEnterl
IEnterl
BAT. Key in the
11.
GOTO end
:message
15
569
IEnterl
IEnterl
ECHO This is a file. not a directory. IEnterl
:end
WHAT' S d;wmwkWW
HAPPENING? This batch file
not a file. The folloWing table analyzes the batch file one line at a time. The line
numbers are for purposes of reference only.
1.
2.
3.
4. ECHO
5. GOTO
6.
7.
8.
9. GOTO
10.
11. ECHO
12.
Batch File b Line Number
Test TRUE
Processin
Test FALSE
1. IF \%1
User keys in
Testing for
nothing for %1.
null value.
User keys in
value for %1.
Since test is
Since test is
true, action is
false, action is
to go to line 2.
to go to line 12.
2. IF NOT
3. ECHO You must include a
destination name
4. ECHO for the new
directory name.
5. GOTO end
User keys in
Testing for
nothing for %2.
null value.
Since test is
Since test is
true, action is
to go to line 3.
false, action is
to go to line 6.
Message for
user that he
or she did
not include
a value.
Continuation
of the
message.
Falls through
to the GOTO
end statement.
Action is to go
to line 12.
6. :next
User keys in
value for %2.
Label referred
to in line 2.
',
.,'. .
"1
f1'
570
CHAPTER 11
7. IF EXIST %1 GOTO message
User keys in
file name for
Testing for
User keys in
value for %1.
directory for
Is it a file or
is true , action is a directory?
%1. Since test
%1. Since test
to go to line 10.
is false, action
is to go to line 8.
8. REN %1 %2
Since %1 test
is false (not a
file), re-naming
directory can
proceed.
9. GOTO end
After directory
is renamed
falls through
to GOTO end.
Label referred
10. :message
to in line 2.
11. ECHO This is a file, not a
directory.
Message that
user used a file
name, not a
directory name.
12. :end
Key in the following: A: \,.RENDIR
JUP.
PAR
LAST /Enter I
A: \ ,.RENOIR JUP.
A:\~IF \JUP. PAR\ == \\ GOTO end
A: \~IF
A:\~IF EXIST JUP. PAR GOTO message
A: \~ECHO This
This is a
A:\~
WHAT'S
tWW!!WiN
iIOOI11IHAPPENING? Since
is true , the batch file executed GOTO and went to the label :message. What if it is a
directory and not a file?
Key in the following: A: \,.RENDIR
A: \~RENOIR TEST
A: \~IF
A: \~IF
A: \~IF
TEST OLDER
IEnterl
11.
15
571
A: \~ECHO This
This is a
A:\~
WHA T'
mefLlW"%\1'
I\B1! HAPPENING?
message. The directory was not renamed. You cannot use IF
existence of a directory, as it only works with files. There is a way around this-you
can " fool" the IF EXIST command. To check for the existence or nonexistence of a
directory, you must use NUL. The null (NUL) device does exist in every directory.
NUL is a device that discards anything sent to it. By using %1 \NUL, you force IF
EXIST IIF
a NUL file (or the nonexistence of a nothing file) in the directory
NUL, then %1 does not exist.
for
If it cannot get through %1 to look
Edit the
RENDIR. BAT
file so that you change the line
IF EXIST %1
to read
IF NOT
Key in the following: A:
IEnterl
\;:.RENDIR TEST OLDER
A: \ ~RENDIR TEST
A: \~IF
A: \ ~IF
A: \ ~IF
A: \ ~REN TEST
A: \
A:\~
WHA 1'5
I\B1! HAPPENING?
whether TEST was a directory. If this statement were false (a file name), then the
batch file would execute the command following IF (the GOTO
TEST does exist (TEST\NUL) the statement is true, the
ignored, and the batch file falls through to the next line and renames the directory
from TEST to OLDER Using the logic you just learned, you can correct
SIZE. BAT so that it processes all the lines in the batch file. There is one more piece
of information you need.
Edit and save the file called
SIZE. BAT
to look as follows:
IF EXIST TEMP. FIL DEL TEMP. FIL
:TOP
IF %1nothing==nothing
IF NOT
DIR %1 :
Directory" ~~ TEMP. FIL
\::\'
572
CHAPTER 11
% 1
DIR
FIND
bytes
free
FIND
TEMP . FIL
:NEXT
SHIFT
GOTO TOP
:END
TYPE TEMP . FIL
PAUSE
DEL TEMP . FIL
WHAT' S
ilW41l&i%1i!1i!
, IF EXIST TEMP.FIL DEL TEMP. FIL, looks for the file
itWiI!m
called TEMP.FIL and delete it if it exists. Then when you create TEMP.FIL , it will be
, will
a new file every time. The next addition, IF NOT EXIST %1
see if a directory exists. That is the purpose of %1 \NUL. If it
will go to the :NEXT label,
not at the top of the batch file because you want to delete TEMP.FIL only the
time you execute the batch file. Notice that you had to move the :END label. In its
previous batch file location, you would not have been able to read TEMP.FIt.
Key in the following: A: \;:.SIZE
CLASS JUP. PAR TRIP
IEnterl
A:\~SIZE CLASS JUP. PAR TRIP
A: \~SIZE
A:\~IF EXIST TEMP. FIL DEL TEMP. FIL
A: \~IF
A: \~IF
A: \~DIR
A: \~DIR
A: \;:.SHIFT
A: \ ~GOTO TOP
A: \;:.IF JUP. PARnothing
A:\~IF NOT EXIST JUP. PAR\NUL GOTO NEXT
A: \~SHIFT
A: \~GOTO TOP
A: \~IF
A: \~IF
A: \~DIR TRIP
I FIND "Directory"
A: \ ~DIR TRIP
I FIND "bytes"
A: \;:.SHIFT
A: \ ~GOTO TOP
tl~:!
1;:.~TEMP. FIL
: FIND Iv "free"
1~~TEMP. FIL
':::~~,::~~
11. 16 THE
573
A: \~IF nothing
A: \ ~TYPE
Directory of A:
14 File(s)
209 bytes
19 File(s)
860 bytes
Directory of A:
! A:
i Press any key to continue
WHAT'S
WMwmWML
I!mHAPPENING7 Your batch file
PAR, knew it was a
file, and did not include it in the output. The more complicated you want a batch
file to be, the more you will have to analyze the logic of what you want to do
how to accomplish it.
Press
Enter I
A: \~PAUSE
Press any key to continue
A: \ ~DEL
A:\~
WHAT'S
tw.mHAPPENING? The batch
and you have returned to the system level.
om
~~':::%8::o/:d%:~~m:;H::'.
:m':f,
A program can set an
exit code
when it finishes executing. A batch file can test this
exit code with the IF ERRORLEVEL statement. Actually, the name ERRORLEVEL is
a misnomer because the number returned does not necessarily mean there was an
error. For instance, the test IF ERRORLEVEL 3
than or equal to 3. Thus , an exit code is not tested for a match with ERRORLEVEL
but to determine if it is greater than or equal to it. The test
be true since every possible exit code is greater than or equal to
trickiest thing about testing ERRORLEVELs in
always
be listed in
descending
order when you use IF
when you use IF NOT ERRORLEVEL. For instance
following exit codes:
0 Files were copied without error.
1 No files were found to copy.
You can write a batch file testing for exit codes.
ascending
, COpy will set one of the
order
574
CHAPTER 11
~';:%~::::m,~:::X:;'~':::;:"'x,8:z..::::;::3.::~2.:':~8:,
III6 ERRORLEVEL
The DATA disk should be in Drive A. The displayed prompt is A:
Note:
Use any text editor to create and save a file called ERROR. BAT. Key in the
following:
COpy %1 %2 lEnterl
IF
IF
IEnterl
IEnterl
IEnterl
:NOTOK
ECHO There are no %1 files. Try again. IEnterl
IEnterl
GOTO END
:OK
Enter I
ECHO You copied the %1 files successfully. IEnterl
:END
Key in the following: A: \ :.ERROR . .TXT OLDER
IEnterl
A: \ ~ERROR * . TXT OLDER
A: \ ~COPY * . TXT OLDER
BORN. TXT
Sandy and Patty, txt
Sandy and Nicki. txt
LONGFILENAME . TXT
LONGFILENAMED . TXT
LONGFILENAMING. TXT
CHKDSK. TXT
TXTFILES . TXT
ASTRO . TXT
DANCES. TXT
HELLO. TXT
TITAN. TXT
JUPITER. TXT
GALAXY. TXT
MERCURY. TXT
PLANETS. TXT
VENUS. TXT
LOG. TXT
18 file
A: \ ~ I F
A: \~IF
A: \~ECHO You
. TXT
You copied the * . TXT
A:\~
WHAT' S
MWit1Niili\N
~HAPPENING? You
TXT files to the OLDER subdirectory.
The exit code that was generated by COpy gave you the message that the copy was
successful.
Key in the following: A: \:.ERROR .
lh Ii'
NON OLDER
IEnterl
"575
11. 18 WRITING
A: \ ~ERROR * . NON OLDER
A: \ ~COpy * . NON OLDER
NON
The system cannot find the file specified.
0 file (s) copied.
A: \~IF
A:\~ECHO There are no * NON files. Try again.
There are no * NON files. Try again.
A: \
A:\~
WHAT'S
~HAPPENING7 Again, the exit code was correctly read. As you can see, you can
use the exit codes successfully in a batch file. Since programs like COpy give you a
message anyway when it could not find the file or files, you may ask yourself, why
go to the trouble of writing a batch file? The reason is that you can write a small
program to test for other kinds of information.
~:;'~*~'%~::X:xW;:::::r~~::W"
~~VX&H.
BIim
Rather than being limited to the exit codes that are set by operating system programs , you can write a small program that will create an exit code based on some
activity. For instance , a program can be written that will identify which key was
pressed and report which key it was. You can do this
Actually, two things are rescan code.
ported when you press any key on the keyboard. First, that you pressed a key.
Second , that you released the key. The keyboard controller tells the CPU that some
keyboard activity is occurring. The stream of bytes is converted into the scan code,
which identifies the specific key (see Appendix G for a list of scan codes for all the
keys).
You are going to write a program that will
pressed on the keyboard. Once you know the reported code, you can test for a
key, if is identified by a one- or two-digit
specific key using ERRORLEVEL in the batch file. The batch file can then act based
on the reported code. In order to do this, you must write a program. Remember, to
be executed, a program must be in ' 'bits and bytes
the Os and Is the
understands.
There are several ways to write a program. One is to know a programming
language and be able to turn the programming language program (source code) into
executable code (object code). This is called compiling a program-turning a
language into code. That task is beyond the scope of this text. Fortunately, there is
an easier way that you can create a small
utility program called
DEBUG can directly modify bytes in a file. DEBUG allows you to test and debug
with a . COM or . EXE file extension. Remember, you cannot
executable files-those
'11
576
CHAPTER 11
ADVANCED BATCH FILES
EXE or . COM because those file
extensions indicate programs that are not ASCII-readable files. DEBUG is a small
program that has its own commands and syntax. If you know the
of the
use TYPE to look at a file with the extension
of
of programming, you could write a . COM program
directly with DEBUG. Unless you are a
, you will probably not
DEBUG program and the rules
want to do this.
The easiest way to use DEBUG is to create a script or a
script file.
A script is a set
instructions that you can write in any ASCII editor. Once you
script, you can
feed"
it to the DEBUG program via redirection (DEBUG
SCRIPT. FIL). DEBUG will then convert the script file to an
a . COM file extension. Once you have a . COM file, you can execute it as you do any
of
program. This process is the simplest way to create a file that will report the scan
code for any key that is pressed. The program you create will be called
Since using DEBUG directly can be tricky, the example below shows a . COM
program written with
of a pressed key. If you
want to try to use
be in
this typeface
and what you key in will be in this typeface. The hyphen (-) and
the colon (:) are prompts presented to you by the DEBUG program. Instructions
such as 100 assemble the program at memory address 100
vary from machine to machine. In the example shown here,
segment I offset memory address. You must press
when
of
commands available
within the DEBUG program:
assemble
compare
dump
enter
fill
(address)
C range address
(range)
E address (list)
F range list
(=address) (addresses)
hex
H value1 value2
I port
move
name
M range address
input
load
output
proceed
quit
register
(address) (drive) (firstsector) (number)
(pathname)
(arglist)
0 port byte
(=address) (number)
(register)
search
range list
unassemble
wri te
(address) (drive) (f irstsector) (number)
trace
(=address) (value)
(range)
allocate expanded memory
deallocate expanded memory
map expanded memory pages
display expanded memory status
XA (#pages)
XD (handle)
XM (Lpage)
The following is shown as an example
of how to use DEBUG , but you do not
have to do this. If you do, note the differences between the letter I and the number 1.
Be sure and check with your lab administrator before attempting to key in this
example. Be very sure you are at the A:
~1,
11.
19
577
A: \:::-DEBUG
-a 100
15BE:0100
IEnterl
mov ah, lEnterl
15BE:0104
int 21 1Enterl
cmp al, IEnterl
15BE:0106
jnz lOa
15BE:01OB
int 21
15BE:0102
IEnterl
Enter I
15BE:01OA
mov ah,4c IEnterl
15BE:01OC
int 21
1Enterl
15BE:Ol OE Enter I
IEnterl
-r cx
0000
CX
IEnterl
:e
IEnterl
-n reply. com
Enter I
-w
Writing
Enter I
An easier way to create REPLYCOM is to create a script file. Again, a script file
merely a text file that contains a series of commands that can be redirected into
DEBUG to create a . COM file. The script file is not the program. You use any text
editor; name the file, in this case
Then, to make REPLY. SCR an executable program you redirect it into DEBUG to
create REPLY. COM.
(Note:
REPLYCOM.
has created REPLYCOM for
om
you
The next activity will show
how to create REPLY. SCR and
You may want to check with your instructor to see if he or she
you.
.0~:::::3;':;-:~":X8:::.;,,~~v.-.::::m~~;'''~8":8~m,,;:1*-
The DATA disk should be in Drive A. The displayed
Note:
file called REPLY . SCR. Key in the
Use any text editor to create and save
following:
08
e 100 b4
rcx
75
00
cd 21 3c
02
cd 21 b4 4c cd 21 1Enterl
IEnterl
IEnterl
n reply. com
~l
IEnterl
lEnterl
WHAT'S pw\;;;nPd
~HAPPENING? Now that
you
have written REPLY. SCR
you
must now " as-
semble " it or convert it into the bytes that make it a program. You do this by redirecting the script file into DEBUG.
Key in the following: A: \::-DEBUG
SCR
Enter I
A: \ ~DEBUG "
e 100 b4 08 cd 21 3c 00 75 02 cd 21 b4 4c cd 21
-rcx
CX
0000
,.~c.
:;'
578
CHAPTER 11
I~I
-n reply. com
Writing OOOOE bytes
A:\~
WHAT'S
You have compiled REPLY. SCR
1!iI!!miI
u;.to a program
REPLY. COM.
IEnterl
Key in the following: A: \:.DIR
A: \ ~DIR
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory of A:
05/06/2002
01: 32
14 REPLY. COM
1 File (s)
0 Dir (s)
14 bytes
899, 584 bytes free
A:\~
WHAT' S
i!!Ni'
%;;wwn
~HAPPENING?
batch file.
Now that you have written a program, you want to use it in a
Use any text editor to create and save a file called
KEYING. BAT
that contains
the following:
ECHO PRESS F1 TO CLEAR THE
ECHO PRESS F2 TO DISPLAY THE
ECHO PRESS ANY OTHER KEY TO
REPLY
IF
IF
IF
GOTO END
:F1
CLS
GOTO END
:F2
DIR
:END
t ~
WHAT'S
1!iI!!miI
.r
by pressing a key. Checking IF ERRORLEVEL codes in descending order is
critical
because the command is tested to determine if the error code is equal to or greater
than the value specified. In this program, if you press a key that returns a value of
61 or above , you exit the program. If you press
press (ElJ, it returns a code of 59. If none of those conditions exist,
batch file.
;r
t~~
.~,
':, )!:
:" ;.' ~
11.
19
579
IEnterl
Key in the following: A: \:.KEYING
A: \~KEYING
A: \ ~ECHO PRESS Fl
PRESS Fl TO CLEAR THE SCREEN.
A: \~ECHO PRESS F2
PRESS F2 TO DISPLAY THE DIRECTORY.
A: \~ECHO PRESS ANY
PRES ANY OTHER KEY TO EXIT.
A: \~REPLY
WHAT'S
~HAPPENING? You have executed the KEYING batch
REPLY. COM is waiting for you to press a
Press lflJ
A: \::-GOTO . END
A:\~
WHAT'S
itiiIIM
Key in the following: A: \::-KEYING
Enter I
Press ff2J
04/29/2002 01: 32
04/29/2002 01: 34
07/06/2002 10:33 AM
07/06/2002 10:33 AM
07/07/2002 04: 44
10/31/2001 07:08 PM
10/30/2001 12:46 PM
07/06/2002 11:37 AM
05/03/2002 02 :46 PM
10/31/2001 07:08 PM
10/31/2001 07:08 PM
07/07/2002 04:44 PM
05/03/2002 03: 07
07/06/2002 11:59 AM
07/06/2002 12:00 PM
07/06/2002 12:00 PM
07/06/2002 12:01 PM
07/06/2002 12: 02
07/06/2002 12: 02
07/06/2002 12: 03
07/06/2002 12: 03
07/06/2002 12: 59
05/03/2002 03 :48 PM
05/05/2002 11:24 AM
05/05/2002 11:30 AM
05/05/2002 12:22 PM
05/05/2002 12:43 PM
9 D. BAT
10 S
"DIR~
"DIR~
BATCH
UTILS
190 JUP. PAR
478 JUP
,i
148 JUP. FIL
13 JUP. XYZ
\1"
161 KILLIT .
~:j f;
478 VEN
478 VEN. PAR
478 VEN .. BUD
207 NOCOPY. BAT
1'.
30 N
ii!
64 log. bat
1!'
111 MULTI. BAT
36 BOG. BAT
113 LOG. TXT
13 0 TESTING. BAT
35 TEST. BAT
157 TEST2. BAT
149 DELTREE. BAT
253 DCOMP. BAT
182 ONE. BAT
102 REPEAT
104 ALPHA. BAT
191 GREET. BAT
~ r~
';it
r'
. I'.
ilt;,
?! I)
:I
~o.:_
'/. .''''''. /:.
580
CHAPTER 11
ADVANCED BATCH FILES
77 File (s)
.t
A:\~
10 Dir (s)
184 bytes
898, 560 bytes free
ill
r. ~
WHAT'S "
WWffl
on your screen. ) Pressing lE2J gave
REPLY.
the instruction in the batch file based on the key you pressed. Remember
, the
number of files, directories, and bytes free on your DATA disk
match the number shown in the directory displays in the book.
,;8$:::;:::::~~"':::~;:;:':::?.::;:;::8:-::::~~~~:?:~~ffi:::*-:
DEJ
The environment is an area that the operating system sets aside in
have used environmental variable PATH in a previous
variables that represent the drive and directory used by the operating system were
discussed. The environment is like a scratch pad where notes are kept about important items that the operating system needs to know. The environment is like a bunch
of post-it notes. Application programs can read any items in the environment and
variable
is a value that can change, depending
on conditions or on information passed to the program. Data consists of constants or
can post their own messages there. A
fixed values that never change and variable values that do change. The
environment
, in essence, an area in memory where data can be stored. When evaluating an
expression in some environment, the evaluation of a variable consists of looking up
its name in the environment and substituting its value. In programming, an
expresis any legal combination of symbols that represents a value. These variables are
used by the operating system to discover things about the environment it is operating in. Environment variables can be changed or created by the user or a program.
Programs can get the value of a variable and use it to modify their operation,
much like you can use a value in a command line argument. The operating system
has the ability to store data in memory. The stored data takes the form of two
strings-one is
, and the other is the value of the variable.
sion
An
environmental variable
is a name assigned to a string (value) of data. You can
set your own environmental variables. However, there are some common environmental variables that are set when you start Wmdows. There are environmental
variables that are commonly used which usually have short, easyto-remember
names. These environmental variables store information such as your user name
(USERNAME); the location where, by default, your files are saved
the search path the operating system uses to look for commands (PATH);
displayed in your prompt
tory-where the
location of the file CMD. EXE. You can also leave messages there via batch
from the command line. You do this with the SET command. Environmental variables set by the operating system will remain in effect throughout the entire work
session at the computer. Those set in the Command Prompt
executed in the Command Prompt window will remain in effect
only
during that
command prompt session. While values are in effect, you can use the syntax
~;
11.
ACTIVITY: USING SET AND THE ENVIRONMENTAL VARIABLES
VARIABLENAME%, which will use the value of
view the value of an environmental variable, you can use the syntax of ECHO
%ENVIRONMENTALVARIABLENAME%. The internal command
to display what is currently in the environment, set environmental variables, or
delete environmental variables. If you use the SET command, followed by a letter
the SET command will list any environmental variables
The basic syntax is:
SET (variable= (string) )
variable
string
Specifies the environment-variable name.
Specifies a series of characters to assign to the
variable.
Type SET without parameters to display the current environment
variables.
lIED
Environmental Variables
Key in the following: A: \:.SET
Note:
Enter I
If the your environment display is too long to fit on one screen, use the
MORE filter.
ALLUSERSPROFILE=C: \Documents and Set
APPDATA=C: \Documents and
CLIENTNAME=Console
CommonProgramFiles=C: \program
COMPUTERNAME=ADMINS04
ComSpec=C: \WINDOWS\system32\cmd. exe
HOMEDRIVE=G:
HOMEPATH=\
HOMESHARE=\ \Busdiv\User\bpeat
LOGONSERVER= \
NUMBER OF PROCESSORS=1
OS=Windows NT
Path=C: \WINDOWS\system32; C: \WINDOWS;
PATHEXT= COM; . EXE; . BAT; . CMD; . VBS; . VBE; . JS; . JSE; . WSF; . WSH
PROCESSOR ARCHITECTURE=x86
PROCESSOR IDENTIFIER=x86 Family 5 Model 8 Stepping
AuthenticAMD
PROCESSOR LEVEL=S
PROCESSOR REVISION=080c
ProgramFiles=C: \ Program
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
Sys temRoot=C: \
TEMP=C: \DOCUME- l \bpeat \LOCALS- l \ Temp
TMP=C: \DOCUME- l \bpeat\LOCALS- l \Temp
USERDNSDOMAIN=BUSINESS. SOLANO. CC. CA. US
USERDOMAIN=BUSDIVl
USERNAME=bpea t
USERPROFILE=C: \Documents and
581
pi'
i~
~L
582
CHAPTER 11
WHAT'S
WWffl HAPP' ENIN
ADVANCED BATCH FILES
G7
dows stores much information about your system (your environment) in the operating system environment. For instance, the environmental variable called ComSpec
has a value, in this example, of C: \ WINDOWS \system32 \cmd. exe. This tells the
operating system that the location of CMD. EXE is C: \ WINDOWS \SYSTEM32.
When you execute a program, Windows no longer needs the command processor in
memory. However, when you exit the program and need to key in another command , Windows must reload the command processor from disk. In order to do so
must know where it is
tion. The PATH value tells Wmdows what directories and in what order it is to
search for executable files. The value for SystemRoot, in this example, is
C: \ WINDOWS. That tells Wmdows
dows operating system files.
If you used the MORE filter, press the spacebar until you are returned to the
prompt or press O.
Key in the following: A: \::.SET U
IEnterl
Key in the following: A: \::.SET S
IEnterl
A: \~SET U
USERDNSDOMAIN=BUSINESS. SOLANO. CC. CA. US
USERDOMAIN=BUSDIVl
USERNAME=bpea t
USERPROFILE=C: \Documents and
A: \~SET S
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C: \WINDOWS
A:\~
By using the SET command with a
, all
environmental variables that began with that letter were displayed. If you wanted
to see the value of an environmental variable, you may do so with the ECHO
command, provided that you enclose the environmental variable name you are
seeking with percent signs.
Key in the following: A:
%PATH%
Key in the following: A:
%systemroot%
A: \~ECHO
C: \WINDOWS\system32 ;C:
A: \~ECHO %systemroot%
C: \WINDOWS
A:\~
IEnterl
IEnterl
, it
::::~;:
II. ~
WHA T'
I
M.I..IIVII T; U;;)I""U;;)I: I
o~;:s
H~~. ~::iM;:::::
iWffiI
see the value for the variable you requested. As you can see, the case you use does
not matter. You may also use the environmental
IEnterl
Key in the following: A: \;:.C:
Key in the following: c: \;:.CD
IEnterl
%systemroot%
A:\~C:
C: \~CD
C: \WINDOWS~
WHAT'S
HAPPENING? Instead of keying
tal variable %SYSTEMROOT%, which changed your location to the value held by
the environmental variable %SYSTEMROOT%, in this case, C: \ WINDOWS.
Key in the following: c:
IEnterl
%userprofile%
C: \WINDOWS~CD
C: \Documents and Settings\bpeat~
WHAT'S
IJJ:HHAPPENING? Again,
you used an environmental variable to change directories.
The displayed prompt represents your personal user
mental variables can be a useful shortcut.
10
Key in the following: C:\Documents
IEnterl
C: \Documents and Settings\bpeat~DIR /AR
Volume in drive C is ADMIN504
Volume Serial Number is OE38- 11FF
Directory
12/14/2001
OS/24/2001
OS/24/2001
OS/24/2001
OS/24/2001
05/06/2002
05/06/2002
12/14/2001
04/29/2002
of C: \Documents
10:0H
11:28
11:28
11:28
11:28
AM
AM
AM
AM
AM
and
Settings\bpeat
01: 40
08:10 AM
10:08 AM
02: 26
2 File(s)
7 Dir(s)
IWWMwwQW
IJJ:H
specific for this user.
Templates
PrintHood
NetHood
Application Data
024
280
-:DIR~
-:DIR~
ntuser. dat. LOG
ntuser. ini
SendTo
Recent
304 bytes
018, 441, 216 bytes free
C:\Documents and Settings\bpeat",
WHAT'S
Local Settings
-:DIR~
-:DIR~
-:DIR~
-:DIR~
-:DIR",
rp;
584
CHAPTER 11
11
ADVANCED BATCH FILES
Key in the following at your personal user prompt:
DIR " Local Settings \TEMP\'"
12/11/2001 12 : 04
12/11/2001 09:17 AM
12/12/2001 03: 07
12/12/2001 03: 12
12/11/2001 10:29 AM
12/12/2001 03: 12
12/12/2001 03 :22 PM
12/12/2001 03: 25
12/12/2001 03: 25
02/25/2002 12: 21
03/04/2002 10:44 AM
03/04/2002 10:44 AM
03/29/2002 10:04 AM
04/17/2002 11:07 AM
04/19/2002 02 : 12
04/19/2002 02: 16
04/17/2002 11:51 AM
04/19/2002 01:54 PM
04/19/2002 01: 56
04/19/2002 02:00 PM
04/19/2002 02:03 PM
04/19/2002 02:03 PM
04/19/2002 02 : 16
05/06/2002 11:28 AM
05/06/2002 11:35 AM
05/06/2002 11:28 AM
05/06/2002 11:28 AM
05/06/2002 01:40 PM
36 File (s)
0 Dir(s)
IEnterl
tmp
270
512 - DF451B. tmp
336 - WRSOOOl. tmp
512 - DFFBAA. tmp
512 - DF4401. tmp
512 - DF9DDB. tmp
278 528 - WRSOO02. tmp
512 - DF6860. tmp
512 - DF7279. tmp
512 - DF8097. tmp
154 148 -WRSOOOO. tmp
330, 970 - WRSOO03. tmp
32, 768 - WRF3237. tmp
536 - WRSOO04.
tmp
16, 384 - DFB2Cl. tmp
16, 384 - DFD062. tmp
16, 384 - DF5BI9. tmp
16, 384 - DFIDAB. tmp
16, 384 - DFFI71. tmp
16, 384 - DFD9CC. tmp
16, 384 - DF74FF. tmp
16, 384 - DF6DB2. tmp
16, 384 - DFA744. tmp
16, 384 - DF6F57. tmp
512 - DFA778. tmp
512 - DFF371. tmp
512 - DFC219. tmp
72, 704 - WRS3168. tmp
512 - DFB6BB. tmp
852, 800 bytes
018, 441, 216 bytes free
W'1"Wi1W
C:\Documents and Settings\bpeat~
WHAT' S!1'""
!&1!!fIIHAPPENING1 You
files). The TEMP directory is where Windows keeps temporary files that
posed to delete when you finish using a program. Often these files are not deleted.
Rather than having to key in a long path name with the DEL command (DEL
C:\Documents and Settings\bpeat\" LOCAL SETTINGS" \ TEMP\use the environmental variable name.
12
Key in the following:
c: \Documents and
13
%TEMP%\"'. . tmp
IEnterl
Key in the following:
c: \Documents and
%TEMP%\"'. . tmp
C: \Documents and
C: \DOCUME- l \bpeat\LOCALS- l
Access is denied.
C: \DOCUME-l \bpeat\LOCALS- l
\Temp\- DFA778. tmp
\Temp\- DFF371. tmp
Access is denied.
C: \DOCUME- l
\bpeat\LOCALS- l \Temp\- DFC219. tmp
Access is denied.
C: \DOCUME- l
J: ;i
Jiili'
TMP), you can
\bpeat\LOCALS- l \Temp\- WRS3168.
tmp
. TMP
Enter I
11.
23
585
The process cannot access the file because it is being used by another
C: \OOCUME- l
\bpeat\LOCALS- l \Temp\- DFB6BB. tmp
Access is denied.
C:\Documents and Settings\bpeat~DIR %TEMP%\Volume in drive C is ADMIN504
Volume Serial Number is OE38- 11FF
TMP
Directory of C: \DOCUME- l \bpeat \LOCALS- l \ Temp
05/06/2002
05/06/2002
05/06/2002
05/06/2002
05/06/2002
11:28
11:35
11:28
11:28
01:40
512 - DFA778. tmp
AM
AM
AM
AM
PM
134
512 - DFF3 71. tmp
512 - DFC219. tmp
144 - WRS3168. tmp
512 - DFB6BB.
5 File (s)
0 Dir(s)
tmp
136, 192 bytes
051, 315, 712 bytes free
C:\Documents and Settings\bpeat~
WHA T'SiFifjD,. QIYJ;;
HAPPENING? You
" Access Denied" or " The
used
by another process." dependon your system. You have quickly deleted the - TMP
use
by the operating system, using an environmental
process cannot access the file because it is being
ing on what is currently in
use
files that are not currently in
variable.
14
Key in the following: c:
15
Key in the following: c: \::.A:
WHAT'S
1m! HAPPENING?
IEnterl
IEnterl
You have returned to the root of C, then returned to Drive A.
om Environment in Batch
~::::~~::;::;~~~*::;:::;:X'-3?,~~~:~,%~~~:;:::~~:~::,;:.:::
You have been using the built- in environmental variables that Windows sets and
You can also set your own environmental variables, giving them both a name
uses.
and a value in a batch file as well as at the command line. Once you set the variable
it in a batch file. However, any variables that are set are only good for
that session of the Command Line window. Once you exit the command prompt,
those values are no longer available the next time you open the command prompt.
you may
use
;~;:;;:;~"~X:8:t:".:~8""~~:,::~~:,,*~~m:~;
D&J
Note:
Environment in Batch
The DATA disk should be in Drive A. The displayed prompt is A:
Close the Command Prompt window, and reopen it to begin a new DOS session.
Return to the A:\;::. prompt.
Write and save the following batch file called
OFF
ECHO %PATH%
ECHO.
~ECHO
TESTIT. BAT:
;\!;!
.~ :
~II)I
586
CHAPTER 11
ADVANCED BATCH FILES
Key in the following: A: \:.TESTIT
IEnterl
A: \~TESTIT
C: \WINDOWS\system32; C:
A:\~
WHAT'
WWnMflWr
!.1!IWlHAPPENING? The screen display created by this
in a Command Line window on your system. Notice that it did not return the word
PATH but the value stored in the environmental variable " PATH. " You can set an
environmental value and then use it in a batch file.
Key in the following: A:
Key in the following: A:
FP
IEnterl
IEnterl
ProgramFiles=C: \ Program
PROMPT=$P$G
SESSIONNAME=Console
SystemDri ve=C:
SystemRoot=C: \WINDOWS
TEMP=C: \DOCUME- l \bpeat \LOCALS- l \ Temp
TMP=C: \DOCUME- l \bpeat \LOCALS- l \ Temp
TODAY=C: \WUGXP\ * . FP
USERDNSDOMAIN=BUSINESS . SOLANO. CC
USERDOMAIN=BUSDIVl
USERNAME=bpeat
USERPROFILE=C: \Documents and
windir=C: \
A:\~
WHAT'
H:Kii1i1PWn
HAPPENiNG? You now have a value for TODAY,
ment as C: \ WUGXP\" . FP. Now, as long as you do not close the
windmv, you can use it in a batch file. When you close the Command Prompt
window, the environmental variables you set there will disappear.
Write and save the following batch file called SETTING. BAT:
DIR %today%
ECHO
Key in the following: A: \::-SETTING
Enter I
A: \~SETTING
A:\~DIR C:\WUGXP\*
Volume in drive C is ADMIN504
Volume Serial Number is OE38- 11FF
Directory of C:
10/31/2001
04:51 PM
13 7
' i1
; i~i
oil
11.
23
10/31/2001
10/31/2001
587
13 7 FILE2. FP
13 7 FILE3. FP
04:51 PM
04:51 PM
3 File(s)
0 Dir(s)
411 bytes
051, 553, 280 bytes free
A: \ ~ECHO C: \WUGXP\ *
C: \WUGXP\ * . FP
A:\~
WHAT'S
WWffl
;WF%tDX'
indicate that the value was in the environment. It substituted C: \
%today% and for %
You can use another value.
today=C:\WUGXP\*
Key in the following: A:
TMP
FP for
IEnterl
IEnterl
Key in the following: A: \:.SETTING
A: \ ~SETTING
A: \~DIR C:
. TMP
Volume in drive C is ADMIN504
Volume Serial Number is OE38- 11FF
Directory of C:
05/07/2002 07:41 AM
10/31/2001 01: 08
05/07/2002 07:41 AM
10/30/2001 02: 47
10/31/2001 11:33 AM
10/31/2001 01: 08
10/30/2001 01:46 PM
10/30/2001 01 :46 PM
10/31/2001 07: 08
9 File (s)
0 Dir (s)
190
406
190
86
152
406
148
148
478
JUPITER. TMP
MERCURY. TMP
JUP. TMP
BONJOUR. TMP
GALAXY. TMP
MER. TMP
AST. TMP
ASTRO. TMP
VEN. TMP
204 bytes
051 553, 280 bytes free
A: \:::-ECHO C:\WUGXP\* . TMP
C: \ WUGXP \ * . TMP
A:\:.
WHAT'S
JiW&J!
C:\WUGXP\* FP to C:\WUGXP\* TMP, the batch file knew to get only the value
in the environment called %
nothing.
10
Key i.; the following: A:
11
Key in the following: A:
TODAY=
IEnterl
IEnterl
A: \~SET
A:\~SET T
TEMP=C: \DOCUME- l \bpeat \LOCALS- l \ Temp
588
CHAPTER 11
TMP=C: \DOCUME- l \bpeat \LOCALSl \ Temp
A:\::WHAT'S 'h,;'
i'iB HAPPENING? You no longer have an
environmental variable would have been eliminated automatically if you had closed
and reopened the Command Prompt window.
12
Key in the following: A: \,.SET
13
Key in the following: A: \,.ECHO
TODAV=MONDAV
%TODAV%
14
Key in the following: A: \,.SET
IEnterl
IEnterl
IEnterl
A: \::-SET
A: \ ::-ECHO %
MONDAY
A: \::-SET T
TEMP=C: \DOCUME- l \bpeat \LOCALS- l \ Temp
TMP=C: \DOCUME- l \bpeat \LOCALS-l \
TODAY=MONDAY
A:\::WHAT'S
;;W;8Wi9NJj'
~iG7 You have
OOMW HAPPENII
MONDAY. You have used the variable syntax %
value of the variable. You have also used the SET T command to see any current
environment variables that begin with
15
Close the Command Prompt window.
16
Reopen the Command Prompt window, and return to the A prompt.
17
Key in the following: A: \::-SET T
IEnterl
A: \::-SET T
TEMP=C: \DOCUME- l \bpeat\LOCALSl \ Temp
TMP=C: \DOCUME- l \bpeat \LOCALS- l \ Temp
A:\::WHAT'S
liWT;WJ%;;;S
i'iB HAPPENING? The JDDAY
batch file that you can use during a DOS session.
directory to your PATH statement, but perhaps you will be doing a lot of work at
the Command Prompt using files that are in the root of the A drive. To do this by
hand would involve keying in the entire path you currently have and adding your
new directory to the end. There is an easier way to do it using the environment.
Note:
ii'
1i...1"-"'~,
The default prompt is A:
11.
23
589
18
BAT:
IF " % 1" "" GOTO END
PATH ~ OLDPATH. BAT
:TOP
PATH %PATH%;%1
SHIFT
IF NOT \%1\==\\
:END
19 Key
BAT
IEnterl
IEnterl
20 Key
A: \ ::-PATH
A: \::-ADD A: \
A:\::-IF "A:\"
A: \::-PATH
GOTO
END
bOLDPATH. BAT
A: \::-PATH
C: \WINDOWS\system32; C:
A: \::-SHIFT
A: \::-IF
NOT
A:\::W;!WiWUf2"
WHAT'S
, you saved it to a file called
ORIGPATH. BAT. Youthen used your new batch file , ADD. BAT, and added the A:\
root directory to the path. You can add more than one directory.
i'iB HAPPENING? To preserve
21 Key
Enter I
IEnterl
22 Key
i A: \::-ORIGPATH
! A: \::-PATH=C:
A: \::-ADD A: \ ;A: \OLDER;A: \CLASS
A: \::-IF "A: \"
; A:\::-PATH
GOTO
END
1 ::-OLDPATH. BAT
A: \::-PATH C: \WINDOWS\system32;C:
A: \::-SHIFT
: A: \::-IF
NOT
\A: \OLDER\ == \ \
GOTO
A: \::-PATH C: \WINDOWS\system32; C:
A: \::-SHIFT
TOP
1111
590
CHAPTER 11
A:\::-IF NOT
A: \::-PATH
C: \WINDOWS\system32 ;C:
A: \::-SHIFT
A:\::-IF NOT
A:\::WHA T'SmF,%!,k
i'iB HAPPENING? You have
23 Key
IEnterl
A: \::-PATH
A: \::-PATH
PATH=C: \WINDOWS\system32 ;C: \WINDOWS;C:
A:\::WHAT' S W!%HTW%
OOMWHAPPENING? You keyed in
return to your original path, you created ORIGPATH. BAT.
24 Key
IEnterl
A: \
A: \::-PATH=C: \WINDOWS\system32 ;C:
A:\::WHAT'S
i'iB HAPPENING?
You have returned to the original path.
~'o:~"8?~~:;.::~~~o:::..';~;wm:::8.,,;:':.;:.:::.
IIEm
As has been discussed , the environment is an area that is set aside in memory. In
addition to being able to place and use variables in the environment, you can preset
DIR command parameters and switches by including the SET command with the
DIRCMD environmental variable. Keying in SET by itself will tell you what is in the
environment. You can use the DIRCMD variable and ERRORLEVEL to write a batch
file that will allow you to change the way DIR displays information for the current
command prompt work session.
:%:8%%:'X:~::':'
:~;)~:%:~;:::;:'~::::~::~~::::"'K.
11m
Note:
The DATA disk should be in Drive A. The displayed prompt isA:\;::.,
Create the following batch file called MY. BAT:
(g)ECHO OFF
CLS
11.
25
591
ECHO.
ECHO.
ECHO How do you want your
ECHO.
ECHO
ECHO
ECHO
ECHO
ECHO
ECHO.
Files only arranged by file name.
Files only arranged by file name.
Files only arranged by file extension.
Files only arranged by file extension.
Directory displays in default mode.
ECHO PLEASE SELECT A NUMBER.
ECHO.
REPLY
ECHO.
IF
IF
IF
IF
IF
ERRORLEVEL
ERRORLEVEL
ERRORLEVEL
ERRORLEVEL
ERRORLEVEL
WHAT' S
MnitMH!iH!!
lliiiiWHAPPENING7
49
50
51
52
53
IF
IF
IF
IF
IF
N /AE /A-
You have created a batch file to set the DIRCMD environmental
variable.
Key in the following: A: \,.MY
IEnterl
How do you want your directory displayed?
Files only arranged by file name.
Files only arranged by file name.
Files only arranged by file extension.
Files only arranged by file extension.
Directory displays in default mode.
A
Z
A
Z
to
to
to
to
Z
A
Z
A
PLEASE SELECT A NUMBER.
WHAT'S
lliiiiW
files displayed. You want your files displayed in file extension order in descending
order (Z-A).
Key in the following: 4
Key in the following: A:
IEnterl
How do you want your directory displayed?
Files only arranged by file name.
Files only arranged by file name.
Files only arranged by file extension.
Files only arranged by file extension.
Directory displays in default mode.
A
Z
A
Z
to
to
to
to
Z
A
Z
A
''''' . ...
592
CHAPTER 11
ADVANCED BATCH FILES
PLEASE SELECT A NUMBER.
A: \ ::-SET D
DIRCMD=/O- E
/A-
A:\::WHAT' S
"'ii','"'''
~HAPPENI NG7
, but you can see that you did
indeed set an environmental variable. Now, during this Command Line session
whenever you key in DIR, it will automatically arrange the files by extension in
reverse order. You can see that you have
DIRC
0 equ
Key in the following: A: \,.DIR
CLASS
IEnterl
A: \::-DIR
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory of A:
05/07/2002
10/31/2001
"
i
;
i
07:41 AM
01:08 PM
07:08 PM
10/31/2001
10/30/2001
10/30/2001
10/31/2001
190
406
478
148
01: 46
01: 46
01: 08
05/07/2002
10/31/2001
10/31/2001
10/31/2001
10/31/2001
10/31/2001
10/30/2001
05/07/2002
JUP.
MER,
VEN,
AST.
PAR
PAR
PAR
PAR
148 JUP, FIL
406 MER. FIL
190 JUP. BUD
406 MER. BUD
07:41 AM
01: 08
04: 51
07: 08
13 7 AST.
478
406
478
148
190
01:08 PM
07:08 PM
01:46 PM
07 :41 AM
14 File(s)
VEN. BUD
MER, ABC
VEN
AST
JUP. ABC
209 bytes
895, 488 bytes free
0 Dir (s)
A:\::WHAT'
;,!!!!gW!!!:nx
~ HAPPE"NING7
The files are arranged by file extension in reverse alphabetical
order. Until you change the values, or close this Command Prompt session, every
time you issue the DIR command it will display file names in reverse alphabetical
order by file extension.
Key in the following: A: \,.MY
IEnterl
Press 5
Key in the following: A: \,.SET
IEnterl
How do you want your directory displayed?
Files only arranged by file name.
A to Z
11, 26 THE FOR... IN... DO COMMAND
Files only arranged by file name.
Files only arranged by file extension.
Files only arranged by file extension.
Directory displays in default mode,
Z to A
A to Z
Z to A
PLEASE SELECT A NUMBER.
A: \::-SET D
Environment variable D not defined
A:\::WHAT'S miWiWWW
OOMWHAPPENING? You
default value. DIRCMD is no longer defined.
*;::;:::;::S:-&1;:''''::::::i~::~;:~:m~~,~0i8;'~""~,,::,,,
I:I:m The FOR.. .lN... DO Command
The FOR... IN... DO command can be issued at the command line or placed in a batch
file. This command allows repetitive processing. FOR allows you to use a single
command to issue several commands at once. The command can DO something
FOR every value IN a specified set. The basic syntax at the command line is:
FOR %variable IN
%variable
parameters)
Specifies a replaceable parameter.
Specifies a set of one or more
(set)
may be used.
command
Specifies the command to carry out for each
command- parameters
file.
Specifies parameters or switches for the
specified command,
To use the FOR command in a batch program , specify %%variable
instead of %variable.
The FOR command was greatly expanded with the release of Windows
sional. For full details, see Appendix H or key in FOR /? at the prompt.
The batch file variable is an arbitrary single letter. The double percent sign with a
letter (%%a) distinguishes the batch file variable from the replaceable parameter
(%1). The difference between a variable and a parameter is not complicated.
FOR statement tells the operating system to get a value from the set you have
chosen. After it executes the command that appears after DO, the FOR command
looks for the next value in the set. If it finds another value, %%a will represent
something new, and the command will be executed with the new value. If there are
no more values in the set, the FOR command stops processing.
loop, you can consider the
loop. You do not need to use the letter a, You may use
any letter-a , c, x , etc. The parameter value , on the other hand, is set before the
batch file begins processing. Remember, the operating sy~tem gets the value from
the position in the command line. The set is always enclosed in parentheses. The
values in the set, either data or file names, will be used to DO some command. The
If you consider the GOTO label as a
FOR... IN... DO
as a
horizontal
vertical
593
' :::
594
CHAPTER 11
ADVANCED BATCH FILES
items in the set must be separated by spaces or commas,
wildcards in a set.
om FOR... IN... DO Command
%~b:::':::';:::':':;';';
.:N:~~!:f.%~;t:;~-
The DATA disk should be in Drive A. The displayed prompt is
Note
1:
Note
2: Look at
, the
command says: Using the variable %a to hold each value in the set (what is
in parentheses), do the command (TYPE) to
Key in the following: A: \,.FOR
A: \::-FOR %a IN
ABC) DO TYPE %a
IEnterl
ABC) DO TYPE %a
A: \::-TYPE VEN . ABC
The planet Venus comes physically closer to us
than any other planet in the solar
The mass of the Venus atmosphere is 96% carbon
not a human- friendly environment! Carbon dioxide makes
up less than 1% of our terrestrial
Venus rotates very slowly and seems to have only
one tectonic plate. "Venusquakes" happen mush less
frequently than Earthquakes.
No moon- lit nights on the planet named for
the goddess of love - Venus has no
A:\::-TYPE JUP. ABC
The planet Venus comes physically closer to us
than any other planet in the solar
The mass of the Venus atmosphere is 96% carbon
not a human- friendly environment! Carbon dioxide makes
up less than 1% of our terrestrial
Venus rotates very slowly and seems to have only
one tectonic plate. "Venusquakes" happen mush less
frequently than Earthquakes.
No moon- lit nights on the planet named for
the goddess of love - Venus has no
A:\::WHAT' S ;WZ#'#'"
WBHAPPEN~ING7
FOR... IN... DO processed every item in the set as indicated below,
Besides using wildcards, you can also be specific.
Key in the following:
A: \ ,.FOR %x IN (VEN. ABC NOFILE. EXT D. BA
T)
Enter I
Key in the following:
A: \,.FOR %y IN (VEN. ABC, NOFILE. EXT,
BAT) DO TYPE %y lEnterl
11,
ACTIVITY: USING THE FOR... IN... DO COMMAND
A:\::-FOR %x IN (VEN. ABC NOFILE. EXT
D. BAT) DO TYPE %x
A: \::-TYPE VEN
The planet Venus comes physically closer to us
than any other planet in the solar
The mass of the Venus atmosphere is 96% carbon
not a human- friendly environment! Carbon dioxide makes
up less than 1% of our terrestrial
Venus rotates very slowly and seems to have only
one tectonic plate. "Venusquakes" happen mush less
frequently than Earthquakes.
No moon- lit nights on the planet named for
the goddess of love - Venus has no moon.
A:\::-TYPE NOFILE. EXT
The system cannot find the file
A: \::-TYPE D. BAT
DIR lAD
A:\::-FOR %y IN (VEN. ABC, NOFILE, EXT,
BAT) DO TYPE %y
A: \::-TYPE VEN
The planet Venus comes physically closer to us
than any other planet in ' the
The mass of the Venus atmosphere is 96% carbon
not a human- friendly environment! Carbon dioxide makes
up less than 1% of our terrestrial
Venus rotates very slowly and seems to have only
one tectonic plate. "Venusquakes" happen mush less
frequently than Earthquakes.
No moon- lit nights on the planet named for
the goddess of love - Venus has no moon.
A: \~TYPE NOFILE , EXT
The system cannot find the file
A: \::-TYPE D. BAT
DIR lAD
A:\::WHAT'
",MN!/Z"
&mill!
lines. First, both a space and a comma between items in a set work the same way.
Second, the variable letter you choose is not important. In the first case, x was
chosen-in the second, y. This command line is case sensitive. If you had
FOR %b IN (VEN. ABC NOFILE. EXT D. BAT) DO T.YPE %B, the difference between b and B would have made the command line invalid. Even when there was
an invalid file (NOFILE. EXT), the command line continued processing the other file
names in the command. You did not need to worry about testing for null values.
595
"~ ,'. \\.j
J ,
I ;
1!
' . I
596
CHAPTER 11
ADVANCED BATCH FILES
This command works the same when placed in a batch file, only you must use
However, it appears that this works no differently than had you keyed in TYPE
VEN. ABC NOFILE. EXT.
VEN. ABC NOFILE. EXT
Key in the following: A: \,.TYPE
A:\::-TYPE VEN, ABC
IEnterl
NOFILE. EXT
VEN . ABC
The planet Venus comes physically closer to us
than any other planet in the solar
The mass of the Venus atmosphere is 96% carbon
not a human- friendly environment! Carbon dioxide makes
up less than 1% of our terrestrial
Venus rotates very slowly and seems to have only
one tectonic plate. "Venusquakes" happen mush less
frequently than Earthquakes.
No moon- lit nights on the planet named for
the goddess of love - Venus has no moon.
The system cannot find the file specified.
Error occurred while processing: NOFILE.
A:\::WHAT'S
W!WJ!HAPPENING? You see an
can use a test to test for an existence of a file in conjunction with the FOR... IN...
so that the TYPE command will only display the files it finds and will not
any error messages.
Key in the following: A: \,.
FOR %a IN (VEN. ABC. NOFILE) DO IF
A:\::-FOR %a IN (VEN. ABC, NOFILE) DO IF EXIST %a TYPE %a
A: \::-IF
ABC
The planet Venus comes physically closer to us
than any other planet in the solar
The mass of the Venus atmosphere is 96% carbon
not a human- friendly environment! Carbon dioxide makes
up less than 1% of our terrestrial
Venus rotates very slowly and seems to have only
one tectonic plate. "Venusquakes" happen mush less
frequently than Earthquakes.
No moon- lit nights on the planet named for
the goddess of love - Venus has no moon.
llj:
MJi.i~(
%a
IEnterl
.-- - -..-
11,
27
IN... DO COMMAND
I ::~:~F EXIST NOFILE TYPE NOFILE
WHAT'S
W0!f!)
error message. You can also test for character strings
Create and save the following batch file called DO. BAT and key in the following:
FOR %%v IN (Patty Nicki Sandy
Key in the following: A:
IEnterl
A:\::-DO
A: \::-FOR %v IN
A: \::-ECHO
Patty
A: \ ::-ECHO
Nicki
A: \ ::-ECHO Sandy
Sandy
A: \ ::-ECHO
Brian
A:\::WHAT'S
, the ECHO command was carried out for each item
in the set. It substituted each value (Patty, Nicki, Sandy, and Brian) for the replaceable parameter in the ECHO command. In this example, spaces were used to
separate the values , but you could have also used commas. The advantage to using
this command is that you do not have to write it with a command on each line, as in:
ECHO Patty
ECHO Nicki
ECHO Sandy
ECHO Brian
Another advantage of the FOR... IN... OO method is that you can set values in the
environment and then use them in a batch file.
OOMW
Write and save a batch file called PASS. BAT and key in the following:
FOR %%a IN
:NO
%1"== %%a " GOTO OKAY
IEnterl
ECHO You, %1, are NOT allowed in
IEnterl
GOTO END
:OKAY
Enter I
ECHO Welcome, %1, to my world of
:END
Enter I
Enter I
IEnterl
597
. "
::I!:
~ : ~l '
598
CHAPTER 11
ADVANCED BATCH FILES
WHAT'S
i'iBHAPPENING? You have combined several features in this FOR..
ment. You have used an environmental variable in the set
IN.. DO state-
signs surrounding the value tell the FOR command to use the environmental
variable called
is in the environment, then it is a true statement and the batch file will go to the
:OKAY label. If what the user keys in is false and not equal to the environmental
variable, then the batch file falls through to the next line. First, you need to set the
environmental variable. (Use upper and lower case exactly as shown.
USERS=Carolyn, Bette
Key in the following: A: \,.SET
10
Key in the following: A:
Bette
IEnterl
IEnterl
A: \::-SET USERS=Carolyn, Bette
A: \ ::-PASS Bet te
A: \::-FOR %a IN
A: \::-IF "Bette"
A: \::-IF "Bette"
Bette" GOTO OKAY
A: \::-ECHO Welcome Bette
Welcome Bette to my world
of
A:\::-
of
computers,
computers.
WHAT'S
OOMW
the PASS. BAT batch file. It worked as directed
What if it were false?
11
Key in the following: A: \,.PASS
lEnterl
A:\::-FOR %a IN
A:\::-IF "Denzel"
Carolyn" GOTO OKAY
A: \::-IF "Denzel"
A: \::-ECHO You,
You, Denzel, are NOT allowed in the system.
A: \::-GOTO
END
A:\::WHAT'S
IffiIImHAPPENING7 The
FOR.. IN.. DO can also be used with replaceable parameters
, file names, and
wildcards. You are going to take another look at UPDATE.
12
ili",
UPDA TE. BA T file so it looks as
follows, Be sure you include two percent signs preceding "
Use any text editor and edit and save the
11.
27
.IN... DO COMMAND
: DOlT
IF " %1" "" GOTO
FOR %%v IN (%
SHIFT
PAUSE
GOTO DOlT
:END
WHAT'S #n#;n(Wfi
lli\IW\
command line. There can be a problem with this batch file. As written, this batch file
will copy the newly updated files to the current default directory. Thus,
idea, in general, to place all your batch files in a subdirectory called BATCH and set
your path to include the BATCH directory. If you do not have the BATCH directory
on your root of your DATA disk, create it
IEnterl
13 Key
A: \ ::-ADD A: \BATCH
A: \::-IF "A:
A: \::-PATH
1 ::-OLDPATH. BAT
A: \::-PATH
C: \WINDOWS\system32 ;C:
A: \::-SHIFT
A: \::-IF
A:\::WHAT' S
Wf,'iiPtW";PWi
NiNG7 You have added the BATCH subdirectory to your current path,
Now you will move all the batch files, as well as the
subdirectory.
14 Keyinthefollowing:A:\,.MOVE . BAT BATCH IEnterl
lli\IW\ HAPPE'
15 Key
16 Key
A: \DCOMP. BAT
A: \ONE. BAT
A: \ALPHA. BAT
A: \ ERROR . BAT
A:\SIZE. BAT
A: \GREET . BAT
A: \RENDIR. BAT
A: \KEYING. BAT
A: \TESTIT .
A: \SETTING. BAT
A: \ADD. BAT
A: \ORIGPATH. BAT
A: \OLDPATH. BAT
A: \MY . BAT
Enter I
BAT IEnterl
599
CHAPTER 11
600
ADVANCED BATCH FILES
A: \DO. BAT
A: \PASS . BAT
A: \UPDATE. BAT
A:\b. bat
A: \TEST. BAT
A: \EXAMPLE. BAT
A: \D. BAT
A: \S . BAT
A:\BOG. BAT
A: \N. BAT
A: \log . bat
A: \::-MOVE REPLY. *
A: \REPLY. SCR
A: \REPLY. COM
A: \::-COPY BATCH\ADD, BAT
1 file (s) copied.
A:\::WHAT'
Wtiff!lWVY
HAPPENING? You have moved all
and you included the REPLY
grouping allowed you to clean up the root directory of the
that you can use your batch files , you first added the BATCH subdirectory to the
PATH statement. Then you copied the ADD.
BAT file to the root.
CAUTION: If you close the Command Prompt window
, you will have to issue the
following command to include the A:\BATCH directory in your path:
A:\ADD A:\BATCH
17
18
Key in the following: A: \,.DIR
Key in the following: A: \,.DIR
SWT
iEnter)
CAP
IEnterl
A: \::-DIR * , SWT
Volume in drive A is DATA
Volume Serial Number is 30B8-
DA1D
Directory of A:
10/31/2001
10/31/2001
04:50 PM
04:50 PM
138 FILE2. SWT
138 FILE3. SWT
2 File (s)
0 Dir (s)
276 bytes
893, 440 bytes free
A: \::-DIR * . CAP
Volume in drive A is DATA
Volume Serial Number is 30B8-DA1D
Directory of A:
07/31/2000
04/23/2002
04/23/2002
04 :32 PM
01: 01
01:04 PM
3 File (s)
0 Dir (s)
A:\::-
t""
260 STATE. CAP
260 SORTED. CAP
260 BYCITY. CAP
780 bytes
893 440 bytes free
11,
27
IN... DO COMMAND
WHAT'S!/,,,;
8* HAPPENING? You can see the
them to the current date.
CAP IEnterl
SWT
Keyinthefollowing:A:\::-UPDATE
19
A:\::-UPDATE * SWT * CAP
. SWT" ==
A: \::-IF
A:\::-FOR %v IN (* SWT) DO COpy %v /b +
l::-NUL
l::-NUL
A: \ ::-COPY
l::-NUL
A: \ ::-COPY
A: \::-SHIFT
A: \::-PAUSE
Press any key to continue
WHAT'
, which it got
SWT). Each file that had a . SWT file extension
NWiKWii/W\
ffi!\W
from the command line (%1 was *
was updated. Now the batch file is going to SHIFT and process
set (* CAP).
20
Keep pressing
21
Key in the
IEnterl until you are back at the command prompt.
SWT * . CAP fEnterl
following: A: \,.DIR
A:\::-DIR * SWT *
CAP
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory
05/07/2002
05/07/2002
Directory
05/07/2002
05/07/2002
05/07/2002
of A:\
08:44 AM
08 :44 AM
138 FILE2. SWT
138 FILE3. SWT
of A:\
08:45 AM
08:45 AM
08:45 AM
5 File (s)
, Dir (s)
260 STATE. CAP
260 SORTED. CAP
260 BYCITY. CAP
056 bytes
893, 440 bytes free
A:\::WHAT'S ,;,,
lliDHAPPENING? Your dates
, but you have successfully changed the
dates of the files using the FOR.. IN.. OO command.
601
~! I.' f
:i
602
CHAPTER 11
~,;8:~'E':~8:':';::~W-:::'tx=,:";:::
:~'~i:;:;::.::',:,:::,::::;:;
lIB) FOR...
IN... DO Command
Some of the features in of the FOR... IN... DO command are that you may list envi-
ronmental variables so
, you may
use the special tilde operator (-) to perform such tasks as stripping a file name of
quotation marks and to expand a variable, and you may also use the /R parameter,
The /R parameter is a recursive parameter.
Recursive
means that the command will
search and perform actions on all subdirectories beneath it. You may also
specific text from ASCII files.
: ":;:;,:~,::::::::::8:':::::0.:::8~~:~
lIB) of the FOR..
.lN... DO Command
Key in the following: A: \,.SET
IEnterl
A: \ ::-SET
PATHEXT= COM; . EXE; . BAT; . CMD; . VBS; . VBE; . JS; , JSE; .
WSF; . WSH
A:\::WHAT'S
IWW!
Professional has associated with the application programs on your system. Your list
may be different, depending on the software in,stalled
separated by semicolons and appears on one line, sometimes making it difficult to
read. You can use FOR... IN... DO to display the list one line at a time.
Key in the following:
A: \ ,.FOR %a IN
A: \::-FOR
IEnterl
%a IN
. COM
EXE
. BAT
CMD
VBS
VBE
JSE
WSF
WSH
A:\::WHAT'S ,. '~\mWiXk
i'iB HAPPENING?
in their names provide certain challenges at the command line as well.
files,
ill,
,*
11.
~
"
. .,.;, ~!
.. **
29
./N... DO COMMAND
Key in the following: A: \,.FOR
MERCURV. TXT)
DO
(g)ECHO %a
IN
603
Sandy and Patty. txt"
IEnterl
Key in the following: A: \,.FOR
IN
Sandy and Patty. txt"
MERCURV. TXT)
DO
(g)ECHO %"' IEnterl
A:\::-FOR %a IN
Sandy and Patty. txt"
txt"
MERCURY. TXT) DO
txt"
MERCURY. TXT) DO
MERCURY. TXT
A:\::-FOR %a IN
Sandy and Patty. txt
MERCURY. TXT
A:\::WHAT'S
ENiNG? In the second command, you added a tilde prior to the variable
, a.
This stripped the file name of its quotation marks. When you want to add a prefix or
!!1!M!i! HAPP'
suffix to a long file name, you need to use quotation marks. However
, if, in the set
you use quotation marks , when you try to rename a file, you will end up with extra
quotation marks.
Key in the following: A: \,.FOR
IN
Sandy and Patty. txt"
MERCURY. TXT)
DO
(g)REN %a " CZG %a IEnterl
Key in the following: A: \,.FOR
IN
Sandy and Patty. txt"
MERCURY. TXT)
DO
(g)ECHO %"' IEnterl
Key in the following: A: \,.DIR CZG* Enter I
A:\::-FOR %a IN
The syntax of the command is
A:\::-FOR %a IN
Sandy and Patty. txt
txt" ,
MERCURY. TXT) DO
txt" ,
MERCURY. TXT) DO
MERCURY. TXT
A: \::-DIR CZG*
Volume in drive A is DATA
Volume Serial Number is 30B8- DA1D
Directory of A:
10/31/2001
01: 08
1 File (s)
0 Dir(s)
406 CZG MERCURY. TXT
406 bytes
893 440 bytes free
A:\::-
, L.
I' ~
"Iff
, lu
WHAT'S
'1\!
i'iBHAPPENING? The file name
the syntax was incorrect. You had too many quotation marks.
Key in the following: A: \ ,.DEL CZG*
IEnterl
;Jf
1;':
~ ' ."'
%"'
.*
;:'
' :
604
CHAPTER 11
ADVANCED BATCH FILES
Key in the following (do not press
IEnterl until you see
IN (" Sandy and Patty. txt" ,
A: \,.FOR %a
IEnterl
Brian
IEnter)
JUPITER. TXT)
DO REN %a
A: \::-DEL CZG*
A: \::-FOR %a IN
Sandy and Patty.
A:\::-REN " Sandy and Patty. txt'"
"
txt" ,
JUPITER. TXT)
Brian %-a"
Brian Sandy and Patty. txt"
A: \::-REN JUPITER. TXT " Brian JUPITER.
A:\::-
WHAT'S
1Xg;;;;W;;\
i'iBHAPPEr'JING? Because you stripped out
fully rename your files. In step 10, overwrite the files
10
Key in the following: A: \::-COPY
11
Key in the fOllowing:
A:\,.FOR IR %a
12
C:\WUGXP\*
IN (* SANDY*
DO
TXT TRIP
IEnterl
lEnterl
Key in the fOllowing:
IN (*SANDY*
A:\,.FOR IR %a
) DO (g)ECHO
A:\::-COPY C:\WUGXP\* TXT TRIP
C: \ WUGXP
C: \ WUGXP \ BORN
C: \WUGXP\DANCES. TXT
C: \ WUGXP\HELLO. TXT
C:\WUGXP\Sandy and Patty.
C: \WUGXP\TITAN . TXT
txt
C: \ WUGXP\Sandy and
C: \WUGXP\LONGFILENAME. TXT
C: \ WUGXP\JUPITER. TXT
C: \ WUGXP\LONGFILENAMED . TXT
C: \ WUGXP \ GALAXY . TXT
C: \ WUGXP \ LONGF ILENAM ING . TXT
C: \ WUGXP \ MERCURY , TXT
C: \WUGXP\PLANETS. TXT
C: \ WUGXP\ VENUS. TXT
IS file
A:\::-FOR /R
A: \Brian
DO
A: \Sandy and
A:\TRIP\Sandy and Patty. txt
A: \TRIP\Sandy and
A: \OLDER\Sandy and
A: \OLDER\Sandy and
A:\::-FOR /R
Brian Sandy and Patty. txt
Sandy and Nicki. txt
Sandy and Patty. txt
Sandy and Nicki. txt
Sandy and Patty.
.I
, if necessary.
DO
nxa IEnterl
%%"'
11.
" "
IN... DO COMMAND
29
sandy and Nicki. t
A:\::WHAT'S
WNII!
files that contained " SANDY" in the filename, The /R parameter
directories on your disk. However, it showed you the entire path name. If you want
to use commands such as REN ,
you need
only
the file name. The options preceded
forces the variable to expand to only the file
forces the expansion only of the file extension. You can use
name whereas the
these features to write a batch file that will allow you to precede any file name with
any prefix you wish.
by the tilde allowed you to do so. The
13
Remember that in a batch file, variable names need to be preceded by two
percent signs. Use your name instead of " YourNameHere.
(g)ECHO OFF
REM VourNameHere
REM Purpose of batch file is to add a new prefix to any file name.
"" GOTO
IF U %1"
IF " %2"== u" GOTO MESSAGE2
%1 %%"' nxa
FOR IR %%a IN
GOTO END
:MESSAGE
ECHO You must include a prefix you wish to use.
ECHO Syntax is
GOTO END
:MESSAGE2
ECHO You must include a file name you wish to rename.
ECHO Syntax is
:END
BAT to
14 Be
include it.
15 Be
A: \ ,.DIR Sandy
A: \::-DIR Sandy* .
IS
Enter I
* /S
Volume in drive A is DATA
Volume Serial Number is 30BB- DAID
Directory
11/16/2000
Directory
11/16/2000
11/16/2000
of A:\
12:00 PM
1 File (s)
53 Sandy and Nicki. txt
53 bytes
of A: \TRIP
12:00 PM
12: 00
2 File (s)
59 Sandy and Patty. txt
53 Sandy and Nicki. txt
112 bytes
605
606
CHAPTER 11
ADVANCED BATCH FILES
Directory of A:
11/16/2000
11/16/2000
12: 00
59 Sandy and Patty. txt
53 Sandy and Nicki. txt
12:00 PM
2 File (s)
112 bytes
, Total Files
5 File(s)
0 Dir(s)
277 bytes
200 bytes free
883
A:\::WHAT'S
i'iB HAPPENING? You have
names, one in the root of A and the others in a subdirectory called TRIP and the
others in the OLDER directory. You are going to modify those files so that their
names will be preceded by
16
Key in the following: A: \,.PREFIX
17
Key in the following: A: \,.DIR
SANDY.
18
Key in the following: A: \,.DIR
RBp.
A: \::-PREFIX
RBP
IEnterl
RBP SANDY.
IS
IS
IEnterl
Enter I
SANDY *
A: \::-DIR
Volume in drive A is DATA
Volume Serial Number is 30B8- DAID
File Not Found
A: \::-DIR RBP* /S
Volume in drive A is DATA
Volume Serial Number is 30B8- DAID
Directory of A:
11/16/2000
12:00 PM
1 File (s)
Directory of A:
11/16/2000
11/16/2000
12: 00
12: 00
2 File (s)
53 RBP Sandy and Nicki.
53 bytes
59 RBP Sandy and Patty.
53 RBP Sandy and Nicki.
112 bytes
Directory of A:
11/16/2000
11/16/2000
12:00 PM
12:00 PM
2 File (s)
59 RBP Sandy and Patty.
53 RBP Sandy and Nicki,
112 bytes
Total Files Listed:
5 File (s)
0 Dir(s)
A:\::-
277 bytes
883, 200 bytes free
~~,
\::-
11,
29
.lN... DO COMMAND
WHAT'S ",;a;w!w;;;.,
i'iB HAPPENING? You successfully renamed
specific fields in a text file. The FOR command also allows you to use the IF parameter. The IF parameter allows you to extract specific data from a text file. It lets you
set the rules by which you will extract the data. The basic syntax is:
FOR IF
USERBACKQ=option TOKENS=list"
%%variable IN (" set"
DO command
Using USERBACKQ, you may specify what
do not specify a delimiter, then spaces or tabs are used. The TOKENS is a series of
numbers telling the command which token on the text line is to be
%%variable.
19
(g)ECHO OFF
FOR
IF
tokens=1,
7~1
20 Key
IEnter)
21 Key
FIL
Golden
Chang
Brogan
Brogan
Babchuk
Babchuk
ROdriguez
Helm
Suzuki
Markiw
Markiw
Jane
Wendy
Lloyd
Sally
Nicholas
Bob
Mil ton
Charlene
Nicholas
Smi th
Steven
David
Babchuk
Babchuk
Deana
JONES
JERRY
Jones
Perez
Ophelia
Ervin
Sergio
Wal ter
Jones
Cleo
Gonzales Antonio
Yuan
Markiw
Peat
Farneth
Lakeview
Edgewood
Miller
Miller
Stratford
Stratford
Bianca
Emily
Nyles
John
Nyles
Sophie
Markiw
Nick
Washingon Tyrone
Jones
345
356
111
111
20 Elm
333 Meadow
567 Abbey
354 Bell
10
Zion
12 Brooks
12 Brooks
10
Zion
345 Newport
32 North
120 Collins
12 View
12 View
355 Second
40 Northern
244 East
1213 Wick
15 Fourth
134 Seventh
Suelin 56 Twin Leaf
Nicholas 12 Fifth
Brian
123 Second
Nichole 456 Stage
Orange
Ann Arbor
Santa Cruz
Santa Cruz
Sun City West
Sun City West
Ontario
Sherman Oaks
tEnter)
Nurse
Librarian
Vice-President
Account Manager
Professor
Professor
Systems Analyst
Consul tant
Rochester
Day Care Teacher
Sun
Sun
Sun
Sun
Retired
Retired
Retired
Retired
Phoenix
City
City
City
City
West
West
West
West
Orange
Phoenix
Orange
Thousand Oaks
Thousand Oaks
Ann Arbor
Ontario
Mission Viejo
Phoenix
Santa Cruz
Ann Arbor
Engineer
Manager
Buyer
Chef
President
Housewife
Clerk
Engineer
Systems Analyst
Wri ter
Banker
Orange
tor
Artist
Vacaville
Athlete
Glendale
Davis
Edi
Engineer
Dancer
A:
WHAT'S
iim!!mHAPPENING? (Some of
U!:
"f;
creen. ) Here you have a text
file. In the batch file you just wrote
, you are going to use the IF option. The tokens
that you specified are in the first, second
, and seventh positions. In other words, you
want the first name, last name, and profession extracted from this file. The last name
607
,.,
608
CHAPTER 11
ADVANCED BATCH FILES
will be a, the first name will be b , and the profession will be c. These are assigned by
the FOR command. You will display them so that you see first name, then last name
then profession.
22 Key
Enter I
Carolyn Smith, MI
Jim Winter, CA
Linda Winter, CA
Tai Tran, CA
Jane Golden , Nurse
Wendy Chang, MI
Lloyd Brogan, CA
Sally Brogan, CA
Nicholas Babchuk, West
Bianca Babchuk, West
Bob Rodriguez, Systems
Milton Helm, CA
Charlene Suzuki, Day
Nicholas Markiw, Engineer
Emily Markiw, West
John Nyles, West
Sophie Nyles, West
Nick Markiw , West
Tyrone Washingon, Manager
Steven Jones, Buyer
David Smith, Chef
Wal ter Babchuk , CA
Deana Babchuk, CA
Cleo Jones, MI
Antonio Gonzales, Engineer
JERRY JONES, CA
Ophelia Lo, Writer
Ervin Jones, CA
Sergio Perez, MI
Suelin Yuan, CA
Nicholas Markiw, Engineer
Brian Peat , Athlete
Nichole Farneth, Dancer
A:\::WHA 1'
M!8%i%!iM!
m1I!HAPPENING1 As you can see,
ever, they are not all the fields you wanted. You can make your batch file more
sophisticated by creating the output in sorted order.
23
BAT file in the BATCH directory as follows:
(g)ECHO OFF
SORT
FOR
IF
tokens=1, 7" %%8 IN
DEL %2
24 Key
Wendy Chang, MI
Nichole Farneth, Dancer
Carolyn Gillay, Professor
1: .
FIL TEMP. FIL IEnterl
~~---11.
29
IN... DO COMMAND
609
Jane Golden , Nurse
Antonio Gonzales , Engineer
Milton Helm, CA
Cleo Jones , MI
Ervin Jones, CA
JERRY JONES , CA
Steven Jones , Buyer
Ophelia Lo, Writer
Emily Markiw , West
Nicholas Markiw , Engineer
Nicholas Markiw , Engineer
Nick Markiw , West
Kathryn Maurdeff, MI
Sonia Maurdeff , MI
John Nyles , West
Sophie Nyles, West
Frank Panezich , Teacher
Brian Peat , Athlete
Sergio Perez , MI
Bob Rodriguez , Systems
Carolyn Smith , MI
David Smith , Chef
Gregory Smith, MI
Charlene Suzuki , Day
Tai Tran, CA
Steven Tuttle , CA
Tyrone Washingon, Manager
Jim Winter , CA
Linda Winter , CA
Suelin Yuan , CA
A:\::WHA 1' 5
wwniiiwqW
~HAPPE' NING1 You have a sorted list of people and their professions. However
there is still a problem. Many of the lines have the state listed instead of the profession. The batch file you wrote looks at a space as a delimiter. If you looked at the
original data, the seventh position varies, depending on the number of words in the
address.
DATA Yuan
Suelin
Twin Leaf
Orange
Artist
Pes! ticn
The problem here is the data. You can alter the data and choose a delimiter that
will set off the fields as actual fields.
25
Open the text editor. Split the screen. With the cursor in the top screen, open
PERSONAL. FIL.
Save the new file as
Select the first five lines. Copy them into the bottom screen.
SHORT. FIL.
1,;1
Y;'
610
CHAPTER 11
WHAT'S wn;wmlnb
You have a smaller sample of data.
HAPPENING?
26
Exit Edit and key in the following:
SHORT. FIL TEMP. FIL IEnterl
A: \:.PERSON
Carolyn Gillay, Professor
Kathryn Maurdeff, MI
Sonia Maurdeff, MI
Frank Panezich, Teacher
Steven Tuttle, CA
A:\
WHAT'S VtF0HiftM
OOJlftjjHAPPENiNG?
edit the file, using semicolons to separate the fields.
27
Edit the
SHORT. FIL file as follows:
Gillay; Carolyn; 699 Lemon;Orange;CA; Professor
Panezich;Frank;689 Lake;Orange;CA;Teacher
Tuttle;Steven;356
Maurdeff; Kathryn; 550 Traver; Ann Arbor; MI;Teacher
Maurdeff;Sonia;550 Traver;Ann Arbor;MI;Student
Note:
A simple way to do this is to place a semicolon at the end of each field, as
shown below
then use Replace on the Search menu to replace a space with nothing,
'
';,~
: :::.: .:::~: . . .~':::::~:"::;,::,:,~::
:",:;::~~.
11. 30 THE
-~~..-~~
611
and Replace All and Save your file. Then put a space back between
" Ann"
and "Arbor " in the two occurrences of "
AnnArbor" and between "Mission " and " Viejo " in the one occurrence of "
Mission Viejo.
WHAT'S 'BEAN&i:WN
MWmHAPPENING? You
lon. You could have used any character. If you had used commas,
considered a comma-delimited file. But you have to alter your batch file so that it
knows that the character you selected is a semicolon. Note that tokens change from
7 to 1
28
(g)ECHO OFF
FIL, then edit PERSON. BAT so it looks as follows:
SORT
FOR
usebackq delinis=; tokens=1, 6" %%a IN
IF
%%b %%a, %%c
DEL %2
Note:
WHAT'S
When entering the third line (beginning with "
FOR" ), do not press the
IEnterl key until after
WWiWifiwNf
mw HAPPENII,iG1 The statement "
usebackq delims " stated that you were going to
use the ; as your delimiter. You also had to change the token. The last token is now 6,
not 7.
29 Key
FIL TEMP. FIL IEnterl
:I:!I;
!:,!Idl'
A: \ :=-PERSON SHORT. FIL
:~; j ; i
Carolyn Gillay, Professor
Kathryn Maurdeff , Teacher
Sonia Maurdeff , Student
Frank Panezich , Teacher
Steven Tuttle , Juggler
. Ii!!;!
;'f
!f!I"
'f;
::i;'!.i j k,
..il
-1\
A:\:=-
WHA 1' 5
WPXiW+WE#
HAPPENING?
want it.
Your data is delimited , and your output is displayed the way you
OEm
You sometimes need to be able to execute one batch file from wi
thin anotheJ: If the
second batch file is the last line in the original batch file
, there is no problem. The
second batch file is " invoked" from the first batch file. But invoking a second batch
file , executing it, and upon completion of the second file
, returning to the first batch
."1
' ,~
~.. . ':'
612
CHAPTER 11
file, is not so simple. When the operating system finishes executing a batch file, it
returns control to the system level and never returns to the original batch file. There
is a solution to this problem, the CALL command. It allows you to call (execute)
another batch file and then return control to the next line in the first (calling) batch
file.
"'.::~~:;k"::,,M;%~V~:::~:~%%&:~,~:::.':.
lIED Activity:
Note:
The DATA disk should be in Drive A. The displayed prompt isA:\:;:.. You
have executed the command
during the current Command Prompt session.
Key in the following: A: \:.CD
BATCH
IEnterl
A: \::-CD
A: \BATCH::- -
WHA 1' 5
iWt'tW;WWWt
~HAPPENING1 Your default directory is now the BATCH
things that you can create is a "noise " to get a user s attention. You can use
to make a noise. All computers will not make a
the
+
ICtr11 G
of
you create sounds like a beep, it is referred to as a bell because in the ASCII character set
ICtr11
+ G is labeled BEL. By using + G
ber that when you see
ICtr11 +
G, it means press the
ring a bell. " Remem-
key and the letter G. When
you
s computers are very
1.6 GHz is commonplace. The beep sound is very short, and
ICtr11
Therefore we will repeat the beep command six times, making it easier for you to
hear the generated sound. Even if you cannot hear the sound
error message.
Key in the following: A: \BATCH:.COPY CON BELL. BAT
ICtr11 + G
IEnterl
ECHO
ICtr11 + G
IEnterl
ECHO
ICtr11 + G
IEnterl
ECHO
ICtr11 + G
IEnterl
ECHO
ICtr11 + G
IEnterl
ECHO
ICtr11 + G
IEnterl
IEnterl
lEID
ECHO
A: \::-COpy
ECHO
ECHO
ECHO
ECHO
ECHO
ECHO
- I
A
A
A
A
A
A
CON BELL. BAT
, you will not get an
IEnterl
........
11.
31
613
1 file (s) copied.
A:\BATCH:=-
WHAT'S
m;;sHAPPENING?
Key in the following: A: \
, you can execute it.
TYPE BELL. BAT
IEnterl
A: \BATCH:=-TYPE BELL. BAT
ECHO
ECHO
ECHO
ECHO
ECHO
ECHO
A: \ BATCH:=- -
WHAT'S WiiniihHik
m1I! HAPPENING?
Key in the following: A:
, you hear the bell.
IEnterl
A: \BATCH:=-BELL
A: \BATCH:=-ECHO
A: \BATCH:=-ECHO
A: \BATCH:=-ECHO
A: \BATCH:=-ECHO
A: \BATCH:=-ECHO
A: \BATCH:=-ECHO
A: \BATCH:=- -
WHAT'S
~HAPPENING? You should have heard the bell beeping or
program. Notice, you heard 12 "beeps " instead of only 6. When the command was
read, the ' 'bell'' sounded, when the command was executed , the ' 'bell'' sounded
again.
Use any text editor to create and save a batch file called
directory. Key in the following:
COpy \* . ABC * . XYZ IEnterl
BATCH
DEL * . XYZ
Enter I
BELLlNG. BAT
in the
614
CHAPTER11 ADVANCED
IEnterl
BELL
IEnterJ
(Be sure to hit
BELL.
after
WHA1'S,+6JiiW+Nj/
Ii&WHAPPENiN(31 This batch
ABC files to the BATCH subdirectory,
delete them, and then ring a bell.
I ;
Key in the following: A: \BATCH:.BELLlNG
IEnterl
A: \BATCH::-COPY \ * .
\VEN. ABC
. XYZ
\JUP. ABC
2 file (s) copied.
A: \BATCH::-DEL * . XYZ
A: \BATCH::-BELL
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::- -
WHAT'S
Ilitt'WHAPPENiNG7 You called
line was BELL. BELL was called and it executed
would like the bell to sound before the files are deleted.
BELLlNG. BAT
Edit and save
COpy
to look as follows:
ABC . . XYZ
BELL
REM You are about to delete
PAUSE
DEL
XYZ files. Are you sure?
XYZ
Key in the ,following:
A:
IEnterl
A: \BATCH::-belling
A:\BATCH::-COPY \*
\VEN. ABC
ABC *
XYZ
11.
31
615
\JUP . ABC
2 f~le (s) copied.
A: \BATCH::-BELL
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::- -
WHAT'S
IMIWHAPPENING?
, it called and
, it
executed BELL. It turned
never returned to BELLING. Since it never returned to BELLING. BAT, your * XYZ
files were not deleted , nor did you see a message.
Key in the following: A: \BATCH:.DIR *
XYZ
IEnterl
A: \BATCH::-DIR * . XYZ
Volume in drive A is DATA
Volume Serial Number is 30B8-DA1D
Directory of A:
10/31/2001
10/31/2001
07:08 PM
07:08 PM
478 JUP. XYZ
2 File (s)
0 Dir (s)
478 VEN . XYZ
956 bytes
880, 128 bytes free
A: \BATCH::-
WHAT'S
m1I! HAPPENING? Indeed , the files are there. This is why you need the CALL
mand. CALL will process the batch file BELL, but it will then return control to
BELLING so that the other commands in BELLING can be executed.
10
BELLlNG. BAT
COpy
ABC * . XYZ
CALL BELL
Edit and save
to look as follows:
REM You are about to delete the * . XYZ files. Are you sure?
PAUSE
DEL * . XYZ
11
Key in the following: A: \BATCH:.BELLlNG
IEnterl
11\
r; ,
616
CHAPTER 11
ADVANCED BATCH FILES
A: \BATCH::-BELLING
A: \BATCH::-COPY \ * .
\ VEN . ABC
\JUP . ABC
. XYZ
2 file
A: \BATCH::-CALL BELL
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-ECHO
A: \BATCH::-REM You
XYZ files. Are you sure?
A: \BATCH::-PAUSE
Press any key to continue
WHAT'S
, the bell sounded. Once
BELL was finished executing, it passed control back to BELLING so that the next
00!!ftjj
commands could be executed.
IEnterl
12
Press
13
Key in the following: A: \BATCH:.DIR *
XYZ
IEnterl
Press any key to continue
A: \BATCH::-DEL * . XYZ
A: \BATCH::-DIR * . XYZ
Volume in drive A is DATA
Volume Serial Number is 30B8- DAID
Directory of A:
File Not Found
A: \BATCH::-
WHAT'S
I!iW'ii\
XYZ files are
gone. A more practical example of using CALL can be seen in the next group of
batch files you are going to write. You may find , as you move among directories
~llii, !
11.
31
617
that you want to go "home " again. In other words , you want to return to the direc-
tory where you were previously. You can create a series of batch files that will
remember the directory you were in and return you to it.
Note:
In the next file , it is important to press the
key immediately after the
command
before
you press
Enter I.
Do not press
Enter I , then
file in the BATCH directory.
DAT in the
14 Use COpy CON
the following:
COpy CON HOME. DAT IEnterl
SET HOME=6 IEnterl
15 Use
BA T in the
BATCH directory. Key in the following:
COpy A:\BATCH\HOME. DAT A:\BATCH\HOMESAVE. BAT
BAT
Enter
CALL HOMESAVE. BAT IEnterl
DEL A:\BATCH\HOMESAVE. BAT
Enter I
CD ~
WHAT'S
lWmIIHAPPENING? The HOME. DAT
data file you created will create an environmental variable called HOME. The batch file HOMETO. BAT that you just created will
be used to set the environmental variable to wherever you want HOME to be. The
batch file, line by line, breaks down as follows:
1: Copies the contents of the data file to the batch file. HOME.DAT now
contains SET HOME= . HOMESAVE. BAT now contains SET HOME=
Line
Line
2: Takes whatever
BAT. If
your current directory is BATCH , HOMESAVE. BAT now has the contents of SET
HOME=A:\BATCH.
3: Executes the batch file. HOMESAVE. BAT now executes and sets the variable
of HOME to A:\BATCH.
Line
Line
4: Deletes the batch file. Now that
no longer need the batch file HOMESAVE. BAT.
16 Use
BAT in the
directory that contains the following:
CD %HOME%
IEnterl
WHAT'S
OOJlftjjHAPPENING?
BAT will change your directory to whatever
value is in the environmental variable HOME at the time the batch file is executed.
In order for this procedure to work correctly, you must include A: \ BATCH in your
path statement.
17 Key
Enter
A: \BATCH::-PATH
PATH=C:
t~;
ll;
A: \BATCH::- -
~...
;'\:" ,
.'
'''''"''' ''''
618
CHAPTER 11
WHAT'S if"""'
!mil! HAPPENIN'
In this example, A: \ BATCH is indeed included in the path.
not included in your path, run ADD A: \ BATCH
18
IEnterl
Key in the following: A: \BATCH:.SET H
A: \BATCH::-SET H
HOMEDRlVE=G:
HOMEPATH= \
HOMESHARE=\
A: \BATCH::- -
WHA 1' 5 itwnnWIW
OOJlftjjHAPPENING? A: \
variable HOME displayed. You are now ready to test" going home."
19
Key in the following: A: \BATCH:.CD \
20
Key in the following: A: \:.CD
21
Key in the following: A:
IEnterl
WORK\ADS
IEnterl
Enter I
A: \BATCH::-CD \
A: \ ::-CD WORK\ADS
A: \ WORK\ADS
A: \WORK\ADS::-COPY , A: \BATCH\HOME . DAT A: \BATCH\HOMESAVE
1 file (s) copied.
A: \WORK\ADS::-CD
l::-::-A: \BATCH\HOMESAVE. BAT
A: \ WORK\ADS ::-CALL HOMESA
A: \WORK\ADS::-SET HOME=A:
A: \WORK\ADS::-DEL A: \BATCH\HOMESAVE. BAT
A: \ WORK\ADS::-
WHAT'S
!mil! HAPPENING?
directory so you keyed in HOMETO.
22
Key in the following: A:
A: \WORK\ADS::-SET H
HOME=A: \ WORK\ADS
HOMEDRIVE=G:
HOMEPATH= \
HOME SHARE = \
A: \ WORK\ADS::-
IEnterl
CHAPTER SUMMARY
WHAT'S UIUdt/inn.
m8%HAPPENING? You
value is A:\ WORK\ADS. You will go to another directory and then return to
A: \
23 Key
IEnterl
A: \ WORK\ADS
A: \MEDIA \ TV:=-
WHA 1'5 t:MU;Ulnn
m8%
home, which is
IEnterl
24 Key
A: \MEDIA \
A: \MEDIA\TV:=-CD A: \WORK\ADS
A: \
WHAT'S
NiNG7 You went home-home being
At any time during the Command Prompt session, you can change the value of
home by running HOMETO while in the directory you want home to be, and you
can return to it at any time by simply keying in HOME.
MillJm HAPPE
IEnterl
25 Key
IEnterl
26 Key
A: \WORK\ADS:=-CD\
A: \
C: \WINDOWSiC: \WINDOWS\system32\WBEM
A: \:=-PATH=C:
A:\:=-
WHA 1' 5
UFFtitt'fi!1+
~HAPPENIN
You have returned to the root directory of the DATA disk and reset
the default path.
Chapter Summary
You may substitute a double colon (::) for the REM statement.
To place a blank line in a batch file, use the ECHO
ately by a period (ECHO.
3. The GOTO command used in conjunction with a label creates a loop. The
will process the command following the label.
619
~;
'j,
"
~
.' .
. . f: r
1 ~;
I:
620
CHAPTER 11
ADVANCED BATCH FILES
4. The label in a
, but identifies a location in the batch
file.
5. The SHIFT command shifts over command line parameters to the left one
position at a time.
6. The SHIFT command is typically used in conjunction with GO
7. The IF command will test for some logical condition. If the condition is true
, the
command will be processed. If the condition is false , the batch file will fall
through to the next line of the batch file.
8. The IF command can test whether or not two character strings are identical.
9. The IF command can test whether or not a file exists.
10. The IF command checks ERRORLEVEL.
11. You may also use IF with NOT. The IF NOT command will test for a NOT
condition. If a condition is not true, then the command will process. If the
command is true, then the batch file will fall through to the next line in the batch
file.
12. You can test for a null value by using quotation marks, a word
, or backslashes.
13. To use IF EXIST and to test for the existence of a subdirectory, you
%1 \ NUL.
14. Many programs set an exit code when finished executing. The IF
in a batch file will test if an exit code is equal to or greater than the one in the
test.
15. When you use IF ERRORLEVEL in a batch file
, the codes must be listed in
descending order.
16. When you use IF NOT ERRORLEVEL in a batch file
, the codes must be listed in
ascending order.
17. You may write programs with DEBUG to
18. You can write programs directly in DEBUG
, or you may write a script file that
supplies input for DEBUG to create
19. The environment is an area in memory where the operating system leaves
messages to itself, including the path and the prompt values.
20. You can create environmental variables that can be used in batch files. When
using the variable in a batch file, the syntax is
%variable%.
21. The SET command lets you view what is currently in the environment.
22. Environmental variables set in other batch files or at the command line remain
in effect only during the current Command Prompt
mand Prompt window is closed, the variable disappears.
23. The DIRCMD command can be used to preset the DIR command parameters
and switches.
24. The FOR.. . INoo. DO command allows repetitive processing. The command will
execute some command for every value in a specified set.
25. When you use FORoo. IN.oo DO
IN
(set)
DO
at the command line, the syntax is FOR
command (command-parameter).
%variable
26. The IR when used with FOR... IN...
DO allows recursive processing. Recursive
meilns that the command will search all the directories.
27. Using the IF parameter with FOR...
INoo. DO allows you to select specified text
using delimiters that you can set.
28. The CALL command allows you to call one batch file from another. When the
second batch file is finished executing, it returns control to the first batch file.
TRUE/FALSE QUESTIONS
621
Key Terms
debug
EOF (end-of-file) mark
exit code
conditional processing
environment
environmental
variable
expression
loop
null value
recursive
scan code
script fil~
variable
Discussion Questions
1. What is the function of the REM, ECHO, and PAUSE commands?
2. What happens in a batch file if ECHO is set to
3. What happens in a batch file if you precede
CiY?
4. What is a NUL device? Why would you use a NUL device?
5. How can you place a blank line in a batch
6. How can you create a loop in a batch file? How can you stop a loop from
processing in a batch file?
7. What is the purpose and function of the GOTO
8. What is a label in a batch file?
9. What is the purpose and function of the SHIFT
10. Why is it useful to shift parameters?
11. How can you determine whether or not a file
12. What is the purpose and function of the IF command?
13. Give the syntax of the IF command and explain each part of the syntax.
14. What does it mean to test for a null value?
15. How can you test for a null value? Why would you test for a null value?
16. What is the purpose and function of the IF EXIST
17. Explain the purpose and function of the IF
18. What is a script file? How can you create one?
19. What is a scan code?
20. Give the syntax of the SET command and explain each part of the syntax.
21. Explain the purpose and function of the DIRCMD environmental variable.
22. What is the purpose and function of the FOR...
IN... DO command?
23. Name two
IN... DO command.
24. Describe the purpose of those parameters.
25. Give the basic syntax of the FOR...
IN... DO command and explain each part of
the syntax.
26. Explain the purpose and function of the CALL command.
True/False Questions
For each question, circle the letter T if the statement is true and the letter F if the
statement is false.
1. The SHIFT
2. Environmental
until the computer is turned off.
3. You
DIRCMD.
...0lIl
,' ~:
==
--:
,i
622
CHAPTER 11
4. Keying in
time.
5. In order to run
to the original batch file, you must use the CALL command.
Completion Questions
Write the correct answer in each blank space.
6. In a batch file,
must have the line IF MY. FIL.
7. In a batch file, the line IF " %1"
"" GOTO END is testing for a(n)
8. You can use the
to look at files in different directories.
9. When you key in
TXT, you are setting a(n)
10. When you use the GOTO command, you must also use a(n)
Multiple Choice Questions
For each question, write the letter for the correct answer in the blank space.
11. If ECHO. (ECHO followed immediately by a period) is entered into a
batch file, it will
a. - display
b. create a
c. do nothing.
d. cause the following
12. ECHO %PATH% (keyed in at the command line) will
a. produce an error
b. display the contents of the
c. display the path for the current session.
d. display the
13. The following command in a batch
TOP-will
a. test for a null value.
b. test for the
c. test for
d. all of
14. A valid batch file label consists of a
a. percent sign (%) and text chosen by the
b. colon
c. double colon (::) and text
d. any of the
15. So that %3 becomes %2, and %2 becomes %1 , you must use the
command in a batch file.
a. ROTATE
b. SHIFT %1
c. SHIFT
d. none of
i j: i
, ;,,1
jji, i
\- - bi '
~1 ~
HOMEWORK ASSIGNMENTS
Homework Assignments
Place the HOMEWORK disk in Drive A. Be sure to work on the HOMEWORK disk, not the DATA disk.
Note
1:
Note
2: The
, such as
floppy drive B or hard drive D , be sure and substitute that drive letter
when reading the questions and answers.
HOMEWORK disk is in Drive A. If you are using
Note
3:
correctly.
As.
Note
4: To
Note
5: There
IAltl + F and choose Save
works correctly, it is most likely written correctly.
Setup
Placethe
Create a
subdirectory on the
BATCH
GO. BAT
Move
subdirectory.
BATCH
from the root directory into the
NAME. BATfrom
and
the
BATCH
subdirectory back to the root
of the HOMEWORK disk.
off the root directory of the
CHAP11
Create a subdirectory called
disk.
Problem Set I
Problem A
A-a
Make
CHAP11
the default directory.
subdirectory called
that has the following line in it:
Create and save a batch file in the
DELBAK. BAT
CHAP11
BAK) DO IF
FOR %%h IN
1. The DELBAK.BAT file will
a. delete any file in the default drive and directory.
b. delete any
BAK exten-
sion.
BAK file extension exist in
c. first
the current drive and directory.
d. both b and c
2. If you wanted to use the previous batch file command on the command line,
what would you have to do?
a. change %%h to %h
b. change
c. change *
d. none of
BAK to %h%
623
---
",!"!1 '
624
CHAPTER 11
r ~~F .
A-c
Create the following batch file in the CHAP11 subdirectory and
LIST. BAT:
(g)ECHO OFF
ECHO.
FOR %%v IN
ECHO.
ECHO.
3. The LIST.
a. display the directory
b. use an environmental
c. both a and b
d. neither a nor b
Problem B
Note:
If you are in a lab environment or if your user name has spaces in it
there may not be a correct answer listed for questions 4 and 5.
B-a
Be sure that CHAP11 is the default directory on the
B-b
In the CHAP11 subdirectory, create and save a batch
PASSING. BAT that has the following lines in it:
(g)ECHO OFF
FOR %%a IN (%USERNAME%) DO IF "
:NOT
ECHO You used %1. This is not
%1"
%%a " GOTO DISPLAY
GOTO END
:DISPLAY
DIR %homepath% Ip
:END
B-c
Execute PASSING. BAT using your own logon name.
4. When you used PASSING.
BAT you
a. displayed the files in the current default
b. displayed the
c. displayed the
d. displayed the message You used Bette. This is not your correct login
name. "
Execute PASSING. BAT using Bette.
5. When you used
a. displayed the files in the
, you
b. displayed the
c. displayed the
d. displayed the message, "
name.
You used Bette. This is not your correct login
Problem C
C-a
Be sure that CHAP11 is the default directory on the
HOMEWORK ASSIGNMENTS
Note:
In the
The following batch file is called
user to swap file names between two existing files. If the user keyed in
SWAP MY. OLD MY. NEW, the file MY. OLD would then be named
MY. NEW and the file MY. NEW would then be named MY.
OLD.
However, this batch file has a problem and does not work properly.
CHAP11
SWAP. BAT
subdirectory, create and save a batch file called
that has the following lines in it:
(g)ECHO OFF
IF NOT
IF NOT
REN
HOLD
% 1
REN %2 %1
REN HOLD %2
GOTO END
:NOFILE1
ECHO Cannot find file %1
: NOFILE2
ECHO Cannot find file %2
:END
C-c
If you do not have a subdirectory called
any file with a
RED
extension
\SPORTS,
from
the
WUGXP
create it now. Then copy
subdirectory to the
subdirectory on the
\SPORTS
Key in the following:
A: \CHAPlbSWAP
\SPORTS\LEFT. RED \SPORTS\RIGHT. RED
IEnterl
6. When you used SWAP.BAT, you received an error message and SWAP.BAT
did not work. Why?
a. You cannot name a file
b. The
RED does not exist.
c. The syntax of the
d. You were immediately sent
7. Did any file get renamed in the \SPORTS directory?
a. yes
b. no
Problem Set II
Note
1:
Note
2:
The HOMEWORK disk is in Drive A and
drive and the default directory. All work will occur on the
disk.
If
, MARK.FIL, GETYN. COM, and GO. BAT are not in the root
directory, copy them from the WUGXP directory before proceeding.
Key in the following: A: \:.NAME
IEnterl
Here is an example to key in , but your instructor will have other information that
applies to your class. Key in the following:
625
"""
1\':1
626
CHAPTER 11
;; : . ~ J
IEnterl
Bette A. Peat
T - Th
IEnterJ
Chapter 11 Homework
(Your
day and time go here.
IEnterl
IEnterl
Problem A
Press (Efi)
(Your
name goes here.
class goes here.
(Your
CIS 55 1Enterl
IEnter!. If the information is correct, press Y and you are back to A:\:::-
Begin a new Command Prompt session by closing any existing Command
Prompt windows and opening a new window.
Make A:\:::- the default directory.
Key in the following: A: \:.PATH
BAT IEnterl
Problem A
A-a
Make CHAP11 the default directory.
The following batch file is called SETPATH.
BAT. Its purpose is to allow the
user to add a path to either the front or the back of the existing path. Any
number of subdirectory names can be added. When the user keys
SETPATH
subdirectory1 subdirectory2 or
IF
subdirectory1 subdirectory2, the
sUbdirectory name in front of the existing path. If the
keyed in without IF or If, each subdirectory listed will be added to the end of
the path.
A-c
Analyze the following batch file:
IF " %1"
%1"
IF
%1"
:ADD
IF " %1"
IF
'''' GOTO DEFAULT
/f"
/F"
GOTO FRONT
GOTO FRONT
"" GOTO
PATH= %PATH%;%1
SHIFT
GOTO ADD
: FRONT
SHIFT
IF " %1"
"" GOTO
PATH=%1;%PATH%
GOTO FRONT
: DEFAULT
PA TH=C:\WINDOWS\SYSTEM32
:END
In the CHAP11 subdirectory, create and edit a batch file
SETPATH. BAT based on the model above.
A-e
Document it with your name and date, and explain the purpose of the batch
file.
Copy the file called REPL Y. COM that you wrote in Activity 11.
XX from the
BATCH directory of the DATA disk to the CHAP11
.!::c~'
:U;
HOMEWORK ASSIGNMENTS
batch file so that you may give the user the opportunity to choose help on
how to use this command. The user will press Y or y if they want help or N
or n if they do not want help. If the user chooses help, provide help on syntax
as well as a description on how to use SETPATH. BAT. After displaying the
description , the user should exit from the batch file.
Note:
The scan code
value for Y is 89 and for y is 121. The scan code value for N is 78 and for n is
110.
Change the :
was, rather than PATH C:\WINDOWS\SYSTEM32 .
(Hint
Remember,
MYPATH. BAT is in the root directory of the
Problem A)
IEnterl
Key in the following: A: \CHAPll:.CD\
Key in the following: A: \:.GO NAME. FIL CHAP11\SETPATH. BAT
Follow the instructions on the screen.
A-j
IEnterl
In Notepad, click File. Click Print. Click the
Exit.
Problem B
B-a
Make CHAP11 the default directory.
Use SETPATH , created in Problem A, to add the A:\CHAP11 and the
A:\BATCH subdirectories to the path.
B-c
Using as a model MY. BAT from Activity 11. XX, create and save a batch file
called DIRS. BAT in the CHAP11 subdirectory.
Document it with your name and date and explain the purpose of the batch
file.
B-e
Have the following choices in DIRS. BAT:
Look at directories only, arranged alphabetically by name in the root
directory. (Call this Root Directory Only.
Look at directories only, arranged alphabetically by name on the entire
disk beginning with the root directory.
Look at file names
directory in reverse alphabetic order.
(Hint:
Remember /b. ) (Call this File
Names Only.
Look at files only in the root directory arranged by date/time.
Files by DatelTime.
Do not use date alone in an
do, you are asking that the DATE command be executed.
Note:
The scan code value for
use other key choices, see Appendix C for the scan code values.
Remove any other choices.
Be sure no command lines are displayed, only the results of the com-
mands.
Key in the following: A:
Enter
Use Edit to modify NAME. FIL, changing
627
628
CHAPTER 11
B-h
Key in the following: A: \;:.GO NAME.
Follow the instructions on the screen.
FIL CHAP11\DIRS. BAT
(enter)
In Notepad , click File. Click Print. Click the
Exit.
Problem C
C-a
C-b
Make CHAP11 the default directory.
Check the path to see if CHAP11 is in the path. If not, use SETPATH,
created in Problem A, to add the A:\CHAP11 subdirectory to the path. Add
the A:\BATCH subdirectory to the path as well.
The following batch file is called WORD.
Note:
BAT. Its purpose is to allow
filename. ext. If the file already exists
is immediately taken into Edit with the named file. If
, the user
the user to key in WORD
is a new file, the user will be told to key in WORD and
, however
the file
the new, file
name at the command line. This batch file
Every time the user keys in WORD
the batch file.
C-c
, as written, has a fatal flaw.
he or she is kicked out of
newfile. nam
Analyze the following batch file:
:START
IF EXIST %
IF NOT
:PROCEED
EDIT %
GOTO END
:NEWFILE
ECHO This is a new file, %
ECHO wish to create a new file, you must
ECHO key in WORD and the new file name
ECHO The syntax is:
ECHO WORD neW. fil
:END
C-d
Edit and save a batch file called WORD.
thename
CHAP11
subdirectory
based on the above model. Document itBAT
with in
your
and date
and
explain the purpose of the batch file.
If the user keys in WORD with no file name
, the to
batch
file should
tell the
user what was done wrong and return the user
the system
level.
If the user keys in WORD filename,
extfile
andname.
it is anHowever
existing file, the file
should go directly into Edit using that
not exist, offer the user two
, if the file does
exit the batch file or to create a new file and be taken back to the
CEED label. You will use
made.
Note:
The scan code value for Y is 89 and for y is 121. The
code value for N is 78 and for n is 110.
C-e
Key in the following: A:
Use Edit to modify NAME.
tenter)
FIL, changing Problem B to
HOMEWORK ASSIGNMENTS
Key in the following: A: \
FIL CHAP11\WORD. BA T
Enter I
Follow the instructions on the screen.
In Notepad, click File. Click Print, Click the
Exit.
Problem D: Challenge Assignment
The following batch file change is difficult, so do it only if you want a challenge.
D-a
Make CHAP11 the default directory.
Check the path to see if CHAP11 is in the path. If not, use SETPATH
created in Problem A, to add the CHAP11 subdirectory to the path.
The following batch file is called COMPILE. BAT. Its purpose is to allow
Note:
the user to make a list of all the files in all the subdirectories on a
specific removable disk. You will need to asce~tain which are your
removable disk drive letters.
have a Zip drive or CD- ROM drive, you will need to know the drive
letter for each device. Once the user keys in COMPILE, the REPLY
command in the batch file asks which drive the user wants to use to
compile a list of files. You will have to use the
to determine which keys the user may press depending on the drive
letters on your system. The user may also quit once the list of files is
completed. All the information from all the disks that are cataloged is
collected in one file called TEMP.TXT.
D-c
Analyze the following batch file:
(g)ECHO OFF
CLS
ECHO.
ECHO Key in a Drive letter to compile a
ECHO Use Q to quit. Use A for Drive A.
ECHO.
REPLY
IF
IF
IF
IF
:DRIVEA
SET DRV=A
GOTO LIST
: LIST
CLS
ECHO You are now compiling a list of all
ECHO Listing files for Drive
ECHO.
TXT
DIR %DRV%:\ IS
IA ION
:=-:=- TEMP. TXT
ECHO.
TXT
ECHO You have compiled a list of all the files
ECHO to a file called TEMP. TXT on the default drive.
629
IT .
630
CHAPTER 11
PAUSE
:END
Edit and create a batch file called COMPILE.
BAT in the CHAP11
subdirectory based on the above model.
D-e
Document it with your name and date and explain the purpose of the batch
file.
The file TEMP, TXT should first be deleted in the batch file.
Following REPLY, add more choices for the user. The additional choices will
allow the user to choose any removable drive. The user should be able to
. return to the
This will give the user the opportunity to either choose a different removable
drive, or to choose the same one and place a new disk or
(Hint:
Remember, adding choices also means adding more IF
Use the same
TEMP. TXTfile on the screen. Remember, the file
the user to be able to view the entire file. When finished viewing, the user
should be returned to
(Hint:
Remember, every time you add a choice, you must add more IF
ERRORLEVEL statements.
Key in the following: A:
D-j
IEnterl
Use EDIT to modify NAME. FIL, changing Problem C to Problem D.
Key in the following: A: \;:.GO NAME. FIL CHAP11\COMPILE.
Follow the instructions on the screen.
BAT
IEnterl
In Notepad, click File. Click Print. Click the
Exit.
Problem E: Challenge Assignment
The following batch file change is difficult, so do it only if you want a challenge.
E-a
Make CHAP11 the default directory.
E-b
Copy SWAP. BAT (created in Problem Set I- Problem C) to a new file called
SWAP2. BAT.
E-c
Document it with your name and date and explain the purpose of the batch
file.
The batch file should perform as follows:
When you key in SWAP2
dirname filename
filename2,
the file names will
be reversed in the specified directory. The batch file will change to the
specified directory prior to performing the "
swap.
One of the items you wish to test for is the existence of a directory.
(Hint
Remember % 1\Nul.) If you key in a subdirectory name where a file name
is expected, the batch file should take you to a message that tells you that
you keyed in a directory name, not a file name.
HOMEWORK ASSIGNMENTS
631
. ~I
If you key in only SWAP2, the batch file will take you to a message that
tells the user how to use the command correctly.
Be sure the batch file returns the user to the CHAP11 subdirectory.
E-e
Key in the following: A: \CHAPll;:.CD \
IEnterl
Use Edit to modify NAME. FIL, changing Problem C to
E-g
Key in the following: A: \;:.GO NAME. FIL \CHAP11\SWAP2. BAT IEnterl
Follow the instructions on the screen,
In Notepad, click File. Click Print, Click the
Exit.
Problem F: Challenge Assignment
The following batch file change is very difficult, so do it only if you want a
real
challenge.
F-a
In the Chap11 directory, copy SWAP2. BAT to SWAP3. BAT.
Change SWAP3. BAT to allow the user to key in either SWAP3
file1 file2 or SWAP3 file1 file2 dirname.
F-c
Use Edit to modify NAME. FIL, changing
Key in the following: A: \;:.GO NAME. FIL \CHAP11\SWAP3. BAT IEnterl
Follow the in.structions on the screen.
F-e
In Notepad, click File. Click Print. Click the
Exit.
Problem Set III-Brief Essay
1. The following is a batch file called TEST. BAT:
(g)ECHO OFF
:AGAIN
IF \%1\==\ \
IF %l==LIFE ECHO LIFE
IF %l==MINE ECHO MINE
IF %l==YOURS ECHO YOURS
IF \ %1 \== \ \
IF NOT EXIST %1 GOTO NEXT
TYPE %1
:NEXT
. SHIFT
GOTO AGAIN
:END
LIFE, MINE , and YOURS are variables. MY. FIL is a file that is on the disk.
Analyze this file and describe what will happen and
of the following at the command line:
a.
b. TEST
c. TEST
d.
FIL
; ~.;.
' ,
U~L
fo3~
CHAPTER 11
e. TEST
f. TEST my. fil
2. The following is a
this batch file is so that the user may copy many different files to any floppy
disk that the user specifies. Thus
, if the user keyed in COpy A:
the. TXT files and all the .
TXT * NEW, all
NEW files would be copied to the disk in Drive
A. The
contents of tOPI. BAT are:
IF " %1" "" GOTO END
FOR %%a IN (a A) DO IF "
%%a
%l" GOTO drivea
FOR
%%b
IN (b B) DO IF
%%b"
%l"
GOTO driveb
~J :
!i
J:
~f
:drivea
SHIFT
:newa
IF " %1"
"" GOTO END
ECHO copying %1
COpy %1 A:
SHIFT
GOTO newa
:driveb
SHIFT
:newb
IF " %1"
"" GOTO END
ECHO copying %1
COpy %1 B:
SHIFT
GOTO newb
:END
Analyze this file and describe what will happen and why when you key in
each of the following at the command line:
a. COPI
b. COPI A C:\WUGXP\*
TMPC:\WUGXP\*
c. COPI B C:\WUGXP\*
TMPC:\WUGXP\*
d. COPI A:
e. COpy
Since most people no longer have a Drive B
, how
could
you modify
batch file so you could use a removable Zip drive
with
the drive
letter ofthis
H?