Batch Script - Alias
Aliases mean creating shortcuts or keywords for existing commands. Suppose if we wanted to run the command below which is nothing more than the directory listing command with the / w option to not show all the necessary details in a directory listing.
Dir / w
Suppose we need to create a shortcut to this command as follows.
dw = dir / w
When we want to run dir / w command, we can just type the word dw . The word "dw" has now become an alias for the Dir command /w.
Creating an alias
Aliases are managed using the doskey .
DOSKEY [options] [macroname = [text]]
Here is the deion of options that can be presented to the DOSKEY command.
| S.No. || Options & Deion |
| 1. || |
Install a new copy of Doskey
| 2. || |
/ LISTSIZE = size
Sets the size of the command history buffer.
| 3. || |
Show all Doskey macros.
| 4. || |
/ MACROS: ALL
Ays displays all Doskey macros for all executables that have Doskey macros.
| 5. || |
/ MACROS: exename
Show all Doskey macros for executionutable given.
| 6. || |
Displays all commands stored in memory.
| 7. || |
Specifies that new text you type is inserted into the old text.
| 8. || |
Specifies that new text overwrites old text.
| 9. || |
/ EXENAME = exename
Specifies the executable.
| 10. || |
/ MACROFILE = filename
Specifies a macro file to install.
| 11. || |
Specifies a name for a macro you create.
| 12. || |
Specifies the commands you want to record.
Create anew file called keys.bat and enter the following commands into the file. The commands below create two aliases, one 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 the command is executed, you can execute these aliases in the command prompt.
The following screenshot shows that after executing the batch file created above, you can freely enter the command 'd ' and it will give you the list of directories which signifies your alias has been created.
Removing an alias
An alias or a macro can be removed by setting the macro value to NULL.
@echo off doskey cd = cd / test doskey d = dir d =
In the example above, we are the first to set the macro d to d = dir. After which, we set it to NULL. As wehave set the value of d to NULL, the macro d will be deleted.
Replacing an alias
An alias or macro can be replaced by setting the value of the macro to the new desired value.
@ech o off doskey cd = cd / test doskey d = dir d = dir / w
In In the example above, we first define the macro d to d = dir. After that, we set it to dir / w. Since we have set the value of d to a new value, the alias "d" will now take the new value.