<<Up     Contents

Command.com

Redirected from MS-DOS Batch File PRogramming

command.com is the name for MS-DOS's default shell (or command line interpreter). It also has an additional role, as the first program being ran after boot, hence being responsible for setting up the system as specified in the autoexec.bat and config.sys configuration files, and being the ancestor of all processes.

As a shell, command.com has two distinct modes of work. First is the interactive mode, in which the user types commands which are then executed in front of his eyes. The second is the batch mode, which executes a pre-defined sequence of commands written down at a text file, with the extension .bat. The original concepts for both functionalities is almost certainly derived from the Unix shells, although most programmers would find command.com less able than its counterparts. However, the batch file format allowed for built-in scripting inside MS-DOS, which was and still is a very useful feature.

Table of contents

Syntax

Note that this section does not try to give a full overview to the syntax of command.com, but rather serve as an overview and a mnemonic for the most common and interesting features. Note that all commands are run only after the Enter key is pressed at the end of the line. command.com is case-insensitive, meaning commands can be typed in either case and are all equivalent (so dir, DIR and DiR will all work in the same way).

Filesystem commands

In accordance with command.com's main function as an operating system shell, it includes a number of built-in commands for working with files.

In order to run a program, simply type the name of its executable and then press "Enter" (it is not necessary to use the extension, e.g. nc.exe can be summoned simply as nc). In order to change the current working drive (see Drive letter assignment), type its letter followed by a colon (D:). Other filesystem commands include:

DIR
Lists the files in the current directory
CD, CHDIR
Changes the current working directory or displays the currect directory. CD is best since it is shorter.
COPY
Copies one file to another (if the destination file already exists, MS-DOS asks whether to replace it). (See also XCOPY[?], an external command that could also copy directory trees)
MOVE
The same as COPY, but removes the original afterwards. (MS-DOS 6 and higher).
DEL, ERASE, DELETE
Deletes a file. DEL is best since it is the shortest. (See also DELTREE[?], an external command which can delete entire directory trees)
MD, MKDIR
Create a new directory
RD, RMDIR
Removes an empty directory

Other commands

Note that all commands from the interactive mode can be used in batch files; similarly, one can use batch-file commands directly on the command.com command line, interactively. Therefore, it is impossible to separate the syntax to "interactive" or "batch" commands.

ECHO
Toggles whether text is displayed (ECHO ON) or not (ECHO OFF). Also displays text on the screen (ECHO text). @ECHO prevents this command from being displayed and only works in a batch file.
SORT
Sorts the output of another command into a file or the like.
SET
Sets the value of an environment variable
PAUSE
Halts execution of the program and displays a message asking the user to press any key to continue.
SHIFT
Replaces each of the command-line variables with the consequent one (e.g. %0 with %1, %1 with %2 etc. - see below for more information on variables).
CHOICE
Presents the user with a menu[?] (MS-DOS 6 and higher). Defaults to Y/N.
CALL
Pauses execution of one batch file, runs another, and returns to the old one and continues.

COMMAND
The COMMAND command starts a new copy of command.com.
EXIT
Exits from a new copy of Command.com, or exits the dos prompt in windows.
VER
This command is built into command.com and displays the current version.

Control structures

Control structures are mostly used inside batch files, although they can also be summoned interactively.

FOR
Iteration: repeats a command for each out of a specified set of files.
GOTO
Moves execution to a specified label. Labels are specified at the beginning of a line, with a colon (:likethis).
REM
Comment: any text following this command is ignored
IF
Conditional statement, allows to branch the program execution

Variables

Batch files for command.com can be said to have 4 kinds of variables:
  1. ERRORLEVEL - contains the return code of the last program to run (an integer between 0 and 255). Most programs have a certain convention for their return codes (for instance, 0 for a successful execution)
  2. Environment variables - these have the form %VARIABLE%
  3. Command-line parameters - these have the form %0, %1...%9, and initially contain the command line parameters passed to the script (e.g., if the invoking command was "myscript.bat John Doe", then %0 is "myscript.bat", %1 is "John" and %2 is "Doe").
  4. "For" variables - used by loops, have the format %%a (there are variables for all letters). Are defined solely within loops, and iterate over a certain set of values (see below).

See also

External links

wikipedia.org dumped 2003-03-17 with terodump