The Explorer: Save Your Butt With DOS: Pulling It All Together - InformationWeek
Software // Enterprise Applications
04:41 PM
Fred Langa
Fred Langa

The Explorer: Save Your Butt With DOS: Pulling It All Together

Here's how you can build your own batch files -- or alter those of others

OK, So How Do You Make A Batch File?
It's easy. Again, a batch file is just a plain text file that lists a series -- a "batch" -- of DOS commands, one after another. When the operating system processes a batch file, it simply executes the command in the order they're listed, starting at the top of the file and working down. (If you need a DOS command primer, see the EasyDOS Internet Guide to DOS.)

Although batch files are indeed just plain-vanilla text files, they're given a special extension (bat) instead of the normal text file extension (txt) so the operating system will know that the bat file contains commands that should be processed, rather than text to be displayed.

The easiest way to get comfortable with batch files is to learn by doing, so let's build a very simple demo file. It won't do anything fancy, but it's a safe and easy way to get started with batch programming. First, we'll perform a simple DOS task by hand; then we'll use a batch file to automate the task.

Here's the manual task: We'll navigate around the hard drive in DOS, and examine the contents of two directories/folders.

1) Open a "DOS window" by clicking on Start/Programs/MS-DOS Prompt, or by typing "Command" (without the quotes) in the Start/Run line. A plain (probably black and white) window will open on-screen. This isn't true or "pure" DOS because Windows is still running and active, but it's all we need for now, and the examples we'll discuss will work exactly the same if you do boot to pure DOS such as by hitting F8 when your hear the system beep when you first turn on your Win9x machine The blinking underline mark you see in the DOS window is the DOS cursor: It appears at what's called the Command Prompt. Depending on how your system is set up, your Command Prompt probably says something like "C:\" or "C:\WINDOWS" or "C:\WINDOWS\Desktop." These are, of course, the names of directories or folders on your hard drive; the Command Prompt is showing you where you are in your hard drive structure. (In the off chance you have a very basic DOS setup, your prompt may only show "C>" and nothing more. If that's the case, type PROMPT $P$G and hit the Enter key to switch to the more descriptive Prompt.)

2) Now let's change to the topmost directory ("folder," in Windows-speak) on your C: drive. That directory is C:\, and the "change directory" command is "CD" so you simply type CD C:\ and press enter. If you weren't already there, your Prompt should now show "C:\>"

3) The command for showing all the files in a directory is DIR, and you can show the resulting file list in wide format by add "/W" to the command: Type DIR /W and hit enter, and you'll see a display of all the files in the C:\ directory.

4) Now let's change directory to C:\Windows: Type CD C:\Windows and hit enter. Your prompt now says "C:\WINDOWS."

5) Now display the files in C:\Windows by typing DIR /W and hitting enter.

Batching It
OK, now let's do the all the above via a batch file:

Start NotePad. (Remember, batch files are plain-vanilla text files: You don't want fancy formatting commands embedded in the file, such as what you might get using a full-blown word processor.)

In the blank NotePad page, type the following lines:

CD C:\
CD C:\Windows

(Those are, of course, exactly the same commands you typed manually a moment ago.)

Now save your batch file: Click on File/Save As. Use the "Save In" function to navigate to your C:\WINDOWS\Desktop folder. In the "Save as type" input line, select "All Files (*.*)" then in the "File Name" area, name your batch file -- something obvious like TEST1.BAT. Click on save.

Now look at your Windows desktop: there should be a new icon there named TEST1.BAT (or whatever you named it) and the icon will sport the image of a gear, showing it's a file the operating system can work on.

Double click the icon and your new batch file will run in a DOS window, carrying out the commands you typed in in rapid succession.

2 of 6
Comment  | 
Print  | 
More Insights
Newest First  |  Oldest First  |  Threaded View
How Enterprises Are Attacking the IT Security Enterprise
How Enterprises Are Attacking the IT Security Enterprise
To learn more about what organizations are doing to tackle attacks and threats we surveyed a group of 300 IT and infosec professionals to find out what their biggest IT security challenges are and what they're doing to defend against today's threats. Download the report to see what they're saying.
Register for InformationWeek Newsletters
White Papers
Current Issue
2017 State of the Cloud Report
As the use of public cloud becomes a given, IT leaders must navigate the transition and advocate for management tools or architectures that allow them to realize the benefits they seek. Download this report to explore the issues and how to best leverage the cloud moving forward.
Twitter Feed
InformationWeek Radio
Archived InformationWeek Radio
Join us for a roundup of the top stories on for the week of November 6, 2016. We'll be talking with the editors and correspondents who brought you the top stories of the week to get the "story behind the story."
Sponsored Live Streaming Video
Everything You've Been Told About Mobility Is Wrong
Attend this video symposium with Sean Wisdom, Global Director of Mobility Solutions, and learn about how you can harness powerful new products to mobilize your business potential.
Flash Poll