Command.com
|
- The title of this article is incorrect because of technical limitations. The correct title is command.com.
command.com is the name for the default operating system shell (or command line interpreter) for DOS and some versions of Windows. It also has an additional role, as the first program run after boot, hence being responsible for setting up the system as specified in the autoexec.bat configuration file, 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 immediately. The second is the batch mode, which executes a predefined sequence of commands stored as a text file with the extension .bat. Its function as the default command interpreter is analogous to that of the Unix shells, although command.com's functionality is considerably more limited than that of its Unix counterparts.
Contents |
Internal commands
This section does not try to give a full overview of the command.com syntax, but rather serves as an overview and a mnemonic for the most common and interesting features. 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 current directory.
- 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)
- REN, RENAME
- Renames a file or directory
- DEL, ERASE, DELETE
- Deletes a file. When used on a directory, deletes all files in that directory, but does not recurse or delete the directory itself.
- MD, MKDIR
- Creates a new directory
- RD, RMDIR
- Removes an empty directory
- VOL
- Shows information about a volume
- LABEL
- Show or change the label of a volume
- VERIFY
- Enable or disable verification of writing for files
- TYPE
- Display the content of a file on the console
Other commands
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.
- BREAK
- Controls the handling of program interruption with Ctrl+C.
- CLS
- Clears the screen.
- CTTY
- Defines the device to use for input and output.
- DATE
- Set the date of the system.
- ECHO
- Toggles whether text is displayed (ECHO ON) or not (ECHO OFF). Also displays text on the screen (ECHO text).
- PATH
- Displays or change the value of the PATH environment variable which controls the places where executable files are searched.
- PAUSE
- Halts execution of the program and displays a message asking the user to press any key to continue.
- PROMPT
- Displays or change the value of the PROMPT internal variable which controls the appearance of the prompt.
- SET
- Sets the value of an environment variable ; Without arguments, shows all defined environment variables.
- TIME
- Set the time of the system.
- VER
- Displays the version of the operating system.
Control structures
Control structures are mostly used inside batch files, although they can also be used interactively.
- :label
- Defines a target for GOTO.
- 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
- CALL
- Pauses execution of one batch file, runs another, and returns to the old one and continues.
- EXIT
- Exits from Command.com and return to the program which launched it.
- SHIFT
- Replaces each of the command-line variables with the consequent one (e.g. %0 with %1, %1 with %2 etc. )
Undocumented commands
Some versions of MS-DOS COMMAND.COM recognized some internal commands which were not documented.
- TRUENAME
- Display the "true name" of a file, by bypassing SUBST and ASSIGN filesystem mappings.
Variables
Batch files for command.com can be said to have 4 kinds of variables:
- ERRORLEVEL - contains the return code of the last program to run that sets a value (an integer between 0 and 255). Most programs have a certain convention for their return codes (for instance, 0 for a successful execution). Some programs do not establish a new value, and thus the older value persists after they execute. The value of ERRORLEVEL is tested for range with the IF statement.
- Environment variables - these have the form %VARIABLE% and are associated with values with the SET statement.
- Command-line parameters - these have the form %0, %1...%9, and initially contain the command name and the first nine 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"). The parameters to the right of the ninth can be mapped into range by using the SHIFT statement.
- "For" variables - used by loops, have the format %%a when run in batch files. These variables are defined solely within a specific FOR statement, and iterate over a certain set of values defined in that FOR statement.
See also
External links
- Rob van der Woude's Scripting Pages (http://www.robvanderwoude.com/batchfiles.html)
- Batfiles: The DOS batch file programming handbook & tutorial (http://purl.oclc.org/net/Batfiles/)
- DOS Batch Language: A personal view by Ted E. Davis (http://gearbox.maem.umr.edu/~batch/batchtoc.htm)
- Terry Newton's Batch Programming Stuff (http://www.nc5.infi.net/~wtnewton/batch/index.html)
- Batch Utilities for DOS (http://members.cox.net/dos/batch01.htm)
- William and Linda Allen's Windows 95/98/ME ERRORLEVELs documentation (small ZIP file) (http://www.allenware.com/mcsw/errorlevels.zip)
- Command.com (http://www.command.com) (humorous page)de:Command.com