Vernalex.com
 
 M  E  N  U
 - Guides
 - Links
 - Tools
 - Themes
 




 
 

Components Guide: Command Prompt

Included with any good operating system is a command-line interface so that you can execute scripts and perform text-based alternate methods to every task and provide swifter command input than is sometimes possible from a graphical interface. Windows has its own command-line interface although it is argued that it is inferior to Unix based operating systems, such as Linux or FreeBSD derivatives. But, no with the ability to perform Windows Scripting on the command prompt the argument has changed dramatically.

 

History

Before the time of Windows there was the disk operating system (DOS). This was a very simplistic command-line interface operating system (OS) that allowed for the execution of programs with a keyboard. But, a trend began where graphical user interfaces (GUIs) became increasingly popular. Eventually the Windows operating system was born, but it was nothing more than a graphical interfaced program for DOS. This meant that DOS was still quite widely used on PCs and so DOS continued to evolve up until the release Windows 95, which was layered upon the last major release of DOS. With Windows 98 and Windows Me there were a few revisions, but very few changes were made to the core operation. With Windows NT based operating systems the OS no longer runs under DOS, but a command prompt interface is still provided for compatability.

Return to top.

 

Execution

In Windows 9x you can access the command prompt from the programs' menu in the start menu. This is easily done by clicking on the start menu icon, then on the programs menu and then by selecting the icon for the MS-DOS prompt. You can also access it from the run menu in the start menu, which is done by clicking on the start menu and then run. Then you just need to type command and press the okay button. You can also start it by running the file in the command directory. In the Windows folder there should be a command.com file that you can use Windows Explorer to find. It is also available by using special start item options (not directly available in Me). When starting your computer you can use the F8 key when it says Windows is starting, or you can hold down the CTRL key. This will bring up a list with two options that will bring you to the command prompt. The command prompt is also accessible from a boot disk, but that's a more complicated example.

In Windows NT you can access the command prompt from the programs' menu in the start menu. This is easily done by clicking on the start menu icon, then on the programs menu, then by selecting the accessories menu, and then by selecting the icon for the command prompt. You can also execute it from the run menu in the start menu, which is done by clicking on the start menu and then run. Or, you can run it by running the file from the system32 directory. In the system32 folder under the Windows folder there should be a cmd.exe file that you can use Windows Explorer to find. By using special start-up options you can load a special version of the command prompt that is called the recovery console that provides a limited version of the NT command prompt. You can access this by pressing the F8 key a few times before the XP logo screen loads on the OS' startup. This special command prompt can also be accessed from a Windows installation CD (2000 and up only).

Return to top.

 

Syntax

The command prompt has a simple interface and the syntax is just as easy. To execute an item you just use a command followed by optional command-line parameters. The command can either be hard-coded into the command interpreter or it can be an external executable in the specified directory or in the set path (see the environment variables section for an explanation of the path). The command interpreter is command.com for Windows 9x, and even though command.com exists in NT you generally use cmd.exe as it provides greater flexibility.

Command DirA very simple example would be the dir command, which means directory. If you type dir (and press enter) it will list the contents of the directory, meaning it will show you all the files and subdirectories that reside in the current directory. By default it will show you the time and date that the file/directory was last modified, whether it is a directory or a file (denoted by <DIR> for a directory and nothing for a regular file), the filesize of the file (not displayed for directories), and some additional information.

As previously mentioned all commands have the potential to receive command-line parameters. This is just a method of feeding options into a command so it can perform varying tasks. For example, the dir command accepts the /W parameter, so if you typed dir /w (and pressed enter) on the command prompt it would display the contents of the current directory in wide format, meaning that uses the space efficiently to display as many files/directories as possible per single page. For directories with lots of files you will generally want to use the /p switch, that is execute dir /p. This will list one full page of data and then pause so that it doesn't just fly by. This is especially important in a true DOS environment or in Windows 9x as there's no way to scroll up to see data that has passed as you can in Windows NT.

The general way to identify command-line parameters (switches) is with the /? switch, although some programs use -? or just help and some use -h or --help. The last two is the syntax of Unix based programs, but some programmers use that method in their programs. Also, some programs will display help on the command if you do not provide any parameters. So, if you use dir /? then you will receive information on the specific syntax for the dir command.

You can also specify additional command-line parameters, but it depends on the particular program how it behaves. Most programs have switches that specify to turn a feature on or a feature off, like a toggle switch. But, there's generally a default behavior. And some parameters are required because they specify what the command needs to do, like a compression programs that needs the target archive file specified as well as the files to archive. Some programs handle multiple switches like command -a -b -c but some prefer or require that you use command -abc instead. These behaviors are typically specified in the help of a command and from the generally supplied syntax of the command you can understand what it expects from you.

For external executables that aren't built into the command interpreter you will need to specify their directory if they are in the current directory or if they aren't in the path variable (see the environment variables section for information on the path). For example, if you wanted to run a command called delete.exe that is found in c:\windows then you would type c:\windows\delete.exe.

It should also be noted that certain file types do not require their extensions supplied for execution. This includes .exe, .com, .bat, .cmd and a few other less common types. So, you could type mem.exe to invoke the memory information command or you could just use mem as it will look for a command mem with the standard execution file types. There is no harm in typing out the entire command, but this is a feature that is meant to reduce the amount of typing needed.

Return to top.

 

Standard Commands

The command prompt environment features a great deal of commands. Below I have compiled a short list of some of the most useful commands. Typically you can use help at the command prompt to receive a similar list. In Windows 9x the external executable commands are normally stored in the \windows\command folder and typically for Windows NT they are stored in the Windows' system32 folder. The external commands are almost always .exe or .com files, but they could in theory be .bat or .cmd or some other type of script, a java program or such. You can also download additional command-line programs to add abilities to the command-prompt, but for the majority of users the built-in commands are enough.

Standard Commands
Value Name
Type
Description
attrib.exe
Executable
The attrib command allows you to change the primitive attributes of a file. These include hidding a file, setting it to read-only, setting it to system or setting the archive flag.
cd / chdir
Interpreter
Allow you to change to a directory. This is one of the most important commands in the command prompt.
chkdsk.exe
Executable
In Windows NT based operating system this command allows you to check a hard-disk for errors.
copy
Interpreter
Designed to let you copy files from one directory to another, but it can also rename a file without removing the original.
dir / ls
Interpreter
The dir command allows you to list the contents of a directory. By default it lists the contents of the current directory. It should be noted that the ls (meaning list) command is identical to the dir command, and it only exists because ls is the Unix command for dir.
echo
Interpreter
Lets you change the echo behavior of batch files or lets you print data to the command prompt. This is really only useful when you're writing scripts as the scripts can use this command to output data to the screen.
exit
Interpreter
This lets you close the command prompt. In a true DOS environment this will have no effect. But, if it is running under Windows then it will close the window. If you've shut down a Windows 9x OS into the command prompt then this command will restart Windows.
format.com
Executable
Allows you to initialize or reinitialize a disk. For the intents of most people this means that this command can erase the contents of a hard-drive, floppy, etc. It's a powerful command so be careful with it.
md
Interpreter
The md command stands for make directory and it lets you create a directory of your choosing.
mem.exe
Executable
Returns memory information about Windows. This command gives you much more useful information when not being executed from a Windows NT operating system.
move
Interpreter
Much like the copy command except that it removes the original. If the move command is used to move files within the same partition then the command will not have to delete anything and instead it will rename the file into the new name / directory.
scandisk.exe
Executable
This is the equivilent of the NT chkdsk command. This command can only be run while Windows is not active, but it will let you check the drive for errors. If it is run from Windows then the Windows version of scandisk will run.
subst.exe
Executable
Lets you alias a directory to another drive letter. This is similar to a Unix mount point or perhaps even to a symbolic link. However, if you want to use a command to create real symbolic links then use the junction command that can be found for download on the Internet.
time
Interpreter
Meant to let you check and change the time.
ver
Interpreter
Returns the version of the command prompt version. When running under Windows it will return the Windows version.
xcopy.exe / xcopy32.exe
Executable
I imagine this command stands for eXtended copy. It is much more powerful than the copy command. If the xcopy32 command exists then that should be used as it retains long file names and xcopy does not. This is true except when running Windows NT where xcopy is actually xcopy32.

Return to top.

 

Environment Variables

Set VariablesOne of the great features of a command-line interface is the ability to set variables. A variable is simply a text identifier that when used in a command resolves to something else. In DOS the variables are identified as being text surrounded by percent signs, such as %temp%, which is the variable that represents the path to the temporary directory. The configured variables can be viewed with the set command, and you can also change or create new variables using that command too. The syntax to view the commands is just to execute the set command without any parameters and to change them you use set variable_name=text so set temp=c:\temp would be an example. And then you could use cd %temp% to change to that directory.

A notable variable is the path variable. Normally when you execute a command on the command-line it checks if it is a command available in the command interpreter. If it is not then it looks in the specified directory, or in the current directory if no explcitit path was given for the command. If it is not found in that directory then it scans the path for the command. This is a variable that contains a list of directories that are separated by semicolons. So, it would look in each directory that is mentioned in the path variable. This is very useful because it saves you a lot of trouble specifying where your command executables are stored instead of typing the explicit path to execute them everytime or having to be in their directory. This path can be viewed or changed using the set command line any variable. So, you could append to the path, as example, by using set path=%path%;C:\somedir. Notice that I used %path% in the path so it included the old path and then added onto it. Likewise, I could have said set path=c:\somedir;%path%. You could also delete the path by using set path= or just reenter the list like set path=C:\windows;c:\windows\system32;c:\windows\system (etc).

Return to top

 

Unnamed Pipes

A pipe means it is a way to feed data into or out from a command. And by unnamed I meant that it is not a named pipe, which is more complicated pipe type that you can't make us of in Windows NT on the command prompt. This means you can choose where a command outputs to or let it feed data into another command-line program. In Unix based operating systems this is extremely powerful, but sadly in Windows the functionality is more limited. There are three types of pipes. The first is <, which is used to feed data into a program. The second is the |, which is used to feed the output of one program into another. And the third is > (or >>), which is used to feed the output data of a command into a file.

Command - Dir, MoreOne of the most widely used pipes is the pipe symbol |, which allows the data from one command-line program to feed another. There is a useful command called more that by itself does nothing very useful. However, if used in conjunction with another command it will work to pause the screen output after a single screen has been paused so the data doesn't pass without you being able to see it. This is useful for commands without built-in pause commands. As an example you could use this with dir like dir |more instead of using dir /p.

Another commonly used pipe is the >. It simply outputs text output from a command-line program into a text file. This is best described with an example. If you said dir > file.txt then no data would appear on screen, however it would have created a text file called file.txt in the executing directory that would contain the output from the command's output. It should be noted that the > command overwrites the entire file that you specify, but the >> command acts the same in everywhere except it appends the data to the end of the file instead of overwriting it.

Return to top.