Batch Files

I use DOS batch files all of the time, often in conjunction with MacroScheduler, a program that simulates keyboard and mouse input.

DOS is the old language code used to communicate with computers before there were icons and mouses.  The thing is, you can list out a bunch of commands in a text file called a 'batch file' and DOS will execute them line by line.  Plus, it'll stop on the line when you run a program until the program is closed, then it'll continue, enabling you to do something after the program has run, like delete some files it left behind.

First, grab this and stash it to the side until it's time to make a 'Command' folder.
 

Examples

Capitalization throughout this page is just for clarity.  DOS doesn't care.

I should note right up front that none of these are particularly important unto themselves; call it more of an accumulative thing.  I hate to belittle it, but in a way DOS has been reduced to kind of a "pest eliminator" these days.  You'll see what I mean in the examples.

Let's take a look at five different scenarios so you can get a feel for what a batch file is capable of.  Don't bother trying to understand the actual commands, just get a general feel for what's involved.


The Problem:  My Verizon Wireless is treated like a modem and needs to be fired up (gasp!) BY HAND(!) after the computer is up, unlike cable and ADSL which fire up along with the computer.  Worse, it actually takes three long, laborious steps on my part, psychologically crippling me by making me feel like I'm stuck in the middle of the 16th century.  By hand??  Surely, there must be a better way.

The Solution: My BAT file, with a shortcut icon in the 'Startup' folder so it'll run when Windows loads up, says nothing more than this:

c:\tools\msched\msched.exe verizon

That tells the MacroScheduler program to run the 'verizon' script, which does this: 

  • Fires up the Verizon program, waits 6 seconds for it to find the network
     
  • Fires up the modem, waits 8 seconds for it to initialize
     
  • Minimizes the program to the SysTray

What's to note here is that it's possible I could have done the first two steps with DOS, but the third step required the click of a mouse in the 'Minimize' box.  That's when a program like MacroScheduler springs to the fore.


The Problem: I normally run my monitor 1024 x 768.  Some of the games I like to play, like Pandora's Box and the Hoyle games, flip the screen down to an 800 x 600 resolution, which causes the 'SquareOne' pop-out menu bar I use to collapse down to two columns.  Normally, it's the height of the monitor, popping out from the left side when touched by the pointer, but it folds down into two short columns when the resolution is reduced.  So, I have to MANUALLY(!!) grab the edge of the column and drag it up near the top of the screen, which snaps it back into 1-column mode.  Having to face such an onerous task after every game session is obviously enough to keep me from wanting to play the game in the first place.  And we can't have that! 

The Solution: Rather than running the normal Pandora's Box entry in the Start Menu, I run a batch file that says this:

C:
games\pandora pandora.exe
CD \
tools\msched msched.exe squareone

That goes to the C drive, goes into the 'Pandora' folder and runs the program.  When the game is closed, the batch file continues.  It then runs MacroScheduler with the 'squareone' script, which then simulates what I do with the mouse to make the pop-out menu bar return to 1 column.

I then changed the icon of the Pandora BAT file to the actual Pandora icon so there's zero difference when running the game — except that it so very politely fixes my pop-out menu bar after the game is over.


The Problem: I edit the pages to my web site on my local computer, then upload them to the site with Dreamweaver when the time is right.  It's a great program but it has this nasty habit of leaving folders called "_notes" in every folder that it uploads from.  Running a very tight ship, there's no way I'm going to allow that kind of nonsense to go on.

The Solution: Rather than firing up Dreamweaver, I run a batch file that says:

C:
"net\dreamweaver\dreamweaver 8" dreamweaver.exe
CD \
CD webfolder
TDEL _notes /s /y

That first runs Dreamweaver, then, when Dreamweaver closes down, the batch file continues.  It then into the web site folder and runs a DOS Delete program called "TDEL" which runs through the entire folder and deletes every pesky "_notes" folder it can find.  Hah!

Chalk up another one for the good guys.


 As you might know if (1) you grab a lot of pictures off the web with Internet Explorer and (2) have your settings set to show you "hidden files and folders", those damn "thumbs.db" files end up all over the place!  Dang, those little rascals are pesky!

The Solution: When they start to build up, I run a batch file from my Start Menu called 'Thumbs' which says this:
 
DEL e:\thumbs.db /s
DEL f:\thumbs.db /s
DEL g:\thumbs.db /s
DEL h:\thumbs.db /s 

That runs the delete program on all four drives (plus all sub-folders) and deletes every 'thumbs.db' file in sight.  Another victory for the little guy!


As noted, none of these come under the heading of "earthshaking", but they demonstrate perfectly how DOS lets you control things, rather than sitting there frustrated because some program's not operating correctly, or leaving a bunch of files scattered around.  Combined with MacroScheduler, you can basically have the computer do anything you, yourself, could do.

If you've ever wondered whether or not you'd be "replaced by a computer" someday, this is that moment.
 

I Feel Like I'm In A Sword Fight — Or A Department Store Close-Out

Slashes everywhere!

You'll notice you're seeing both forward slashes and backslashes.  There's actually very little crossover.  Backslashes signify paths on a PC, always have.  If you're directing the command to some file on the system, you use backslashes.

Forward slashes in a command signify 'strings' or 'options' or 'parameters' or whatever the programmer calls them.  A '/S' usually means "look in sub-folders".  A '/Y' usually means "Yes, I mean this command", to answer the pesky "Are you sure you want to (whatever) this file?"
 

Tweak Time

It just wouldn't feel right if we didn't tweak something first.  To open a DOS box, you go to Start Menu, Programs, Accessories, 'Command Prompt'.  Go find it and with the right mouse button open its 'Properties'.  In the 'Start in' box put:

C:\

That'll start it up right on the root of the C drive.

We also need a spot to put our new DOS commands that will be in the 'paths' so Windows can find them.

The standard place for DOS commands is a "Command" folder inside of the 'Windows' folder.  Go ahead and make one.

Now click on the 'My Computer' icon with the right mouse button and open 'Properties'.  Click on the 'Advanced' tab, then 'Environment Variables'.  In the bottom window, scroll down to the 'Path' line.  Click 'Edit'.  Go out to the very end of the line and paste this in:

;C:\Windows\Command

Now Windows will recognize any DOS commands in the folder.
 

In The Ballpark

Note 1: For the rest of the page, whenever it's speaking of a DOS window, it'll be expected that you follow every command with the Enter key.

Note 2: Virtually every DOS command will spit out its proper format if you follow it with a "/?".  For example:

DIR /?

will show you the template for the DIR command.

Note 3: Templates are really confusing.  I've been attempting to decipher them for decades and still get stuck when taking a new DOS command out for a spin.

Note 4: Properly, in DOS, all folders should be referred to as "directories", but for the sake of Windows compliance, the term "folder" will be used here.
 

As stated above, normally you won't use a DOS window very much, just batch files, but you usually want to first try out the commands in real-time using DOS, just to make sure the thing actually works, so you have to know the basics of DOS navigation just to get around the system in the first place.
 

File System Overview

Your computer system probably has one hard drive in it.  The drive is usually divided into "partitions".  Your main C Drive is the 'C' partition, and you might have a 'D' drive, or even more.  What's displayed in 'My Computer' is what's on your system.  After the hard drive partitions come any ROM drives, then virtual devices and portable devices.

In DOS, the 'you' typing into the DOS window is located somewhere on the system.  If it says "C:\" next to the prompt, then you're on the root of the 'C' drive.  If it says "D:\songs>", you'd be on the 'D' drive in the 'songs' folder.  Using four basic commands, you navigate your way around the system until you're in the folder you want.
 

And Away We Go

Pop open a DOS box.  If you did the tweak above, the prompt should say "C:\", meaning you're on the root of the 'C' drive.  If you want to get a visual reference, open 'My Computer', then the 'C' drive, and that's where you are on the drive.  Down below, when you 'DIR' the drive (to list out the contents), you'll see listings of the same folders and files you're seeing in the 'C' drive's window.

The main navigation commands:

<drive letter>: — puts you on root of that drive

CD <folder name> — puts you in that folder

CD .. — up to parent folder

CD \ — back to root of drive

You can also skip folders with the CD command, like so:

CD multimedia\songs

That would bring you into your 'Multimedia' folder, then into the 'Song's folder inside of it.

If a folder name has a blank space in it, like "Program Files", you need to use quote marks around the whole line, like so:

CD "program files\coolprog"

That would bring you into the 'Program Files' folder, then the 'CoolProg' folder inside of that.
 

Manipulating Files

We use the DIR ("Directory") command to see what's in the current folder.  Assuming you're on the root of the 'C' drive, type in:

DIR

and it'll display the contents of the root of the C Drive.  Although displayed quite differently, it should basically mirror what's in the 'C' drive's window.

If it's scrolling past the page, enter a /P at the end, for 'Page':

DIR /p

Hit the space bar to read the next page.

To rename a file, type:

RENAME <old name> <new name>

To move a file, type:

MOVE <file> <new folder>

To make a copy of a file:

COPY <file> <new file name>

If it's a binary file, make sure Copy knows it by making the command "Copy /b (etc)".

To copy a file to a folder:

COPY <file> <folder name>

To make a new folder:

MKDIR <new folder name>

(MD also works)

! ! ! WARNING ! ! !

In a word: DOS is forever.  You delete something with DOS, it's history.  No friendly Recycle Bin with this thing.  So be careful.

To delete a file:

DELETE <file>

(DEL also works)

There are, of course, zillions of DOS commands out there, but the above is the core group.

Tip: Hit Alt-Enter to make a DOS box full-screen size, and back again.
 

Batch Files

BAT files are always edited with Notepad.  They have to be 'pure' text.  Another perfect candidate for the SendTo feature.

The commands you'll usually use in a BAT file will be the above bunch, with the occasional special-purpose command thrown in.  Or, just as likely, a better replacement for an existing DOS command.

On the subject, if you're going to replace an existing DOS command, you'll have to give the replacment a different name, i.e. 'ATTRIB2' or 'DELETE2'.  When the system sweeps the paths, looking for the command you just typed in, it'll find the original command first and ignore your new one in the 'Command' folder.  Thus, it needs to be renamed and the new name used in DOS and batch files.

Here's a small package of commands I've used in the past.  They are:

HOLDIT — A 'pause' command, pausing for X number of seconds or until the 'Any' key is pressed

ATTRIB2 — Better 'Attrib' command

REPSTR — 'Replace String', replaces bits of text inside of files

SETRES — Resets the screen resolution

TDEL — Improved Delete command

XCOPY — Improved Copy command
 

Attributes

Every file on the computer has four 'attributes'.  To access them, you open the file's Properties and check or uncheck the appropriate box.  They are: 

  • Read Only — This keeps the file from being deleted or changed; i.e., it can only be read.
     
  • System — Means it's a Windows system file and (usually) can't be deleted or moved.  Or, if it's moved or renamed, Windows will keep track of it and still consider it the default folder for that particular function.  As a quick example, Windows will let you rename the 'Fonts' folder, but it knows what you did and you'll still see all the fonts when a program loads.
     
  • Hidden — This keeps certain system files out of view from the newbies and, hopefully, safe from accidental deletion.
     
  • Archive — This is an anachronism.  It's how old-style backup systems worked.  If the file had been run through a back-up program ("archived"), it had the 'Archive' attribute checked.  Modern programs use lists, dates and databases to keep track of things.

If you're dealing with hidden or system files, you'll usually have to use the Attrib command first to change them back to normal.  Then you can delete, move, rename, etc, the file.
 

BAT Shortcut Icons

Most likely you won't want the DOS box from popping open when running a task.  To run the box in the background, as well as change the icon, you need to make and use a shortcut icon.  If you're going to run it from the Start Menu, d-r-a-g the BAT file to the appropriate place on the Start Menu and drop it in.  This entry is a shortcut icon, so open its Properties and set the 'Run' box to 'Minimized'.

If you want to change the icon in the Start Menu, click on 'Change Icon', then 'OK' if the small box pops up, then the 'Browse' button.  Browse to the program whose icon you want to steal.  If it's not displaying, select 'Programs' from the drop-down menu at the bottom.

If you're going to run the BAT file with an icon, then it'll need to be run with a shortcut icon if you want it to be customized in any way.
 

Building A BAT File

I use the term 'building' because a BAT file is usually constructed in separate stages unless it's fairly small.  As noted before, the usual procedure is to first perform it step-by-step via DOS, then scribble the steps into the BAT file as they're proven to work.

And, rather than testing it on your real files, you'll probably want to use fake folders and files until it's working, especially if it's actually altering or deleting the files, then put in the paths to your real folders and files.
 

BAT Troubleshooting

DOS isn't "finicky", as some might describe it.  It's exacting.  One tiny mixtake in the command line and the BAT file is toast.  Nor will you get much feedback from the window as to what screwed up.

The best troubleshooting trick is to put a "HOLDIT 10" between each BAT entry.  That'll at least allow the DOS box to stop long enough to see what's already worked, then it's a matter of figuring out why the next line didn't work.
 

Online DOS

It might come as a surprise that you can use BAT files to upload and download files from a web site with ease.  The actual DOS command is 'FTP', which, in turn, runs a secondary file with the site name & password and what files to 'put' or 'get'.

Here's the DOS file:

FTP -s:script.txt

And here's 'script.txt':

open www.yourdomain.com <account name> <account password>
put <filename> http://www.yourdomain.com/newfiles/
get http://www.yourdomain.com/files/<filename>
quit

That would upload the first file to the 'newfiles' folder on the site and download the second file to the folder the batch file was run from.

As a small side note, the script file, above, is using FTP language, like 'put', 'get' and 'quit', to quit the process and close the window.  'Quit' doesn't work in DOS, however.  The DOS command to quit the process and close the window is 'EXIT'.
 

Lowering A Program's Priority

It's not uncommon to be running a program in the background that uses a lot of CPU, like some sort of video conversion program, and have the rest of the system feel kind of sluggish.

The manual (cough!) way around this is to:

  • Open Task Manager, 'Processes' panel
      
  • Click on the 'CPU' header, twice
     
  • Find the CPU hog at the top of the list, highlight it with the right mouse button and 'Set Priority', 'Low'

I'm exhausted just typing it all out!

With DOS, you'd run the program via a BAT file and use the DOS 'START' command, like so:

C:
CD folder\program_folder
START /LOW program.exe

You first CD to the program's folder, then run the program.  The above starts the program with a 'Low' priority.  I use this method to run at least three video conversion programs, all of which I just want to run in the background and not hog any system priority.
 

Advanced DOS

In truth, we've just scratched the surface of DOS here.  Advanced DOS can do such things as (1) generate a list of files, (2) take the top entry of the list, (3) interrogate the file to see if it's one type of format or another or perhaps one size or another, (4) do something or not to the file, depending, (5) save the new file or not, depending, (6) scratch the name from the top of the list and (7) repeat the above.
 

Finding DOS Programs

There are still sites out there that have a nice library of DOS commands, and just as often you'll find your one particular command will be inside a larger package.