Web developer Michael Ross creates great websites for successfulbusinesses and non-profits, and he can build one for you...

Programming Your Windows Machine

This article was published by ComputorEdge, issue #2638, 2008-09-19, as the cover article, in both their PDF edition (on pages 5-9) and their website.

Most Windows users work with the data on their PCs primarily through the graphical user interfaces (GUIs) of applications — for instance, Web browsers and word processors. On those rare occasions when the typical users needs to interact directly with some files or folders, they usually make use of Windows Explorer, via its own GUI (or perhaps an alternative file management application).

Consequently, most Windows users nowadays have little or even no experience utilizing DOS commands typed in at a command prompt. As a result, these people are invariably not aware that they can perform most if not all operations using DOS commands, instead of Windows Explorer. In fact, many of these file and folder operations are best accomplished using DOS commands.

In turn, these commands can be stored — and thus easily reused — in simple computer programs, called "scripts". Such Windows programs are oftentimes referred to as "DOS shell scripts" or "DOS batch scripts", because they are composed of DOS commands, in sequence, with each one instructing the PC to perform a particular action. Veteran Windows programmers sometimes call them "DOS batch files". This phrase is noted here because if and when you decide to search online for some DOS scripting resources, you may find that some of the best information has been categorized using that older terminology.

Worth the Effort?

In this day and age of ubiquitous graphical interfaces, command-line scripts may seem quaint, at best. Yet even though these saved commands are a form of computer programming, it tends to be less intimidating than writing elaborate computer programs in third-generation languages such as C++ and Java. This is just one of the many advantages to beginners of DOS scripting.

In addition, Windows DOS scripts are generally favored by veteran PC users, who fondly recall using DOS even before there was a Windows GUI. These same people may also fondly recall the legendary stability of DOS, versus Windows 3.1, with its "General Protection Faults" and "three-finger salute" (i.e., having to press the key combination Ctrl-Alt-Del to restart Windows after it has frozen up… again).

Admittedly, there exist computer programs for doing almost everything nowadays. But they are usually overkill and consume a fair amount of memory or disk space. As an alternative in many instances, a simple DOS script would get the same work done, with greater opportunities for automation. Before GUI operating systems and applications were created and distributed through the marketplace, DOS scripts were the only way to accomplish repetitious Windows tasks without losing one's sanity or fingertips.

Due to space limitations, we cannot explain in this article all of the techniques for writing DOS scripts (in fact, there are entire books devoted to that subject), but we will consider how to get started, and how to discover the commands available. We will also examine a couple easy examples.

Begin with the Basics

Every DOS batch script is saved in a file that has a file name extension of ".bat" (short for "batch"). If you set the extension of any script file to ".bat", then the script is capable of being run in Windows Explorer by merely double-clicking on the file's icon. Also, it can be run at a DOS command line, by typing or pasting in the name of the file, and then pressing Enter. (The file extension is optional.) To demonstrate this, we will now go through the process of developing an extremely simple DOS script.

In case you are not familiar with how to obtain a DOS command line, first click on the Windows Start button, then select Programs, then Accessories, and finally Command Prompt. If all goes well, then you should be looking at a black window with "Command Prompt" in the blue title bar at the top of it. It should be showing you a prompt, such as "C:\>", which indicates the current partition (C: in this example) and the current folder (\, which is the top-level folder).

Anytime you are at such a prompt, you can type in DOS commands, and see the results. For instance, at your prompt, type in the DOS command "ver" and then press the Enter key. Windows will output on the screen its current version number.

Next create your first DOS script to do programmatically what you just did manually on the command line. You can use any editor to create the file; you can even use a word processor, such as Microsoft Word, provided that you save the file with a type of "text only". But the fastest approach is to utilize Windows's built-in character-based editor, from the command line. Type "edit test.bat" and press Enter. In the blue editor window that appears, type in "ver" and then save the file (File > Exit, and then Yes). Back at your command prompt, run your newly-created DOS script by typing in "test" or "test.bat", and pressing Enter. The script should output the same Windows version information that you saw before, when you executed the command manually.

Script Commands

Admittedly, the DOS script we created just now is of no real value; more commands are needed to perform any substantial task. But at least it demonstrates how easy it is to get started.

Scripts developed by programmers are usually several lines long, and some can be hundreds of lines long. They usually comprise the most common DOS commands, such as "cd", "copy", and "md". All of the DOS commands alone would be enough to make many useful scripts — especially for saving a list of commands performed frequently.

But DOS scripts can contain more powerful commands that are only used in scripts. For example, "echo" can output a message. "@echo off", as the first line of a script, can suppress the output of the script's commands. If you need to test if something is true, use an "if" statement. If you need to test whether a file or folder exists, use the "exist" statement. To perform some commands on a list of files or folders, use a "for" loop. To skip ahead or go backwards within a script, use "goto"; but keep them to an absolute minimum, because they can make your code as convoluted as a plate of spaghetti. To stop the execution of your script temporarily and wait for user input, use the "pause" statement.

Now we will try out some of these commands in a short script, to demonstrate their usage. Any line beginning with a colon and a space, is ignored by the DOS command interpreter. This is an excellent way to insert comments in your scripts, so in the future you or someone else can better understand the purpose of each commented chunk of code. If the space is absent, then the first word is considered to be a destination for a "goto" command. Alternatively, you can begin a comment line with "rem", which is short for "remark".

The following script saves some files to a backup partition, D:, if a folder "C:\files_to_backup" exists:

@echo off
: Sample script to save some files.
c:
if not exist \files_to_backup goto SkipBackup
echo Save any files in the folder "\files_to_backup" and then
pause
copy \files_to_backup\*.* d:\
:SkipBackup
echo All done

Further Resources

To learn more about writing DOS scripts and getting the most out of them, there are several sources of information. To display a list of all of the commands available to you at a DOS command line, enter "help", and it will show you over 70 commands — the majority of which can be used inside of your DOS scripts. To obtain more information on a particular command, such as "del", you can type in "help del" for instance. If that does not work, then enter the command but followed by the "/?" option.

There are a number of books in print devoted to DOS scripting, but the bulk of them focus on the two most frequently used alternative scripting languages for Windows, WSH and VBScript. However, one book that looks promising is Windows NT Shell Scripting by Timothy Hill. Its information would still be applicable even if you are running Windows XP or Vista.

The best resources of all will most likely be the innumerable websites devoted to DOS scripts. Using your favorite search engine, enter "DOS scripting" or "DOS batch files". That is how I discovered the "Batch" section of the Open Directory Project (considered by many to be the most extensive directory of websites, at this time).

Some people may argue that the huge number and variety of utility applications, boasting slick GUIs, has rendered DOS scripting skills obsolete. Yet in many instances, your homemade scripts will prove to be the most straightforward, powerful, and automatable solutions. If you perform particular Windows tasks repeatedly — especially if they involve files and folders — then saving those commands in a DOS script could save you, in the long run, a fair amount of time.

Connect

Airbnb Referral

Drupal Association

Client Feedback

I am appreciative of the work you have accomplished. I am absolutely impressed. I believe I am incredibly fortunate to have found you. Due to your keen eye and expansive knowledge base, I know that this project will be successful, and I am excited about the prospects. [...] Aesthetics — in general, it looks very appealing. Well done. Thanks again for your hard work.