Windows Vista : Scripting and Automation - Command Prompt Scripting (part 1) - DOS Commands, Batch Files
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.
|
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 (>).
Figure 1. The Command Prompt is the old-school way to get things done

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
|
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.[21] Here are some examples:
[21] * The Change file attributes tool in Creative Element Power Tools (http://www.creativelement.com/powertools/) lets you quickly add or remove any of the four standard attributes by right-clicking.
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.
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.
|
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.