One way to simplify batch file programming is to use aliases to hide unnecessary detail inside a batch file. For example, suppose you want a batch file to check for certain errors, and display a message and exit if one is encountered. This example shows one way to do so:



unalias *

alias error `echo. & echo ERROR: %$ & goto dispmenu`

alias fatalerror `echo. & echo FATAL ERROR: %$ & quit`

alias in `pushd %1 & %2$ & popd`

if not exist setup.btm fatalerror Missing setup file!

call setup.btm




1. Word Processing

2. Solitaire

3. Internet

4. Exit



inkey Enter your choice:  %%userchoice

switch %userchoice

case 1

  input Enter the file name:  %%fname

  if not exist fname error File does not exist

  in d:\letters c:\windows\wordpad.exe

case 2

  in d:\finance c:\windows\sol.exe

case 3

  in d:\comm c:\windows\iexplore.exe

case 4

  goto done


 error Invalid choice, try again


goto dispmenu




The first alias, ERROR, simply displays an error message and jumps to the label DISPMENU to redisplay the menu. The %$ in the second ECHO command displays all the text passed to ERROR as the content of the message. The similar FATALERROR alias displays the message, then exits the batch file.


The last alias, IN, expects 2 or more command line parameters. It uses the first as a new working directory and changes to that directory with a PUSHD command. The rest of the command line is interpreted as another command plus possible command line parameters, which the alias executes. This alias is used here to switch to a directory, run an application, and switch back. It could also be used from the command line.


The following 9 lines print a menu on the screen and then get a keystroke from the user and store the keystroke in an environment variable called userchoice. Then the SWITCH command is used to test the user's keystroke and to decide what action to take.


There's another side to aliases in batch files. If you're going to distribute your batch files to others, you need to remember that they may have aliases defined for the commands you're going to use. For example, if the user has aliased CD to CDD and you aren't expecting this, your file may not work as you intended. There are two ways to address this problem.


The simplest method is to use SETLOCAL, ENDLOCAL, and UNALIAS to clear out aliases before your batch file starts, and SETDOS to select the special characters you depend on, and restore them at the end, as we did in the previous example. Remember that SETLOCAL and ENDLOCAL will save and restore not only the aliases but also the environment, the current drive and directory, and various special characters.


If this method isn't appropriate or necessary for the batch file you're working on, you can also use an asterisk * before the name of any command. The asterisk means the command that follows it should not be interpreted as an alias. For example the following command redirects a list of file names to the file FILELIST:


dir /b > filelist


However, if the user has redefined DIR with an alias this command may not do what you want. To get around this just use:


*dir /b > filelist


The same can be done for any command in your batch file. If you use the asterisk, it will disable alias processing, and the rest of the command will be processed normally as an internal command, external command, or batch file. Using an asterisk before a command will work whether or not there is actually an alias defined with the same name as the command. If there is no alias with that name, the asterisk will be ignored and the command will be processed as if the asterisk wasn't there.