[Toc][Index]

Aliases


Much of the power of CMD.EXE comes together in aliases, which give you the 
ability to create your own commands.  An alias is a name that you select 
for a command or group of commands.  Simple aliases substitute a new name 
for an existing command.  More complex aliases can redefine the default 
settings of internal or external commands, operate as very fast in-memory 
batch files, and perform commands based on the results of other commands. 
This section will show you some examples of the power of aliases.  See the 
ALIAS command for complete details about writing your own aliases. 
The simplest type of alias gives a new name to an existing command.  For 
example, you could create a command called ROOT which uses CD to switch to 
the root directory this way: 


        [c:\] alias root = cd \

After the alias has been defined this way, every time you type the command 
ROOT, you will actually execute the command CD \. 
Aliases can also create customized versions of commands.  For example, the 
DIR command can sort a directory in various ways.  You can create an alias 
called DE that means "sort the directory by filename extension, and pause 
after each page while displaying it" like this: 


        [c:\] alias de = dir /oe /p

Aliases can be used to execute sequences of commands as well.  The 
following command creates an alias called W which saves the current drive 
and directory, changes to the WP directory on drive C, runs the program 
E:\WP60\WP.EXE, and, when the program terminates, returns to the original 
drive and directory: 


        [c:\] alias w = `pushd c:\wp & e:\wp60\wp.exe & popd`

This alias is enclosed in back-quotes because it contains multiple 
commands. You must use the back-quotes whenever an alias contains multiple 
commands, environment variables, parameters (see below), redirection, or 
piping.  See the ALIAS 
Aliases can be nested, that is, one alias can invoke another.  For 
example, the alias above could also be written as: 


        [c:\] alias wp = e:\wp60\wp.exe
        [c:\] alias w = `pushd c:\wp & wp & popd`

If you enter W as a command, the command processor will execute the PUSHD 
command, detect that the next command (WP) is another alias, and execute 
the program E:\WP60\WP.EXE, and - when the program exits - return to the 
first alias, execute the POPD command, and return to the prompt. 
You can use aliases to change the default options for both internal 
commands and external commands.  Suppose that you always want the DEL 
command to prompt before it erases a file: 


        [c:\] alias del = *del /p

An asterisk [*] is used in front of the second "del" to show that it is 
the name of an internal command, not an alias.  See ALIAS for more 
information about this use of the asterisk. 
You may have a program on your system that has the same name as an 
internal command.  Normally, if you type the command name, you will start 
the internal command rather than the program you desire, unless you 
explicitly add its full path on the command line.  For example, if you 
have a program named LIST.COM in the C:\UTIL directory, you could run it 
with the command C:\UTIL\LIST.COM.  However, if you simply type LIST, the 
internal LIST command will be invoked instead.  Aliases give you two ways 
to get around this problem. 
First, you could define an alias that runs the program in question, but 
with a different name: 


        [c:\] alias l = c:\util\list.com

Another approach is to rename the internal command and use the original 
name for the external program.  The following example renames the LIST 
command as DISPLAY and then uses a second alias to run LIST.COM whenever 
you type LIST: 


        [c:\] alias display = *list
        [c:\] alias list = c:\util\list.com

You can also assign an alias to a key, so that every time you press the 
key, the command will be invoked.  You do so by naming the alias with an 
at sign [@] followed by a key name.  After you enter this next example, 
you will see a 2-column directory with paging whenever you press Shift-F5, 
then Enter: 


        [c:\] alias @Shift-F5 = *dir /2/p

This alias will put the DIR command on the command line when you press 
Shift-F5, then wait for you to enter file names or additional switches. 
 You must press Enter when you are ready to execute the command.  To 
execute the command immediately, without displaying it on the command line 
or waiting for you to press Enter, use two at signs at the start of the 
alias name: 


        [c:\] alias @@Shift-F5 = *dir /2/p

The next example clears the screen whenever you press Alt-F1: 


        [c:\] alias @@Alt-F1 = cls

Aliases have many other capabilities as well.  This example creates a 
simple command-line calculator using the @EVAL function.  Once you have 
entered the example, you can type CALC 4*19, for example, and you will see 
the answer: 


        [c:\] alias calc = `echo The answer is:  %@eval[%&]`

Our last example in this section creates an alias called IN.  It will 
temporarily change directories, run an internal or external command, and 
then return to the current directory when the command is finished: 


        [c:\] alias in = `pushd %1 & %2& & popd`

Now if you type 


        [c:\] in c:\letters wp letter.txt

you will change to the C:\LETTERS subdirectory, execute the command WP 
LETTER.TXT and then return to the current directory. 
The above example uses two parameters:  %1 means the first argument on the 
command line, and %2& means the second and all subsequent arguments. 
Parameters are explained in detail under the ALIAS command. 
Your copy of CMD.EXE includes a sample alias file called ALIASES which 
contains several useful aliases and demonstrates many alias techniques. 
See the ALIAS and UNALIAS commands for more information and examples. 
 Also see Using Aliases in Batch Files. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs