Answers To Discussion Questions Advanced Batch Files

advertisement
Instructor: Prof. Michael P. Harris
Answers to Discussion Questions
Chapter 11
Advanced BATCH Files
Chapter 11
Advanced Batch Files
ANSWERS TO DISCUSSION QUESTIONS (p. 621)
1.
What is the function of the REM, ECHO, and PAUSE commands?
REM, ECHO, and PAUSE are used to make DOS batch files easier for people to work
with. REM allows the batch file to be documented. ECHO allows the batch file to
control the screen output during the execution of the batch file by displaying output from
the commands, not the commands themselves. PAUSE will stop the batch file from
executing until the user presses a key or breaks into the batch file with a <Ctrl> + C.
2.
What happens in a batch file if ECHO is set to OFF?
When ECHO OFF is used, commands will not be displayed in a batch file. Only the
output of the commands will be displayed.
3.
What happens in a batch file if you precede the ECHO OFF switch with @?
Preceding ECHO OFF with the @ sign suppresses the appearance of the words ECHO
OFF on the screen.
4.
What is a NUL device? Why would you use a NUL device?
A NUL device is nothing. You can use the NUL device when you want to eliminate
unwanted screen output. An example of when you want to eliminate screen output is
when you copy files in a batch file. If you do not want to see the names of files or the
message of how many files copied, you can redirect the output to NUL.
5.
How can you place a blank line in a batch file?
To place a blank line in a batch file output, use ECHO followed immediately by a period
(ECHO.). Note: there can be no space between the word ECHO and the period.
6.
How can you create a loop in a batch file? How can you stop a loop from
processing in a batch file?
Looping in a batch file is accomplished with the use of GOTO commands and a label.
You can stop a loop by using an IF statement, or by breaking into the batch file with
<Ctrl> + C.
7.
What is the purpose and function of the GOTO command?
The GOTO command used in conjunction with a label creates a loop. The GOTO
command will process the command following the label. The GOTO statement directs
the batch file to the location where the batch file processing will continue executing.
Carolyn Z. Gillay, Bette A. Peat, WUGXP Command Line
Franklin, Beedle & Associates, Inc. 2002 ©
Page 1
Instructor: Prof. Michael P. Harris
Answers to Discussion Questions
8.
Chapter 11
Advanced BATCH Files
What is a label in a batch file?
A label is a name chosen to flag (identify) a location in a batch file. The name is always
preceded by a colon and has a maximum length of 8 characters. Batch file labels are used
with GOTO commands to allow the processing of the command after the label.
9.
What is the purpose and function of the SHIFT command?
The SHIFT command is used to maximize the parameters on a command line in batch
files. Since the OS can only accept ten parameters at a time on the command line, SHIFT
can be used to access any number of parameters on the command line. The SHIFT
command will move over each parameter each time the SHIFT is read.
10.
Why is it useful to SHIFT parameters?
By using SHIFT in a batch file, you need not know how many parameters the user will
key in. It also limits the specific items you have to test for in a batch file.
11.
How can you determine whether or not a file exists?
The IF EXIST <filename> command tests whether or not a file exists.
12.
What is the purpose and function of the IF command?
The IF command tests for some logical condition. If condition is true, command
will be processed. If condition is false, batch file will fall through to next line of batch
file.
IF command can also check for three conditions: 1) Whether two sets of character
strings are identical, 2) Whether a file exists, or 3) Value of an ERRORLEVEL.
13.
Give the syntax of the IF command and explain each part of the syntax.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT
ERRORLEVEL number
command
string1= =string2
EXIST filename
14.
specifies execution if condition is false
Condition is true if ERRORLEVEL from last program is
greater than or equal to number
Command to run if condition met
True if strings match
True if the file exists.
What does it mean to test for a null value?
Testing for a null value is testing to see if nothing is there (no data). But you must have
“something” to test for “nothing” so you place a value in the test that will give you
nothing. Testing for null values eliminates endless loops.
Carolyn Z. Gillay, Bette A. Peat, WUGXP Command Line
Franklin, Beedle & Associates, Inc. 2002 ©
Page 2
Instructor: Prof. Michael P. Harris
Answers to Discussion Questions
15.
Chapter 11
Advanced BATCH Files
How can you test for a null value? Why would you test for a null value?
Can test for null value by:
1. Using quotation marks so statement becomes IF “%1” ==””GOTO LABEL. What
this is saying is “If nothing is there GOTO somewhere else.”
2. Use any word IF %1word==word GOTO LABEL.
3. Use \ so statement becomes IF \%1\==\\GOTO LABEL.
16
What is the purpose and function of the IF EXIST/IF NOT EXIST command?
IF EXIST or IF NOT EXIST determine whether or not a file exists in the specified path.
You can then execute one or more commands based on the existence of that file. IF
EXIST/IF NOT EXIST works only with file names and not with directory names.
17.
Explain the purpose and function of the IF ERRORLEVEL command.
The IF ERRORLEVEL command is used to check the exit code set by a program. A
batch file can use that exit code to take action based on the results of the exit code.
18.
What is a script/batch file? How can you create one?
A script is a set of instructions that you can write in any ASCII editor. Basically a batch
file is a list of command-line commands that would have been typed into the CLI.
19.
What is a scan code?
A keyboard scan code is the code that the computer actually receives when you
press a key. These codes are then translated by the operating system and application to
indicate which key was pressed.
Each key is identified by a one- or two-digit scan code. Two things are reported
when any key is pressed. First, a key was pressed. Second, that the key was released. 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.
20.
Give the syntax of the SET command and explain each part of the syntax.
SET [variable=[string]]
variable
string
Specifies environment - the variable being set.
Specifies a series of characters to assign to the variable = the string
the variable will be set to.
If no parameters are used, all current environmental variables will be displayed.
21.
Explain the purpose and function of the DIRCMD environment variable.
DIRCMD is an OS variable used to store the default parameters for the DIR command.
DIRCMD can be set in AUTOEXEC.BAT or in a batch file to automatically store the
parameters each time you boot the computer. It can also be set at the command line so
that for instance, by setting DIRCMD=DIR /ON, your directory display will always be in
alphabetical order by name whenever you execute the DIR command.
Carolyn Z. Gillay, Bette A. Peat, WUGXP Command Line
Franklin, Beedle & Associates, Inc. 2002 ©
Page 3
Instructor: Prof. Michael P. Harris
Answers to Discussion Questions
22.
Chapter 11
Advanced BATCH Files
What is the purpose and function of the FOR…IN…DO command?
The FOR…IN…DO command allows you to execute several commands at once, with
different parameters. The FOR…IN.(set)..DO command allows processing of all items
that are specified in a set. Whereas GOTO/LABEL provides a vertical loop,
FOR…IN…DO provides a horizontal loop.
23.
Name two parameters you can use with the FOR…IN…DO command.
%variable
(set)
command
command-parameters
24.
Describe the purpose of those parameters.
%variable
(set)
command
command-parameters
25.
Specifies a replaceable parameter.
Specifies a set of one or more files. Wildcards may be
used.
Specifies the command to carry out for each file.
Specifies parameters or switches for the specified
command.
Give the syntax of the FOR...IN...DO command and explain each part of the
syntax.
Runs a specified command for each file in a set of files.
FOR %variable IN (set) DO command [command-parameters]
%variable
(set)
command
command-parameters
Specifies a replaceable parameter.
Specifies a set of one or more files. Wildcards may be
used.
Specifies the command to carry out for each file.
Specifies parameters or switches for the specified
command.
To use the FOR command in a batch program, specify %%variable instead of
%variable.
26.
Explain the purpose and function of the CALL command.
The CALL command is used to execute one batch file from within another. Without the
CALL command, when batch file A executes batch file B, batch file A will never be
returned to. With the CALL command, batch file A can call batch file B and will be
returned to when batch file B is done.
Carolyn Z. Gillay, Bette A. Peat, WUGXP Command Line
Franklin, Beedle & Associates, Inc. 2002 ©
Page 4
Download