[Front] [Prev Chapter]
[Next Chapter]
Chapter 7 Batch Processing
When you work with the operating system, you usually enter commands at the system prompt and the system executes them. This is referred to as interactive processing. There is another kind of processing, however, called batch processing.
This chapter describes two methods of batch processing: batch files and macros.
What is a Batch File?
As you work with the operating system, you will probably find that you enter a command or a sequence of commands over and over again. You may have a set procedure each morning to load a spreadsheet program, for example, and another procedure in the evening to back up the files you created or changed during the day.
Batch processing allows you to collect all the commands you need to carry out a task into a single batch file so that you do not need to enter each command individually. When you enter the name of the batch file at the system prompt, each command in the file is executed as if you had entered them one at a time. Using a batch file also ensures that a procedure is carried out in exactly the same way every time.
For example, assume that you use the following commands every day to back up your files:
CD \LETTERS <Enter>
(to make LETTERS the current directory)
ERASE *.OLD <Enter>
(to erase all files with the extension .OLD)
MD A:\DAYSWORK <Enter>
(to create the directory DAYSWORK on the A: drive)
COPY *.DOC A:\DAYSWORK <Enter>
(to copy all files with the extension .DOC to A:\DAYSWORK)
You can create a batch file that contains all of these commands called DAYOVER.BAT. Then, at the end of the day, you would only need to enter the following to execute all of the commands:
DAYOVER
Creating Batch Files with EDIT
You can create batch files using EDIT or another word processor.
NOTE: If you want to use a word processor other than EDIT, make sure that the completed batch file is in ASCII format. Refer to the word processor documentation for more information.
The rules for naming a batch file are the same as for any other file except that you must use the extension .BAT. Also, you must enter each command in the batch file on a separate line.
To create the batch file described in the previous section, for example, type the following at the system prompt:
EDIT DAYOVER.BAT <Enter>
Because this is a new file, you see a message asking you whether you want to create it. Press <OK> and the EDIT screen is displayed.
Type the commands you want in the batch file, as follows, pressing <Enter> at the end of each line:
CD \LETTERS
ERASE *.OLD
MD A:\DAYSWORK
COPY *.DOC A:\DAYSWORK
Press <Ctrl> KX to save the file and exit EDIT.
NOTE:There are several other ways of saving files and quitting EDIT. Refer to Chapter 6, "Editing Text Files" for complete information.
Use this procedure to create any batch file you require.
Creating Batch Files with COPY
Instead of using EDIT or another word processor, you can use the COPY command to create batch files directly at the system prompt.
Use the following version of COPY:
COPY CON filespec
When you specify CON as the device, every character you enter is recorded in the destination file you specify (filespec), except for the line editing characters you use, such as <Enter> to start a new line or <Ctrl> H to delete the character to the left of the cursor. See the sections "Command Line Editing" and "Extended Command Line Editing" in the "Command Reference" chapter of DOSBook for a complete list of the command line editing keys.
Press <Ctrl> Z or <F6> when you have finished entering your file and close the file by pressing <Enter>.
Running Batch Files
To run a batch file, it must be located in the current directory or in one of the directories in the search path defined by PATH. Refer to the description of PATH in the "Command Reference" chapter of DOSBook for more information about search paths.
Run a batch file by entering its name in the command line; you do not need to include the extension (.BAT).
When the batch file is finished executing, the system prompt returns.
Example
Assume you created the following batch file to copy two sets of files from the current drive to the root directory of the diskette drive: all files in the directory DIRA and then all files with the extension .DAT in the directory DIRB.
CD \DIRA
COPY *.* A:\
CD \DIRB
COPY *.DAT A:\
CD\
When you run the batch file, you might see something similar to the following at the system prompt as each command in the file is executed:
CD \DIRA
then
COPY *.* A:\
PGM1.EXE
PGM2.EXE
PGM3.EXE
3 File(s) copied
then
CD \DIRB
then
COPY *.DAT A:\
MON.DAT
TUES.DAT
WED.DAT
THURS.DAT
FRI.DAT
5 File(s) copied
then
CD\
Stopping Batch Files
Press <Ctrl> C to stop a batch file while it is running. You will see the following message:
Halt Batch Process (Y/N)?
Answer Y for YES and the remaining commands in the batch file are not executed. You return to the system prompt.
Answer N for NO and the remaining commands will be processed.
Displaying Messages
By default, the commands contained in a batch file are displayed during batch file execution. To disable this feature, set the ECHO command to OFF. Enter ECHO by itself at the system prompt to see the current status of ECHO. You can also prevent the display of individual commands in a batch file by preceding the command with @. Refer to the descriptions of @ and ECHO in "Batch File Command Reference" starting on page 7-14.
ECHO can also be used to display messages during batch file execution. When you include a message preceded by ECHO in your batch file, that message displays regardless of whether the ECHO feature itself is set to ON or OFF.
For example, assume you include the following statements at the beginning of a batch file:
@ECHO OFF
ECHO THIS IS MY FIRST
ECHO BATCH FILE
When you run the batch file, the following message is displayed:
THIS IS MY FIRST
BATCH FILE
Comments
When ECHO is ON, any comments or remarks that you include in a batch file are also displayed. You include comments by using the REM command; any line in a batch file that begins with REM is ignored during execution (other than being displayed when ECHO is ON).
Using Replacement Variables
By using replacement variables in a batch file, you can make the batch file behave differently according to the parameters you enter with the filename when you run the file. When you run the batch file, the variables are replaced by the parameters entered at the system prompt.
Replacement variables are entered as %n, where n is a number from 0 through 9. %0 represents the batch file name. %1 represents the first parameter at the system prompt, %2 represents the second parameter, %3 the third parameter, and so on.
If you want to use more than 10 parameters, you must also use the SHIFT command in your batch file. SHIFT allows you to change the position of command line parameters by shifting them one parameter position to the left. Refer to the description of SHIFT in "Batch File Command Reference" starting on page 7-14.
If you want to use the % character in a filename within a batch file, you must enter it twice. If the filename is TEST%.DAT, for example, you must enter it in the batch file as TEST%%.DAT. Generally speaking, you should not use the % character in filenames.
When you run the batch file at the system prompt, always separate parameters with a space and make sure that they are entered in the correct sequence.
Example
The following batch file MOVEOVER.BAT, if created in C:\AMANDA\LETTERS, copies any file from C:\AMANDA\LETTERS to C:\QUERIES\DONEWITH and then deletes the original:
COPY %1 C:\QUERIES\DONEWITH
ERASE %1
So to run MOVEOVER.BAT to copy and delete a file called JONES.LET, you would simply type the following:
MOVEOVER JONES.LET <Enter>
The operating system would then execute the following commands:
COPY JONES.LET C:\QUERIES\DONEWITH
ERASE JONES.LET
To copy and delete SMITH.LET instead, you would type the following:
MOVEOVER SMITH.LET <Enter>
Using Environment Variables
You can also use environment variables in batch files. Environment variables are similar to replacement variables except that they are not provided in the command line that runs the batch file. Some are pre-defined by the operating system, some are set by specific commands, and some are handled by the SET command. Refer to the description of SET in the "Command Reference" chapter of DOSBook for information about SET.
Table 7-1 lists the standard environment variables.
To use the current value of an environment variable in a batch file, enclose it in % signs.
For example, the following command changes the current system prompt:
PROMPT %OS% $P$G <Enter>
Controlling Execution of Commands
Most batch files are more than just commands and variables. You can also use various special batch file subcommands to control the flow of execution within a batch file and perform more complex tasks.
Conditional Execution
Use the IF statement to specify a condition that must be true before a command is run.
You can specify that one string must equal another, for example. Assume that you want to create a batch file called GAME.BAT to start a certain computer game when you type the following command:
GAME FUN <Enter>
You could include the following statement in GAME.BAT so that the game is started only if %1 (the first parameter in the command line) is FUN. If %1 is not FUN, the operating system skips the PLAY command and processes the next line in the batch file.
IF "%1" == "FUN" PLAY
After PLAY is finished, the next line of the batch file is processed.
Changing Execution Flow
You can also force processing of a batch file to switch to another part of the file and continue executing commands from that point. You do this by using a label, to identify a particular point within the file, and a GOTO command.
For example, when execution reaches the GOTO statement in the following lines, it is forced to jump back to the label START and continue processing from there.
:START
TYPE INFO.TXT
GOTO START
The next example shows a batch file with three GOTOs in it.
@ECHO OFF
REM GOTO.BAT - Example of Using GOTOs
GOTO LABEL1
REM This part is never executed
ECHO This part is never executed
:LABEL2
ECHO The program is at LABEL2
GOTO END
REM This part is executed first
:LABEL1
ECHO The program is at LABEL1
GOTO LABEL2
REM This part is executed last
:END
ECHO This is the end of the file
You can use GOSUB instead of GOTO when you want to force execution to a subroutine rather than an individual command. Statements are processed until a RETURN command is encountered.
Using IF and GOTO Together
By using GOSUB or GOTO commands together with IF statements, you can run different sections of the batch file according to different conditions.
Many external commands set a return code or "error level" when they finish. The value such a command returns can be tested using an IF ERRORLEVEL command. The following batch file, for example, tests the return code from an XCOPY operation:
XCOPY %1 %2
IF ERRORLEVEL 1 GOTO FAIL
DEL %1
GOTO END
:FAIL
ECHO Copy Failed - Not Deleting Files
:END
You can also specify that a certain file or condition must exist or must be true before a command is executed. The following batch file, for example, uses IF to test whether expanded memory is available before starting a calculator application called CALC:
IF EXIST EMMXXXX0 GOTO OK
ECHO EMS memory manager not loaded - Load EMS
ECHO driver
GOTO END
:OK
CALC
:END
Refer to the descriptions of IF, GOTO, and GOSUB in "Batch File Command Reference" starting on page 7-14 for more information about using these commands to control batch file execution.
Using System Information
You can use a number of "system information" values in a batch file. These values are like environment variables but in most cases you cannot set their values yourself. They are useful, however, for displaying extra information or for testing to determine the way in which the batch file executes. All the system information variables are listed in Table 7-2.
Table 7-2
System Information Variables
|
Variable Name
|
Explanation
|
|
AM_PM
|
a.m. or p.m.
|
|
Day
|
01 to 31
|
|
Day_of_Week
|
Monday, Tuesday, etc.
|
|
Hour
|
1 to 12
|
|
NDay_of_Week
|
1 to 7 (1=Sunday)
|
|
Month
|
01 to 12
|
|
Month_Name
|
May, June, December, etc.
|
|
Year
|
1983, 2016, etc.
|
|
Short_Year
|
83, 95, etc.
|
|
Hour24
|
00 to 23
|
|
Minute
|
00 to 59
|
|
Second
|
00 to 59
|
|
Greeting_Time
|
Morning, Afternoon, or Evening
|
|
ErrorLevel
|
Error return code
|
|
OS
|
Operating system (DRDOS)
|
|
OS_Version
|
Operating system version number
|
The following variables are only available when you have the NetWare® drivers and shell loaded:
The following statements in a batch file, for example, cause the appropriate greeting (including the greeting time and login name) to display for any user. Mail will only be checked, however, if the user is FRED.
ECHO "Good %Greeting_Time% %Login_Name%"
IF NOT "%Login_Name%" == "FRED" GOTO NOMAIL
ECHO "Checking Mail"
...
...
:NOMAIL
Running a Batch File from Inside Another
If you want to run a batch file from inside another batch file, simply include the name of the second batch file inside the first. The second batch file is run when the operating system processes the line containing the name of the file. When it has finished running, you return to the system prompt. The remaining statements in the original batch file are not executed.
To run a batch file from inside another and then return to the original batch file and continue processing, you can use the CALL command in the original file.
The following batch file OUTER.BAT, for example, displays a message and then calls another batch file, INNER.BAT. When INNER.BAT is finished, execution returns to OUTER.BAT and processes the next line (which displays another message):
@ECHO OFF
REM OUTER.BAT
ECHO I am in OUTER.BAT
CALL INNER
ECHO I am back in OUTER.BAT
You can call as many batch files as you like from inside another batch file.
Refer to the description of CALL in "Batch File Command Reference" starting on this page for more information.
Batch File Command Reference
Any command you enter at the system prompt can be used in a batch file, using the same syntax as you would use when entering the command at the system prompt. Refer to the "Command Reference" chapter of DOSBook for descriptions of all these commands.
As described previously in this chapter, however, there are also special commands available for use with batch files called batch file subcommands. Batch file subcommands allow you to write batch files that are like simple programs. This section describes each of these commands.
NOTE: All batch file subcommands (except CHOICE) are internal commands. CHOICE is an external command that is only used in batch files.
The conventions used in the following command descriptions are the same as those used in the "Command Reference" chapter in DOSBook; refer to this if you need an explanation of the conventions.
@
Format
@ command
Explanation
@ prevents the specified command from displaying during batch file execution.
Example
The following line in a batch file turns the ECHO feature off and ECHO OFF itself is not displayed:
@ECHO OFF
?
Format
? ["message_string"] command
Explanation
The question mark (?) at the beginning of a statement causes the operating system to prompt the user about whether the statement should be processed.
You can also specify the text of the prompt by including it between the question mark (?) and the beginning of the statement, enclosed by double quotation marks.
When you run a batch file containing a ? statement, a response to the prompt is required by the user before processing continues.
Note that the maximum length of a batch file statement (including ?) is 128 characters.
Example
The following statement in a batch file causes the system to prompt the user before deleting all the files with the extension .OLD from C:\ACCOUNTS:
?DEL C:\ACCOUNTS\*.OLD
So, when the file executes, the user sees the following:
DEL C:\ACCOUNTS\*.OLD (Y/N) ?
If the user answers Y, the DEL command is run and the files are deleted. If the user answers N, the command is ignored and the next line in the batch file is processed.
:label
Format
:label
Explanation
A label defines a point within a batch file that can be referenced by a GOSUB, GOTO, or SWITCH command within the same file.
Only the first eight characters in a label are recognized; any additional characters are ignored.
Labels are not displayed during batch file execution.
CALL
Format
CALL filespec
Explanation
CALL runs another batch file before the next statement in the original batch file is processed.
The search path defined by the PATH command is used to locate the second batch file.
Example
The following batch file uses two CALLs to other batch files:
REM This file runs BATCH2 and then BATCH3
CALL BATCH2
CALL BATCH3
CHOICE
Format
CHOICE [/C:choices] [/N] [/S] [/T[:]c,nn] [text]
Explanation
The CHOICE command is used in batch files to present the user with choices. The user selects from the displayed choices by pressing a key, and CHOICE returns an ERRORLEVEL value that reflects the key pressed. The ERRORLEVEL value can be used by other batch file commands, such as IF, to control what the batch file does next.
NOTE: CHOICE is an external command that is only used in batch files and is therefore described here rather than in the "Command Reference" chapter of DOSBook.
CHOICE returns ERRORLEVEL 1 if the user selects the first of the key choices, ERRORLEVEL 2 if the user selects the second of the keys, and so on. If the user presses <Ctrl> C instead of selecting one of the keys, CHOICE returns 0.
If the user enters an invalid CHOICE command, ERRORLEVEL 255 is returned, and help text is displayed.
Examples
If you include the following command in a batch file:
CHOICE /C:yne Yes, No, or Exit
the user sees the following message when the batch file runs:
Yes, No, or Exit [Y,N,E]
CHOICE returns ERRORLEVEL 1 if the user presses Y, 2 if they press N, and 3 if they press E.
In the following example CHOICE defaults to Y, unless the user makes a selection within 10 seconds:
CHOICE /C:yne /T:y,10 Yes, No, or Exit
The next example demonstrates how CHOICE can be used in conjunction with other batch file commands. In this example, the user is given the option to select one of three utilities: DOSBook, EDIT, or SETUP.
@echo off
:start
cls
echo DOSBOOK
echo EDIT
echo SETUP
echo -------
choice /c:desq Select an option or Q to quit
if errorlevel 4 goto end
if errorlevel 3 goto setup
if errorlevel 2 goto edit
if errorlevel 1 goto dbook
:setup
setup
goto start
:edit
edit
goto start
:dbook
dosbook
goto start
:end
cls
NOTE: The IF statements in this example are true if the ERRORLEVEL value returned by CHOICE is greater than, or equal to, the ERRORLEVEL specified in the IF statement. Therefore, the IF statements are listed with the highest ERRORLEVEL first; if the order was reversed, the batch file would always jump to DOSBook.
ECHO
Format
ECHO [ON|OFF|message]
Explanation
ECHO enables or disables the display of commands during batch file execution when you specify ON or OFF. ECHO is ON by default. To display the current status of ECHO, enter ECHO without a parameter.
NOTE: @ECHO OFF is often used at the beginning of a batch file to prevent all commands from being displayed, including ECHO OFF itself.
ECHO displays a message during batch file execution when you specify ECHO message. Use multiple ECHO statements to display more than one line of message text.
If you type a period immediately after ECHO in a batch file, a blank line appears when you run the batch file.
FOR
Format
FOR %%variable IN (fileset) DO command
Explanation
FOR executes the specified command for each file in a set of files. In sequence, each filename is substituted for the command.
You can use wildcards in filenames; all matching filenames on the current drive will be substituted.
Example
The following statement in a batch file types two files to the screen in turn:
FOR %%f IN (ACCT.BAS ACCT2.BAS) DO TYPE %%f
GOSUB
Format
GOSUB label
Explanation
GOSUB causes batch file execution to jump to the specified label, process commands until RETURN is found, and then go back to the line immediately after GOSUB and continue processing from there. You must use a RETURN with GOSUB.
Example
The following batch file types a text file to the screen and then runs a subroutine to type three more text files before calling a second batch file.
TYPE INTRO.TXT
GOSUB LISTS
CALL SUMMARY
EXIT
:LISTS
TYPE LISTA.TXT
TYPE LISTB.TXT
TYPE LISTC.TXT
RETURN
GOTO
Format
GOTO label
Explanation
GOTO causes batch file execution to jump to the specified label and continue from there.
You must include the label referenced by a GOTO. If the label is not found, you see the following error message:
Label xxxxxxxx not found
Note that only the first eight characters of a label are recognized.
Example
The following batch file displays a file called INFO.TXT over and over again until you stop the file by pressing <Ctrl> C or <Ctrl><Break>.
:START
TYPE INFO.TXT
GOTO START
IF
Format
IF [NOT] condition1 [OR [NOT] condition2] [AND [NOT] condition3] command
Explanation
IF specifies conditional execution of commands within a batch file. The command is executed when the condition is true. When the condition is not true (or true, if you specify IF NOT), command is ignored and the next statement in the batch file is executed.
The conditions can be any of the following:
ERRORLEVEL n
This condition is true when the error code produced by the previous program is greater than or equal to n (where n is a number).
string1 =|==|!=|<> string2
This condition is true when the relationship between the specified string (string1) and another string (string2) matches the specified symbol: "identical to" (= or ==) or "not identical to" (!= or <>). Strings can be literal strings or batch variables (%hour%, for example).
#value1 =|==|!=|<> #value2
This condition is true when the relationship between the absolute value of the specified variables matches the specified symbol.
EXIST filespec
This condition is true when filespec exists, in the specified directory or in the current directory (if no dirpath is specified in filespec).
DIREXIST dirpath
This condition is true when dirpath exists, in the specified directory or in the current directory (if no dirpath is specified in dirpath).
Examples
The following batch file uses several IF statements to test the ERRORLEVEL from an XCOPY operation:
REM XCOPY and DEL with ERRORLEVEL Testing
XCOPY %1 %2
IF ERRORLEVEL 5 GOTO FAIL5
IF ERRORLEVEL 4 GOTO FAIL4
IF ERRORLEVEL 3 GOTO FAIL2
IF ERRORLEVEL 2 GOTO FAIL1
DEL %1
GOTO END
:FAIL1
ECHO Files %1 not found - not deleting files
GOTO END
:FAIL2
ECHO Aborted - not deleting files
GOTO END
:FAIL4
ECHO Could not start XCOPY - not deleting files
GOTO END
:FAIL5
ECHO Disk write failure - not deleting files
:END
The following IF statement specifies that WHERE IS JOHN? displays only if JOHN is not the current value of the %1 parameter. Note that the strings are enclosed by quotation marks; you should always use quotation marks around strings to prevent syntax errors, especially when no parameters are specified:
IF "%1"<>"JOHN" ECHO WHERE IS JOHN?
The IF statement in the following set of statements means that the commands between DIR B: and the label XYZ will only be executed if MYFILE does not exist in the current directory:
IF EXIST MYFILE GOTO XYZ
DIR B:
.
.
:XYZ
.
.
PAUSE
Format
PAUSE [remark]
Explanation
PAUSE delays batch file processing so that an action can be performed, such as changing diskettes.
PAUSE always displays the following message:
Strike a key when ready . . .
You can specify an additional remark of up to 122 characters; this is optional, however.
Example
The following batch file includes a PAUSE command with a remark specified:
COPY A:*.* B:
@ECHO OFF
PAUSE Insert new disk in DRIVE A:
COPY A:*.* B:
When this batch file runs, the files on the diskette in the A: drive are copied to the diskette in the B: drive. Processing is then suspended and the following message appears on the screen:
Insert new disk in DRIVE A:
Strike a key when ready . . .
You can either insert a new diskette and press any key to continue or press <Ctrl> C to stop batch file processing.
REM
Format
REM | ; [remark]
Explanation
REM or a semi-colon (;) causes the operating system to treat a statement as a comment or remark. Any line in a batch file preceded by REM or a semi-colon is not processed.
NOTE: If ECHO is ON, REM lines will be displayed on the screen as the batch file executes.
The maximum length of remark is 123 characters. You can use commas, spaces, and tabs to separate words. Empty comment lines (REM with no remark) are useful for adding space to a batch file, making it easier to read and edit.
RETURN
Format
RETURN
Explanation
RETURN causes batch file execution to return to sequential processing of commands after a GOSUB or SWITCH statement.
SHIFT
Format
SHIFT
Explanation
SHIFT allows you to access more than nine command line parameters (%1 to %9) by shifting their position to the left.
When SHIFT is executed, the value in %0 is replaced by the value in %1, %1 is replaced by %2, %2 by %3, and so on up to %8, which is replaced by the value of %9.
Use SHIFT as many times as you need to get all of the parameters in the command line, up to a maximum of 10 at a time. Think of the batch parameters as a "window"; SHIFT simply moves the window one position to the right.
Example
SHIFT is most useful when you are using a batch file to perform a single operation many times and using a varying number of command line parameters.
The batch file COPYX, for example, is designed to copy a number of files to a single destination directory; you would enter the following to run COPYX:
COPYX filename1 filename2 filename3... dirpath
COPYX.BAT would be written as follows:
@ECHO OFF
IF "%1" == "/?" GOTO HELP
REM COPYX requires at least 2 parameters if
REM the first parameter is not /?
IF "%2" == "" GOTO FAIL
SET COPYX_FILE =
:LOOP
REM If the second parameter is empty, the
REM first parameter must be the destination
IF "%2" == "" GOTO NO_MORE_FILES
REM Otherwise, add the first parameter to the
REM list of files to copy
SET COPYX_FILE = %COPYX_FILE% %1
REM Now SHIFT the parameters so that %1 is
REM replaced by %2, etc.
SHIFT
REM Go back to look at the rest of the
REM parameters
GOTO LOOP
:NO_MORE_FILES
REM The first parameter now contains the
REM destination
REM COPYX_FILE contains the list of files to
REM copy
FOR %%i IN (%COPYX_FILE%) DO XCOPY %%i %1
GOTO END
:FAIL
ECHO COPYX: At least 2 parameters are required
GOTO END
:HELP
ECHO COPYX: Multiple file copy program
ECHO Enter: COPYX filename1 filename2... dirpath
GOTO END
:END
SET COPYX_FILE =
SWITCH
Format
SWITCH label1, label2[, label3... label9]
Explanation
SWITCH causes batch file processing to switch between several different subroutines within the same batch file, depending on the information (label) provided by the user. SWITCH also causes the batch file to prompt the user for this information. In effect, SWITCH creates a simple menu interface.
When the label is entered, execution jumps to that label in the batch file and returns to the line following SWITCH when a RETURN command is encountered.
The maximum number of labels you can use in a SWITCH command is nine.
Example
The following batch file defines a SWITCH routine for selecting an application from a list of applications:
@ECHO OFF
:BEGIN
CLS
ECHO APPLICATIONS MENU
ECHO =================
ECHO 1 Word Processor
ECHO 2 Desktop Publisher
ECHO 3 Database
ECHO 4 Exit
ECHO Choose a number from 1 to 4
SWITCH WP, VP, DB, EX
GOTO BEGIN
:WP
EDIT
RETURN
:VP
CALL VPPROF
RETURN
:DB
CD \DATABASE
DB
RETURN
:EX
What are Macros?
A macro is similar to a batch file; it consists of a number of commands that you execute by entering a single name at the system prompt. The main difference between batch files and macros is that macros are stored in RAM rather than on disk and can be run from any directory. This means that they run faster than batch files. Macros use memory that would normally be used by command line HISTORY information, however. Also, macros are lost when you reboot your computer.
The following sections in this chapter, about using macros, describe further differences between batch files and macros.
Creating Macros Using DOSKEY
The operating system provides the DOSKEY command to work with macros.
To create a macro, simply type the DOSKEY command at the system prompt as follows:
DOSKEY macroname = commands <Enter>
The number of commands you can put in a macro is limited by the length of a single line (127 characters); all the commands in a macro must be on the same line.
Separate each command with $T (or $t). The following command line, for example, defines a macro that lists all the .TXT files in the current directory in order of size before copying them to the root directory of the A: drive:
DOSKEY CPYTXT = XDIR *.TXT /Z $t COPY *.TXT A:\
Besides $t, there are other special characters you can use in a macro. They are listed in the following table.
Note the following restrictions about what you can use in macros:
Editing Macros
You edit a macro by editing the command line you entered to create it. Use the command line HISTORY feature to recall the command line, edit it, and then re-enter it.
Running and Stopping Macros
To run a macro, enter its name at the system prompt, followed by any parameters required. Make sure you leave a space between the macro name and the parameters.
To stop a macro, press <Ctrl> C. This stops the command that is currently executing. Press <Ctrl> C as many times as necessary to stop every command in the macro.
Saving Macros in Batch Files
The best way of saving your macros is to put the commands into a single batch file and then run the batch file whenever you want to make the macros available.
To save macros this way, use the DOSKEY command with the
/macros option and a redirection symbol. The following command, for example, stores all the macros currently stored in memory in a batch file called MACROS.BAT:
DOSKEY /MACROS >MACROS.BAT
Saving macros in batch files also means you can edit them in the future.
[Front] [Prev Chapter]
[Next Chapter]
info@caldera.com
Copyright © 1993, 1997, 1998 Caldera, Inc. All rights
reserved.