Windows Vista : Scripting and Automation - Command Prompt Scripting (part 1) - DOS Commands, Batch Files

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
9/26/2012 1:56:04 AM
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 (, 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 (>).

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

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.[21] Here are some examples:

[21] * The Change file attributes tool in Creative Element Power Tools ( 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.


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. 


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.

Using Long Filenames in the Command Prompt

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:

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 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.

  •  Windows Vista : Scripting and Automation - Wacky Script Ideas
  •  Windows 7 : Programming Drivers for the User Mode Driver Framework - Required Driver Functionality, UMDF Sample Drivers
  •  Windows 7 : Programming Drivers for the User Mode Driver Framework - Windows I/O Overview, Brief COM Information, UMDF Architecture
  •  Installing Windows Small Business Server 2011 (part 2) - Understanding the Installation Process
  •  Installing Windows Small Business Server 2011 (part 1) - Performing a Clean Windows SBS 2011 Installation
  •  Preparing Your Windows 8 PC : Connecting to Wireless Networks
  •  Preparing Your Windows 8 PC : Setting App Notifications - Changing Notifications
  •  Gainward Geforce GTX 690 4GB For The Most Dedicated Gamers
  •  Portable Drive Algato Thunderbolt SSD
  •  Laptop HD Pavillion DM1 - 3200SA - The Thinnest Ultraportables
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us