Windows Scripting Using DOS

This article was published by ComputorEdge, issue #2221, , as a feature article, in both their print edition (on pages 26 and 28) and their website.

Most Windows PC users spend the bulk of their computer time interacting with the graphical user interfaces (GUIs) of various application programs, such as word processors and Web browsers. Even on those rare occasions when these folks must work directly with files and folders, they typically use Windows Explorer via its own GUI.

As a result, most Windows users nowadays have little or no experience typing in DOS commands at a prompt. (For those new to Windows, it should be noted that DOS — which stands for "Disk Operating System" — is the older operating system upon which Windows was originally built, as a GUI.) More to the point, these users typically are not aware that they can write simple computer programs to work with those files and folders, and do so without having to learn any high-level computer programming language.

These simple Windows programs are often called "DOS shell scripts" or "DOS batch scripts", because they are largely composed of scripted DOS commands. Seasoned programmers often refer to them as "DOS batch files". This terminology is mentioned here because, when searching for Windows scripting resources on the Internet, some of the most valuable information is often categorized using the older name.

DOS scripts tend to be favored by veteran PC users, who remember using DOS before there even was a Windows. These are also the same users who fondly remember the stability of DOS, prior to Windows 3.1, "General Protection Faults", and the "three-finger salute", namely, pressing Ctrl-Alt-Del to restart Windows after it had frozen.

These days, there are applications for doing almost everything; but they are often overkill, or consume too much disk space. In many cases, a simple script would get the job done, and with greater control. Prior to all of those GUI applications being developed and made available to PC users, DOS scripts were the only way to get very repetitious tasks accomplished, at least while retaining one's sanity and fingertips.

Even though this article cannot explain all of the techniques for writing DOS scripts (there are whole books devoted to that), I will discuss how to get started, show how to find out the commands available, and consider some examples.

Starting with the Basics

Each DOS batch script is stored in a file that has in its name the file extension ".bat" (short for "batch"). When given this extension, the script can be run in Windows Explorer by simply double-clicking on the file's icon. It can also be run at the DOS command line by entering the name of the file; the file extension is optional. To illustrate all of this, let's step through the process of creating a very simple example script.

If you are not familiar with how to get to the DOS command line, here are the steps: Click on the Start button, then select Programs, then Accessories, and finally Command Prompt. This should display a black window entitled "Command Prompt" that displays a prompt such as "C:\>". At this prompt you can enter DOS commands, and see their results. For example, type in "ver" and then press the Enter key. Windows should display its current version information.

Now let's create our first DOS script. You can use any editor to create the file. You can even use a word processor, such as Microsoft Word, as long as you save the file with a type of "text only". For now, let's use Windows' built-in editor. Type "edit test.bat" and press Enter. In the blue editor window that pops up, type in "ver"; then save the file. Go back to your command prompt, and run the DOS script by typing in "test" or "test.bat", and pressing Enter. The script should produce the same Windows version information seen before.

Another Example

Obviously, the script I just created is of no practical value, and more commands are needed to perform any task of substance. One can use the most commonly used DOS commands, such as "copy", "md", and "cd".

In addition, scripts are made much more powerful by using commands that are usually only seen in scripts: "echo", to output a message; "@echo off", as the first line of the script, to suppress the echoing of the script's commands; "if", to perform another command if something is true; "exist", to test if a file or folder exists; "for", to perform some commands on a list of files or folders; "goto", to skip ahead or backwards in a script; and "pause", to stop the script temporarily and wait for user input.

Let's try to use most of these commands in a short script, to illustrate their usage. Note that any line beginning with a colon and a space, is ignored by the DOS command interpreter, and thus can be used to insert comments into your script. If the space is missing, the first word is treated as a destination for the "goto" command. This sample script backs up some files onto a diskette if a particular folder "c:\files_to_backup" has been detected:

@echo off
: Sample script to backup some files.
c:
if not exist \files_to_backup goto SkipBackup
echo Insert a blank diskette and
pause
copy \files_to_backup\*.* a:\
:SkipBackup
echo All done

Resources

If you would like to learn more about how to write DOS scripts, there are many sources of information. For instance, to see a list of all of the commands available to you at the DOS command line, just enter "help", and you will get a list of over 70 commands, most of which can be used inside of a DOS script. To get help on a particular command, such as "del", you would enter "help del" (at least in Windows 2000, and probably other versions as well).

You could try to locate and read a book on DOS scripting, but you will probably find that most of them focus on the two most common alternative scripting languages for Windows, WSH (Windows Script Host) and VBScript (Visual Basic Script). However, one title that looks promising is Windows NT Shell Scripting by Timothy Hill.

Perhaps the best resource of all is the countless websites devoted to DOS scripts. Simply enter "DOS scripting" or "DOS batch files" into your favorite search engine. That's how I found the "Batch" section at the Open Directory Project (reputed to be the largest directory of websites in existence), at http://dmoz.org/Computers/Software/Operating_Systems/x86/DOS/Programming/Languages/Batch/.

It might be argued that the plethora of utility programs sporting slick GUIs, has made DOS scripting obsolete. But in many cases, the humble, homebrewed scripts will turn out to be the most flexible and straightforward solution. If you find yourself performing certain Windows tasks repeatedly — especially those involving files and folders — then capturing those procedures in some DOS scripts could save you a lot of time in the long run.

Copyright © 2004 Michael J. Ross. All rights reserved.
bad bots block