Batch Script - Aliases
Aliases means creating shortcuts or keywords for existing commands. Suppose if we wanted to execute the below command which is nothing but the directory listing command with the /w option to not show all of the necessary details in a directory listing.
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]]
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.
|S.No.||Options & Description|
Installs a new copy of Doskey
/LISTSIZE = size
Sets size of command history buffer.
Displays all Doskey macros.
Displays all Doskey macros for all executables which have Doskey macros.
Displays all Doskey macros for the given executable.
Displays all commands stored in memory.
Specifies that new text you type is inserted in old text.
Specifies that new text overwrites old text.
/EXENAME = exename
Specifies the executable.
/MACROFILE = filename
Specifies a file of macros to install.
Specifies a name for a macro you create.
Specifies commands you want to record.
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.
@echo off doskey cd = cd/test doskey d = dir
Once you execute the command, you will able to run these aliases in the command prompt.
The following screenshot shows that after the above created batch file is executed, you can freely enter the ‘d’ command and it will give you the directory listing which means that your alias has been created.
Deleting an Alias
An alias or macro can be deleted by setting the value of the macro to NULL.
@echo off doskey cd = cd/test doskey d = dir d=
In the above 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 deleted.
Replacing an Alias
An alias or macro can be replaced by setting the value of the macro to the new desired value.
@echo off doskey cd = cd/test doskey d = dir d = dir /w
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.