Despite the fact that most people haven't used it as their main operating system in about five years, the venerable DOS still has a lot of life left in it, especially for a tech working on the frontlines. It has been my experience that many of the techs who have joined the IT ranks since around 1995 are decidedly unaware of the power DOS has to make their work easier.
Furthermore, many of the "old school" techs who remember the pre-Windows 9x days have forgotten just how much easier DOS can make their lives. From simple file copies all the way up to automating PC reloads and modifying registry keys, DOS batch files are powerful tools that should not be overlooked in any tech's toolkit. Let's begin with one of the easiest and most useful applications for batch files.
In the spirit of the DOS-based theme of this article, whenever there exists both a DOS-based and a Windows-based process for doing something, I’ll use DOS. The only time I’ll choose Windows will be when it is noticeably faster. For the purposes of this article, we’ll assume that C:\Windows is the default location of the Windows install.
Copying and deleting files automatically
One of the most common uses for DOS batch files has always been the maintenance of groups of files. While it is easy enough to argue that Windows Explorer is a better suited environment for this task, it is far faster to update a couple of files on 200 machines through the use of a DOS boot disk than to start up all of those machines, log in to Windows, open Windows Explorer, navigate to the needed files, drag/drop them onto the proper location, and answer the prompt confirming that you’re sure you want to copy over those old files.
DOS is the only solution when the files you are updating or deleting are system files that are normally in use when Windows is up and running. With this in mind, let's go over the procedure for creating a boot disk that will do this:
- With a blank or unneeded high-density floppy disk inserted into drive A:, click Start, choose Run, type COMMAND, and press [Enter]. An MS-DOS window should open up with a C:\Windows> prompt and a blinking cursor.
- At the command prompt, type format a: /q /u /s and press [Enter] Note: This command will format the floppy disk in the drive. The /q means use quick format—this simply overwrites the File Allocation Table on the disk rather than overwriting the entire disk. The /u means to do an unconditional format—the only thing that will stop this sort of format is physically locking the disk; this option is more useful when formatting hard drives, but it is not a bad habit to get into using this with all your formats. Finally, the /s switch will copy system files to the disk so that it can be used as a boot disk.
- Type xcopy32 c:\windows\command\xcopy32.* a:\ and press [Enter]. This command will copy the files needed to use the xcopy32 command from your hard drive to the floppy disk. I prefer to use the xcopy32 command when writing DOS batch files because of its extended capabilities.
We are now going to copy the file that we will be updating. For the purposes of this article, we will use an imaginary file called updatefile.dll that resides in the C:\Program Files\Foo directory. This exercise will allow us to examine a common problem with using DOS in the Win 9x era—dealing with directory structures that contain spaces.
First, we need to make a directory on the floppy disk to hold the file we need to copy. To do so, type md a:\temp. The string md is the DOS command meaning “make directory”. Now type the command xcopy32 “c:\program files\foo\updatefile.dll” a:\temp and press [Enter].
Note how we deal with directory structures containing spaces when working within a DOS window running under Windows: Simply enclose the name of the file in quotation marks. This trick will not work when running from a boot disk, but we’ll deal with this situation shortly.
Next, type edit a:\autoexec.bat and press [Enter]. Doing so will open a DOS edit window. Your screen should change to a blue color; the window title bar should now read MS-DOS Prompt – EDIT. You should see a menu bar across the top of the DOS screen and a blinking white cursor.
Enter the following line into the Edit window: xcopy32 a:\temp\*.* c:\progra~1\foo\ /y.
Note how we handle directory names that contain spaces or more than eight characters. When we are working off of a boot disk, we use the first six characters of the directory’s name and append a ~1 to the end of the name. This rule is also true if the space falls earlier in the name than the first six characters. The first example that jumps to mind is the c:\my documents\ directory. In a DOS command, you’d write this as c:\mydocu~1\.
Another important point here is that the ~1 is used only if that file is the first one, alphabetically, that appears on the drive with those first six letters. Using directories named c:\this is an example\ and c:\this is a test\, we would use c:\thisis~1\ for c:\this is a test\ and c:\thisis~2 for c:\this is an example\, because when the operating system sorts those names, a space comes before letters.
Another interesting thing to note here is that if the updatefile.dll file did not already exist in c:\program files\foo\ directory, the file would be copied as update~1.dll. The /y switch will automatically answer “Yes” to the question of whether or not to overwrite the existing file.
Now, tap your [Alt] key (to activate the menu bar), then F (to choose the File menu), and finally S (to Save the updated AUTOEXEC.BAT file). To exit the Edit window, open the File menu and press X to exit. At this point, you should be back to the MS-DOS prompt window. Now type exit and press [Enter] to close the window.
Now your disk is ready to use on any machine to replace the updatefile.dll file—simply pop the disk into the floppy drive and boot it up—it will automatically boot in DOS and copy the updatefile.dll file from a:\temp to the c:\program files\foo directory. Once the file has been copied, you can turn the computer off and remove the disk.
The timesaving benefit derived from this procedure will be best realized when you have multiple copies of the disk so you can do more than one machine at a time. The procedure for making a copy is quite simple. Just insert the floppy into the drive, open the MS-DOS prompt window, enter the command diskcopy a: a: and press [Enter]. The disk you’re copying is referred to as the “Target disk” and the disk you are copying to is referred to as the “Destination disk.” When the disk has finished copying, you will be given the option of making another copy. Answer “Yes” to that prompt if you want to make more copies without having to read from the Target disk again.
To comment on this tip or to share your own favorite DOS batch file technique, please post a comment below or follow this link to write to Jason.