Chapter 18. Mastering DOSKEY and Macros

Using DOSKEY

DOSKEY is a program that enables you to edit and reuse DOS commands without retyping them. The program also enables you to create new commands, referred to as macros, that can take the place of several DOS commands. Another benefit of DOSKEY is that it occupies only about 4KB of memory when loaded. The following sections explain how to load DOSKEY into your computer's memory and use the program's capabilities.

Loading DOSKEY

DOSKEY is a memory-resident program that was first available in DOS 5.0. This program enables you to easily edit what you enter at the DOS prompt. DOSKEY also maintains a running history of the commands you have entered. DOSKEY then enables you to reuse those commands without retyping them.

Before you can use DOSKEY's features, you must load the program into memory. To load DOSKEY, enter the following command at the DOS prompt:

DOSKEY

A message appears onscreen, telling you that DOSKEY is installed. After this message appears, all DOSKEY features are available.

Tip

The most convenient way to load DOSKEY is to include a command in your AUTOEXEC.BAT file to start the program. In this way, DOSKEY loads every time you start your computer. It is also easier to take advantage of loading DOSKEY in upper memory.

As is true of most DOS commands, DOSKEY has several available switches. The full syntax of the command to install DOSKEY is as follows:

DOSKEY /REINSTALL /BUFSIZE=size /MACROS /HISTORY /INSERT /OVERSTRIKE

Following are explanations of the components of this command:

  • /REINSTALL—. Installs another copy of DOSKEY and clears the command-history buffer. This command does not, however, remove existing copies of DOSKEY that already are in memory.

  • /BUFSIZE—. Sets the size of the command buffer. The size parameter represents the number of bytes that the buffer occupies in memory. The default size is 512 bytes; the minimum size is 256 bytes.

  • /MACROS—. Displays a list of the currently defined DOSKEY macros.

  • /HISTORY—. Displays the contents of the command-history buffer.

  • /INSERT—. Instructs DOS to insert new text into the existing text at the cursor position. (You cannot use this switch with /OVERSTRIKE.)

  • /OVERSTRIKE—. Instructs DOS to insert new text in place of existing text at the cursor position. This condition is the default. (You cannot use this switch with /INSERT

Editing the Command Line

Even before you load DOSKEY, DOS provides some command-line editing capability. Table 18.1 lists the normal DOS command-line editing keys, which are available if DOSKEY is not memory-resident.

Table 18.1. DOS Command-Line Editing Keys

Key Action
Tab Moves the cursor to the following tab stop
Esc Cancels the current line and does not change the buffer
Ins Enables you to insert characters into the preceding command line
Del Deletes a character from the preceding command line
F1 or right arrow Copies one character from the preceding command line
F2 Copies all characters from the preceding command line up to, but not including, the next character you type
F3 Copies all remaining characters from the preceding command line
F4 Deletes all characters from the preceding command line up to, but not including, the next character typed (the opposite of F2)
F5 Moves the current line into the buffer but prevents DOS from executing the line
F6 Produces an end-of-file marker (^Z) when you copy from the console to a disk file

A primary purpose of DOSKEY is to facilitate editing of DOS commands. If you are a typical PC user, you issue the same or similar commands frequently, and you don't always type each command correctly the first time. DOSKEY can save you typing by enabling you to edit commands without typing them from scratch every time you notice an error.

Suppose that you want to see a directory listing of all files with the .WQ1 extension in the SPREADSHQPRO2DAT directory in drive C. In haste, however, you type the DIR command as follows:

DOR C:SPREADSHQPRO2DAT*.WQ1

Before you press Enter, you realize that you mistyped the DIR command, but you don't want to retype it. When DOSKEY is loaded, you can use the following procedure to correct the mistake:

  1. Press the Home key to move the cursor to the left end of the command line.

  2. Use the right-arrow key to move the cursor to the O in the word DOR.

  3. Type I to correct the error.

  4. Press Enter.

DOS displays the directory listing as you requested.

Tip

Another quick way to repeat the command is to press the F1 key. Each time it is pressed, it repeats a character of the previous command. Although this technique is more limited, in that it does not enable you to edit a character and then continue repeating, it can be helpful if DOSKEY was not activated before you typed the command.

The keys listed in Table 18.2 supplement the normal DOSKEY command-line editing keys.

Table 18.2. Additional DOSKEY Command-Line Editing Keys

Key Action
Left arrow Moves the cursor one character to the left
Right arrow Moves the cursor one character to the right
Backspace Moves the cursor one character to the left; erases the character to the left
Ctrl+left arrow Moves the cursor one word to the left
Ctrl+right arrow Moves the cursor one word to the right
Ins Toggles between Replace mode (the default) and Insert mode
Home Moves the cursor to the left end of the command line
End Moves the cursor to the space after the last character in the command line
Esc Erases the command line

Reusing Commands

In addition to enhancing DOS's command-line editing capabilities, DOSKEY adds a capability that was not previously available in DOS: You can redisplay a command that you issued earlier during the current DOS session. You then can execute the command without changing it, or you can use the DOS and DOSKEY editing keys to make modifications before executing the command.

After you load DOSKEY into memory, the program maintains a buffer in memory that contains a history of DOS commands issued at the command prompt during the current DOS session. DOSKEY enables you to reuse the commands in this command-history buffer.

Suppose that earlier during the current DOS session, you issued the following COPY command:

COPY C:DATABASEFOXPROMAIL.DBF C:WORDPROWP

Now you want to issue the following similar command without retyping the entire command:

COPY C:DATABASEDBASEMAIL.DBF C:WORDPROWP

To edit the earlier command, follow these steps:

  1. Press the up-arrow key repeatedly until the original COPY command appears on the command line.

  2. Use the DOS and DOSKEY editing keys to change the command.

  3. Press Enter.

In addition to the up-arrow key, DOSKEY provides the keys listed in Table 18.3 for use in retrieving commands from the command-history buffer.

Table 18.3. DOSKEY Command-History Buffer Keys

Key Action
Up arrow Displays] the preceding DOS command.
Down arrow Displays the DOS command issued after the one currently displayed, or displays a blank line when you are at the end of the list.
Alt+F7 Clears the command-history buffer.
Alt+F10 Clears all macro definitions.
F7 Displays the contents of the command-history buffer in a numbered list.
F8 Searches for the command that most closely matches the characters typed at the command line.
F9 Prompts for a line number, where line number refers to the number displayed next to a command in the command-history listing generated by pressing F7. Type the number to display the corresponding command.
PgDn Displays the last command stored in the DOSKEY command buffer.
PgUp Displays ]the earliest command issued that still is stored in the DOSKEY command buffer.

To view the entire list of commands currently stored in the command-history buffer, press F7. DOSKEY lists all commands contained in the buffer, one on each line, with a number at the left end of each line. The oldest command—the command issued earliest in the current DOS session—is number one. Subsequent commands are listed in the order in which you issued them.

DOSKEY provides another way for you to see the entire list of commands in the command-history buffer. Type the following command and then press Enter:

DOSKEY /HISTORY

DOSKEY generates the same list of commands as the F7 command, but without line numbers.

To create a batch file that contains all the commands in the current command-history buffer, use the following command syntax:

DOSKEY /HISTORY > filename.BAT

Substitute for filename the name that you want to give the batch file. After you issue this command, the new batch file contains all the commands from the command-history buffer, including the command that created the batch file itself. Use the DOS Editor, Edlin, or some other text editor to delete the last command and any other commands you don't want to include in the batch file.

You can use the up-arrow key to display previously issued commands. Each time you press the up-arrow key, DOSKEY displays the preceding command. After you display one or more previous commands by pressing the up-arrow key, you can use the down-arrow key to move back down through the commands to the most recent command. Sometimes, however, selecting a command from the list generated by pressing F7 is easier. To use this method, press F9. DOSKEY displays the following message:

Line number:

Type the number that corresponds to the desired command in the list of commands generated by pressing F7. DOSKEY displays the selected command in the command line for you to edit or execute.

When you want to move quickly to the first command in the buffer, press PgUp. To go to the last command in the buffer, press PgDn.

If you want to clear the command-history buffer, press Alt+F7. DOSKEY abandons the contents of the command-history buffer.

DOSKEY also can help you locate a command quickly. Type the first several characters of the command you need to find and then press F8. Suppose that you want to locate the following command:

COPY C:DATABASEFOXPROMAIL.DBF C:WORDPROWP

Type COPY and press F8. Each time you press F8, DOSKEY shows you the next command that contains the COPY command. When the desired command is displayed, you easily can edit and reuse the command with minimal typing.

Creating and Using Macros

In addition to providing command-line editing capabilities and the command-history buffer, DOSKEY enables you to create your own DOS commands, referred to as macros. A DOSKEY macro is similar to a batch file but is contained in memory rather than on disk. Each macro can contain one or more DOS commands, up to a maximum of 127 characters.

DOSKEY macros are similar to batch files in the following ways:

  • Macros can contain multiple DOS commands.

  • You invoke macros by typing a name at the DOS prompt.

  • Macros can use replaceable parameters.

Macros differ from batch files in the following ways:

  • Macros are stored in memory (RAM); batch files are stored on disk.

  • Macros are limited to 127 characters; batch files have unlimited maximum length.

  • Ctrl+C or Ctrl+Break stops a single command in a DOSKEY macro; Ctrl+C or Ctrl+Break stops an entire batch file.

  • The GOTO command is not available in macros.

  • One macro cannot call another macro, and you cannot call a macro from within a batch file.

  • Macros can define environment variables but cannot use them.

The following sections explain how to create and run DOSKEY macros.

Creating Macros

DOSKEY enables you to create macros at the command line or through a batch file. The syntax for creating a macro is as follows:

DOSKEY macroname=command(s)

The macroname parameter is the name that you want to give the macro. Use any keyboard characters in the name except <, >, |, or =. Do not include a space in the macro name; use an underscore or hyphen instead if you want the macro name to have the appearance of two words.

The command parameter can include any number of DOS commands, subject to the following rules:

  • The entire command cannot exceed 127 characters (the DOS command-line limit).

  • Each pair of commands must be separated by $t.

  • Instead of using the redirection and piping operators <, >, and |, use $l, $g, and $b, respectively.

  • The ECHO OFF command is not effective in macros. Commands always appear onscreen.

You can use several special characters when defining your macros. They are shown in Table 18.4.

Table 18.4. Special Characters for Use in DOSKEY Macros

Character Meaning
$G or $g Redirects output; used in place of the greater-than sign (>)
$G$G or $g$g Redirects and appends output; used in place of the double greater-than sign (>>)
$L or $l Redirects input; used in place of the less-than sign (<)
$B or $b Uses output from one command as input to another; used in place of the vertical bar (|)
$T or $t Separates commands on the DOSKEY command line
$$ Prints the dollar sign character
$1 through $9 Indicates individual replaceable parameters
$* Indicates all parameters

Notice that when you want to use replaceable parameters in a macro, you use the codes $1 through $9 rather than %1 through %9. Suppose, for example, that you often use the REPLACE command to keep current copies of particular subdirectory files on floppy disks so that you can take the files with you. To do so, however, you must issue the REPLACE command twice for each subdirectory you want to keep current. An easier method is to create the following macro called UPD:

DOSKEY UPD=REPLACE C:$1$2 A:$1 /U $T REPLACE C:$1$2 A:$1 /A

In this example, you also see how the $T (or $t) special characters separate commands on the command line.

DOSKEY has a special type of replaceable parameter that is not available in batch files. The characters $* represent not just one parameter, but all the characters you type in the command line to the right of the macro name. This type of replaceable parameter is useful when you don't know ahead of time how many parameters or switches you might type when you execute the macro.

Suppose, for example, that you want to create a macro to help format floppy disks in drive A, a 3 1/2-inch, high-density drive. You want to be able to type FA 720 to format a 720KB disk and FA 1.44 to format a 1.44MB disk. Occasionally, however, you might want to use one or more of FORMAT's switches, such as the /S switch to create a system disk, the /Q Quick Format switch, or the /U unconditional format switch. To create the FA macro, you type the following command:

DOSKEY FA=FORMAT A: /F:$*

To confirm that DOSKEY has stored the macros you defined, you type the following command and then press Enter:

DOSKEY /MACROS

DOSKEY lists all macros currently stored in the DOSKEY macro buffer. Assuming you defined the UPD and FA macros, the preceding command displays the following lines:

UPD=REPLACE C:$1$2 A:$1 /U $T REPLACE C:$1$2 A:$1 /A
FA=FORMAT A: /F:$*

You can easily save a copy of the entire contents of the macro buffer by using redirection. To create a file named MACROS.BAT that contains all the current macros, you type the following command and press Enter:

DOSKEY /MACROS > MACROS.BAT

If you want to use this batch file later to re-create the macros during a future session, edit the file, adding DOSKEY to the beginning of each line.

Because DOSKEY macros reside in memory rather than on disk, all macros are erased when you turn off or reboot the computer. One disadvantage of using DOSKEY macros is that you need to reenter commonly used macros each time you turn on your computer. You can overcome this drawback, however, by using AUTOEXEC.BAT to define the macros that you use most often. To make the UPD and FA macros routinely available, for example, include the following commands in AUTOEXEC.BAT:

DOSKEY UPD=REPLACE C:$1$2 A:$1 /U $T REPLACE C:$1$2 A:$1 /A
DOSKEY FA=FORMAT A: /F:$*

Every time you turn on or reboot your computer, the preceding commands create the UPD and FA macros in the DOSKEY macro buffer.

Note

The first DOSKEY command in AUTOEXEC.BAT loads the program as memory-resident, even if the command also is defining a macro.

Running Macros

Using a DOSKEY macro is as easy as using any other DOS command. Simply type the macro name at the command line and then press Enter. If the macro has any replaceable parameters, include appropriate values in the command line. Suppose, for example, that you want to use the UPD macro to maintain current copies of files. To do so, just type the following:

UPD dirname filespec

As with a batch file, dirname is the first replaceable parameter, $1, and filespec is the second, $2. To keep current all files in C:PROJECT1, type the following at the DOS prompt:

UPD PROJECT1 *.*

Perhaps you want to use the FA macro created in the preceding section to format a 1.44MB disk. You want to use Quick Format, make this disk bootable, and assign the volume label BOOT_DISK. You type the following command at the DOS prompt and then press Enter:

FA 1.44 /Q /S /V:BOOT_DISK

DOS first displays the command in the following format:

FORMAT A: /F:1.44 /Q /S /V:BOOT_DISK

Then DOS prompts you as follows:

Insert new diskette for drive A:
and press ENTER when ready_

Press Enter to proceed with the formatting operation. DOS displays messages indicating the progress and successful completion of the procedure. Finally, DOS displays the following message:

QuickFormat another (Y/N)?

Press Y if you want to use the same switch settings to format another disk or press N to return to the DOS prompt.

Deleting Macros

You can remove a macro from memory simply by putting nothing to the right of the equal sign when you define the macro. Assume, for example, that you no longer need the FA macro defined earlier. To remove it from memory—thereby freeing more space for other macros—type the following at the DOS prompt:

DOSKEY FA=

Warning

If you use a macro name that is the same as an existing DOS command, you effectively replace the DOS command with the macro.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.172.130