Skip to main content

Batch Script Alias

Batch Script Alias

Alias means creating shortcuts or keywords for existing commands.

Suppose we want to run the following command, which is nothing more than the directory list command with the /w option to not show all the necessary details in a directory list.

dir /w

Suppose if we were to create a shortcut to this command as follows.

dw = dir /w

When we want to execute the dir /w command, we can simply type in the word dw. The word dw has now become an alias to the command dir /w.

Creating an Alias

Alias are managed by using the doskey command.

doskey [options] [macroname=[text]]

where

  • macroname: a short name for the macro.
  • text: the commands you want to recall.

Following are the description of the options which can be presented to the doskey command.

OptionsDescription
/reinstallInstalls a new copy of Doskey
/listsize = sizeSets size of command history buffer.
/macrosDisplays all Doskey macros.
/macros:allDisplays all Doskey macros for all executables which have Doskey macros.
/macros:exenameDisplays all Doskey macros for the given executable.
/historyDisplays all commands stored in memory.
/insertSpecifies that new text you type is inserted in old text.
/overstrikeSpecifies that new text overwrites old text.
/exename = exenameSpecifies the executable.
/macrofile = filenameSpecifies a file of macros to install.
macronameSpecifies a name for a macro you create.
textSpecifies commands you want to record.

Example

Create a new file called keys.bat and enter the following commands in the file. The below commands creates two aliases, one if for the cd command, which automatically goes to the directory called test. And the other is for the dir command.

keys.bat
@echo off
doskey cd = cd/test
doskey d = dir

Once the command is executed, you will be able to run these aliases in the command prompt.

Deleting an Alias

An alias or macro can be eliminated by setting the value of the macro to NULL.

Example

In the following example, we are first setting the macro d to d = dir. After which we are setting it to NULL. Because we have set the value of d to NULL, the macro d will be deleted.

@echo off
doskey cd = cd/test
doskey d = dir
d=

Replacing an Alias

An alias or macro can be replaced by setting the value of the macro to the new desired value.

Example

In the above example, we are first setting the macro d to d = dir. After which we are setting it to dir /w. Since we have set the value of d to a new value, the alias d will now take on the new value.

@echo off
doskey cd = cd/test
doskey d = dir

d = dir /w