If you don't quite have a
grasp on the concept of DOS or the Command Prompt, here's a quick primer
on this useful but oft-forgotten interface.The
Command Prompt in Windows XP is based on MS-DOS (Microsoft Disk
Operating System), the operating system used by the first PCs and the
basis for many versions of Windows, including 95 and 98, up until
Windows Me.
Fortunately, the DOS-like Command Prompt is still available from within Windows. You can open the Command Prompt by clicking its icon in the All Programs portion of your Start menu, or by typing cmd into the Start menu Search box and pressing Enter.
Windows Vista also includes the Command Prompt application found in Windows 9x/Me (command.com),
but given its poor support for long filenames and other limitations,
you shouldn't ever use it. Use either the native Windows Command Prompt (cmd.exe), or the Windows PowerShell,. |
|
When you open a Command Prompt window, you'll see a window that looks like the one shown in Figure 1.
The cursor indicates the command line (where commands are typed), and
the prompt usually shows the current working directory (here, C:\Users\Cory\AppData\Local), followed by a caret (>).
To
run a program or execute a command, just type the name of the program
or command at the command line (also called the "C" prompt because it
usually looks like C:\>), and press Enter.
Some Command Prompt applications simply display information and then exit immediately.
1. DOS Commands
The commands shown here are in constant width, and any parameters (the information you supply to the command) are in constant width italic.
Optional parameters are shown in [square brackets]. It doesn't matter
which case you use when you type them in the Command Prompt (DOS, like
Windows, is not case-sensitive). If there is more than one parameter,
each is separated by a space. |
|
attrib
attributes filename
Changes the attributes of a file or folder. The four attributes are R for read only, S for system, A for archive, and H for hidden.
In Explorer, you can right-click a file or group of files and select Properties to change the attributes; attrib is the DOS counterpart to this functionality. In addition, attrib lets you change the S (system) attribute, something Explorer doesn't let you do. Here are some examples:
attrib +h
myfile.txt
This turns on the H parameter for the file myfile.txt, making the file hidden.
attrib -r" another file.doc"-
This turns off the R (read-only) parameter for the file another file.doc (note the use of quotation marks because of the space in the filename).
Type attrib /? for additional options.
cd
foldername
Changes the working directory to foldername. If the prompt indicates you are in C:\Windows and you want to enter the C:\Windows\System32 folder, type cd system32. You can also switch to any folder on your hard disk by including the full path of the folder. Type cd .. to go to the parent folder. Type cd by itself to display the current directory.
To switch to another drive, just type the drive letter, followed by a colon (:). For example, type a: to switch to the floppy drive.
cls
Clear the display and empty the buffer (the history of output accessible with the scroll bar).
copy
filename destination
Copies a file to another directory or drive, specified by destination.
This is the same as dragging and dropping files in Explorer, except
that you use the keyboard instead of the mouse. For example, to copy the
file myfile.txt (located in the current working directory) to another drive, type copy myfile.txt g:\. Type copy /? for additional options.
del
filename
Deletes a file. For example, to delete the file myfile.txt, type del myfile.txt. This is not exactly the same as deleting a file in Windows, because the file will not
be stored in the Recycle Bin. The advantage of the DOS variant is that
you can more easily and quickly delete a group of files, such as all the
files with the .tmp extension: del *.tmp. Type del /? for additional options.
dir
name
Displays a listing of all the files and directories in the current working directory. Use cd to change to a different directory, or type dir c:\files to display the contents of C:\Files without having to first use the cd command. Type dir /p
to pause the display after each page, useful for very long listings (or
just enlarge the window). You can also specify wildcards to filter the
results; type dir *.tmp to display only files with the .tmp filename extension. Type dir /? for additional options.
echo
text
Displays the text, text, on the screen.
exit
Closes
the Command Prompt window. In most situations, you can just click the
close button × on the upper-right corner of the window, but the exit command works just as well.
md
foldername
This command creates a new directory (md=make directory) with the name foldername. The command will have no effect if there's already a directory or file with the same name.
move
filename destination
Is the same as copy, except that the file is moved instead of copied. Type move /? for additional options. (Unlike dragging and dropping in Windows Explorer, this command moves files unconditionally.)
rd
foldername
This command deletes an empty directory (rd=remove directory) with the name foldername. The command will have no effect if the directory is not empty. To delete a directory and all of its contents, use deltree.
ren
oldfilename newfilename
Renames a file to newfilename. This is especially useful, because you can use the ren command to rename more than one file at once—something Explorer doesn't let you do. For example, to rename hisfile.txt to herfile.txt, type ren hisfile.txt herfile.txt. To change the extensions of all the files in the current working directory from .txt to .doc, type ren *.txt *.doc. Type ren /? for additional options.
set [
variable
=[
string
]]
When used without any arguments, displays a list of active environment variables . The set command is also used to assign data to environment variables.
type
filename
Displays the contents of a text file. Type type
filename
| more to display the file and pause between each page of information rather than displaying the whole file at once.
Given
the nature of long filenames (they can have spaces), there are times
that typing them on the command line can cause a problem. The workaround
is to enclose the filename in quotation marks. Say you wish to rename a file named my stuff.txt to her stuff.doc. Instinctively, you might type: ren my stuff.txt her stuff.doc
However, this won't work, since the ren command sees only that you've typed four parameters: my, stuff.txt, her, and stuff.doc. Instead, you'll need to use quotation marks, like this: ren "my stuff.txt" "her stuff.doc"
Now, this isn't always the case. For example, say you want to use the cd command to change the current working directory to Program Files, like this: cd Program Files
Here, the Command Prompt is smart enough to interpret this correctly, and no quotation marks are needed. Tip for you lazy types: you can often leave off the final quote, and it'll still work. |
2. Batch Files
When
it comes to quick and dirty scripting, it's hard to beat DOS batch
files. Batch files, similar to WSH scripts , are plain-text files with the .bat or .cmd
filename extension. However, rather than relying on a complex,
unfamiliar scripting language, batch files simply consist of one or more
DOS commands, typed one after another.
One
of the problems with Windows-based scripting is that it tries to
control a graphical environment with a command-based language. Because
DOS is a command-based interface, DOS-based scripting (batch files) is a
natural extension of the environment.
Consider the following four DOS commands:
c:
cd \windows\temp
attrib -r *.tmp
del *.tmp
If you type these commands into a plain-text editor, such as Notepad, save them into a .bat
file, and then execute the batch file by double-clicking or typing its
name at the Command Prompt, it will have the same effect as if the
commands were manually typed consecutively at the prompt. Obviously,
this can be a tremendous time saver if you find yourself entering the
same commands repeatedly.
When
you run a batch file, each command in the file will be displayed
(echoed) on the screen before it's executed, which can be unsightly for
the more compulsive among us. To turn off the echoing of any given
command, precede it with the @ character. To turn off the printing of all commands in a batch file, place the command @echo off at the beginning of the batch file. |
|
To
run a batch file, double-click its icon in Explorer or, if it's in the
current working directory (folder), you can type its name at the Command
Prompt. You'll want to put more frequently used, general-purpose batch
files in a folder specified in the system path (see the upcoming
sidebar, "The Path Less Travelled"), so that they can be executed regardless of the current working directory.
Although batch files can run Windows programs (just type notepad
to launch Notepad), it's preferable to run Windows programs with WSH
scripts, because they'll be able to run without having to first load a
Command Prompt window.
In
addition to the standard DOS commands, most of which are documented
earlier, batch files use a couple of extra statements to fill the holes.
Variables, conditional statements, and for...next loops are all implemented with statements that are ordinarily not much use outside of batch files.
The next few section cover the concepts used to turn a task or a string of DOS commands into a capable batch file.