Using Doskey


Doskey
Using Doskey
Index

Doskey has 3 main functions:

  1. Editing the Command Line.
  2. Recalling previous Command Line entries.
  3. Creating Macros (assigning Command Line entries to specified keystrokes).

Once Doskey has been loaded, it runs as a TSR program with no distinctive interface. However, it is very useful when doing anything more than the absolute minimum from the Command Prompt.

Editing the Command Line

Plain Dos stores the last command executed and this can be recalled in its entirety using F3 or character by character with the left and right cursor keys. Plain Dos also allows limited command line editing using a simple, if somewhat idiosyncratic, use of the Insert, Delete, and left/right cursor keys.

DOSKEY considerably expands on this capability, allowing the Command Line to be edited in much the same way as with any other simple line editor. The left/right cursor keys move the cursor back and forth along the line and characters can be inserted and deleted using normal keyboard techniques - bearing in mind that DOSKEY is in overstrike mode by default. For those who are accustomed to editing in "Insert" mode by default, the /i switch should be used as a matter of course when invoking or reloading DosKey.

Recalling Previous Commands

As commands are entered on the Command Line, DOSKEY stores them in a buffer as a sequential list. Commands stored in the buffer can be retrieved in several ways:

  • Use the [Up] cursor key to scroll back through the list and the [Down] key to scroll forward. Each press of the up/down key displays the previous/subsequent command on the Command Line.
  • F7 will display a numbered list of all the commands stored in the buffer. F9 + the command number will display the specified command on the Command Line.
  • Entering one or more characters followed by F8 will display the most recent command beginning with those characters. Pressing F8 again will display the next most recent command beginning with the same characters, and so on.

Notes:

The [Esc] key will clear the Command Line at any time.

A command can be added to the list without actually executing it by typing the command followed by F5.

Should the buffer be filled, the oldest command(s) are displaced from the list. This affects the "Command Number" as displayed using F7 and used with F9. The default buffer size is 512 bytes - sufficient for around 10 to 20 commands (depending on their complexity). Whilst this is usually fine for normal use, an alternative buffer size can be specified using the /b:buffersize switch.

Creating and Using Macros

A DosKey macro comprises one or more Dos commands assigned to a name - which can be any series of characters entered from the keyboard. A very simple macro could assign the command XCOPY c:\data\docs\*.* a: /s to X. Entering X would be exactly equivalent to entering XCOPY c:\data\docs\*.*.

A number of special keys have been designated that allow macros to include multiple commands, user input, and generally incorporate much of the power of batch files.

To create a macro, enter DOSKEY Macroname=Text where Macroname is the series of characters used to call the macro (X in the above example) and Text comprises the command(s) to be listed.

To delete a macro, enter DOSKEY Macroname= or use Alt-F10 to delete all macro definitions stored in the buffer. Entering DOSKEY /R will also clear the buffer of all macros and any stored command lines.

To display a list of macros stored in the buffer, use DOSKEY /M.

Macros are stored in the buffer along with the command line history. The default size for the buffer is 512 bytes (enough for some 10-20 commands/macros) which may be insufficient if several complex macros are to be set up. To provide more room, DosKey can be invoked with a buffer of 1K (or whatever) using the /B switch, eg. DOSKEY /B:1024. However, if DosKey is already running, it must be reloaded (with the /R switch) when changing the buffer size - ie. the previous command would be DOSKEY /R /B:1024 and any macros and/or commands in the old buffer will be lost. Note also that memory allocated to the buffer is not available for use by other programs so it is generally advisable not to specify a larger buffer than is actually required.

DosKey Macro Special Characters

For some reason DosKey macros will not interpret the pipe and redirection symbols used by Dos. To include such symbols in a macro, it is necessary to use certain special characters:

DosKey Special CharacterFunction
$GRedirect output from a command to a file or device - equivalent to the > command line symbol.
$G$GAppend output from a command to the specified file - equivalent to the >> command line symbol.
$LInput to the command is from the specified file or device - equivalent to the < command line symbol.
$BPipes the output from one command to be used as input to another - equivalent to the | command line symbol.
$TUsed to designate a Carriage Return/Line Feed between commands in a multiple line macro.
$$Just in case you should ever want to include a $ sign in a macro.

Variables and User Input

Under normal circumstances, entering a macro name displays that name on the command line, and pressing [Enter] causes the macro to run. If a macro called D referred to the command DIR, for example, entering D A: runs DIR not DIR A:. In other words, any added text is ignored.

The DosKey replaceable parameters ($1 to $9) allow a macro to have up to 9 parameters which are used similarly to batch file variables. In the example above, the command assigned to D would be DIR $1 (DOSKEY D=DIR $1). When calling the macro, it would be possible to add a drive letter as a parameter and D A: would be interpreted as DIR A:. If the parameter was omitted, the command would be interpreted simply as DIR.

In another example, illustrating the use of two variables, the command DIR path > file/device is assigned to a macro called D2F:
DOSKEY D2F=DIR $1 $g $2
where $1 corresponds to the path parameter, and $2 is for the file/device parameter. This macro might be invoked something like D2F C:\data dirdata.txt. If the first DIR parameter is not required (because it refers to the current directory), it is necessary to use a comma so that D2F can recognize the dirdata.txt as parameter $2 rather than $1.

Unfortunately, although a parameter can be a long filename, it cannot include spaces, even if enclosed in inverted commas. If a filename includes spaces, it is necessary to use the 8.3 version of the name. I know of no work around to this.

DosKey macros can also use S* which is termed a "global replaceable parameter". This must be the last part of a macro and is replaced by whatever is on the command line that is not the macro name or assigned to numbered parameters. If the above example was rewritten as:
DOSKEY D2F=DIR $1 $g $*
then long filenames-with-spaces could be used as the final parameter - but the path name used as a substitute for $1 still could not contain directory names with spaces.

Naming Macros

A macro name can comprise any one or more of the letters, numbers, or symbols available on a standard keyboard - so long as they display on the screen.

Names are not case sensitive, and cannot include spaces.

Ctrl + a letter key will usually be valid but some combinations have special significance and are not acceptable. Note: before using any Ctrl keys for names, see the caveat below.

It is not uncommon to create macros which include a single Dos command along with a number of almost-always-used switches and then to name the macro with the base command name. For example, if copying lots of disks using DISKCOPY and always use the parameters A: B:, it is quite reasonable to prepare a macro DISKCOPY A: B: and call it DISKCOPY. Entering DISKCOPY would then actually play DISKCOPY A: B:. If you ever need to actually copy from B: to A:, preceding the command with a space negates the macro and allows full control of the Dos command. This technique can be used both as a time saver and to add one more safety net with some commands - FORMAT comes to mind.
Although it is possible to use a macro to "set the switches" on all commands, sometimes this can also be done using environmental variables (eg. with DIR and COPY). This is generally the preferred route as it takes no buffer space.

It is not possible to create a macro in DosKey named "/?", for example, because Doskey reads this as a switch before thinking if it could be a macro name. However, if you really want to name a macro "/?", this can be done in a text editor and retrieved as described below.

Saving Macro Collections

Because macros are stored in the buffer, it follows that they are lost when the Dos session is ended or if Doskey is reloaded with DOSKEY /R. To save macros across sessions, the list of macros can be redirected to a text file which can be retrieved when required. Thus:
DOSKEY /M > macros.txt
will save the current macros to a text file called "macros.txt". In a later session, these macros can be retrieved using:
DOSKEY /F:macros.txt

Using this procedure makes it easy to have several collections of macros that would be used for specific activities - perhaps one "general purpose" set, a "disk maintenance" set, and so on. Each set being stored in a different text file. Note that retrieving a file of macros adds them to whatever is currently in the buffer, so the "disk maintenance" set does not need to duplicate any of the commands in the "general purpose" set - so long as none of the macros have the same name.

It follows that sets of DosKey macros could be created directly with a text editor.

CAVEAT!! Naming a macro using Ctrl-G is no problem and it will be recalled just as one would expect. BUT when the keystroke Ctrl-G is saved to a text file, it is saved as ^G (carat + G). When the file is retrieved, ^G is NOT converted back to Ctrl-G and the macro can no longer be recalled using Ctrl-G. Bit of a nuisance.

Macros V. Batch Files

Although there is considerable overlap in functionality of DosKey macros and batch files, deciding which route to follow is usually based on the following criteria:

  • Macros are stored in RAM and batch files on disk. It follows that macros will execute faster than batch files.
  • Even allocating a few Ks of conventional/upper memory to Doskey may adversely affect the operation of other programs.
  • Batch programs can be much more complex than macros.

Glossary

Buffer
An area of RAM set aside for storing data. A buffer has a fixed size (that can usually be specified) and a fixed location (allocated by Dos).


This page last revised:
December 9, 1999.