Technology in terms you understand. Sign up for the Confident Computing newsletter for weekly solutions to make your life easier. Click here and get The Ask Leo! Guide to Staying Safe on the Internet — FREE Edition as my thank you for subscribing!

What's a Batch File?

I’ve seen you mention a “batch file” a couple of times in articles.
What is that? What’s it good for?

A batch file, or command file, is a very simple text file that
contains a list of programs and commands that Windows can then execute
when you “run” the batch file.

The “problem” is that batch files date back to the days of MS-DOS,
and are designed to contain commands that you would run within a
Windows Command prompt.

I’ll show you what I mean with a simple example.

]]>

Fire up Notepad (Windows Key + R, then “notepad“, and press “OK“). Type in a series of commands that you want to run. Here I’ll use it to run a single command which will show a simple file listing, ordered by the time files were modified.

Notepad with a single command

“Dir” is the command prompt command to display a “directory”, the list of files in a folder, and “/O:-D” is the option to that command to Order the results by the date/time the files were last modified, with the most recent at the top of the list.

Now, save that file as a “.cmd” file. Let’s call it “tdir.cmd”:

Notepad Saving a Batch File

Pay attention to the full path to the folder that the file was saved in as well (or select a folder of your choice). I saved my file into a folder “t”, at the root of my C: drive, or “C:\t”.

Now, file up an instance of Windows Command Prompt (Windows Key + R, then “cmd“, and press “OK“).

Enter “CD ” followed by the path to the folder into which you saved the file.

Now enter “tdir” without the “.cmd”, followed by Enter:

Running a Simple Batch File

You can see that after typing in tdir it automatically ran the command listed in the file and listed the files in that folder in reverse chronological order.

“Batch files get their name because you can “batch up” multiple commands into a single file …”

So far this has been pretty simple, but now that you’ve seen how to create and use a batch file, we’re going to cover three concepts that give us just a peek at the power: multiple commands, parameters, and arbitrary programs.

Batch files get their name because you can “batch up” multiple commands into a single file where they are executed in the sequence listed. There’s no limit to how many commands you can include in a batch file.

Let’s take our existing batch file:

dir /O:-D

And make it less sensitive to where it might be:

cd /D C:\Windows
dir /O:-D

Now if you save and run that batch file, you’ll see that the current directory has been changed to C:\Windows, and that the directory listing is of the files in that folder:

Volume in drive C is NOTENQUAD
Volume Serial Number is E852-AA62 Directory of C:\WINDOWS 03/24/2009 11:07 AM 1,149,165 WindowsUpdate.log
03/24/2009 02:13 AM <DIR> Prefetch
03/23/2009 08:28 PM <DIR> system32
03/23/2009 11:32 AM 2,629 wiadebug.log
… 159 other files omitted …

As I said, simply creating and running a batch file with a list of commands you want to have executed will execute them in the order that they’re listed.

So far our little batch file isn’t really all that useful, because it shows the listing for only one specific folder. We wouldn’t want to create a batch file for each possible folder, so instead we use parameters. We modify tdir.cmd to remove the “cd” line, because it’s no longer necessary, and change the “dir” line to this:

dir /O:-D %*

That “%*” is a parameter substitution that is replaced when the batch file is run by whatever follows invocation of the batch file. So, for example, we can now run:

tdir c:\Windows

And the batch file will run:

dir /O:-D c:\Windows

This makes the batch file significantly more useful, as you can now specify the folder you want to have listed, or any additional parameters to “dir”. Or you can specify nothing at all, and it will operate on the current directory.

So the question comes up: what can you put in a batch file?

Well, commands like CD and DIR are actually commands internal to the Windows Command Prompt, but in reality any “.exe” file (among others) will do, as long as it can be found. So, for example, this batch file:

“C:\Program Files\Microsoft Office\Office12\WinWord.exe”
“C:\Program Files\Microsoft Office\Office12\Excel.exe”

Would start Microsoft Word, and then after Word completes and exits, Microsoft Excel (assuming that they’re installed to the path shown in this example).

You may not have to specify the full path to many commands. When you run programs in the Windows Command Shell it will search for them in something called the “PATH”. This normally includes C:\Windows as well as C:\Windows\System32 so any “.exe” you find there can be run in a batch file without any qualification:

notepad “C:\Documents and Settings\LeoN\Desktop\notes.txt”

This, in a batch file, would run notepad (from C:\windows) and open or create the file “notes.txt” on my desktop.

Similarly, placing your batch file into one of the directories on the path means it, too, will be available regardless of what directory may be current at the time you run it. Batch files can, in many ways, be used very much like the other programs on your machine.

I’ve only scratched the surface of what can be done in batch files. There are programming constructs like loops and conditionals, there are additional ways to manipulate parameters, and ways to interact with your computer’s environment. There are even ways to alter that Word/Excel example above so that rather starting one after the other, they’re started nearly simultaneously.

There’s too much more than I can get into here, but I wanted to give you a brief overview of what they are and how they can be used in some simple circumstances.

If you’re interested in pursuing batch files in more detail, a quick Google search on “batch files” returns many helpful resources.

Subscribe to Confident Computing! Tech problem solving & safety tips & a weekly confidence boost in your inbox every week.

I'll see you there!

10 Reasons Your Computer is Slow

Slow Computer?

Speed up with my special report: 10 Reasons Your Computer is Slow, now updated for Windows 10.

NOW: name your own price! You decide how much to pay -- and yes, that means you can get this report completely free if you so choose. Get your copy now!

12 comments on “What&apos;s a Batch File?”

  1. I am not sure, but it appears that batch files must be built and run (only) from the command prompt.

    Actually not true. You can build them in notepad, as per my example, and if you want, you can assign them a shortcut so that they can be run from your start menu or wherever you want to place a shortcut. A batchfile will start a command prompt, if it’s not run from within one, since it is a command prompt scripting language.

    – Leo
    31-Mar-2009
    Reply
  2. Try giving the batch command file the extension “.bat” – e.g. filename.bat.

    Windows recognizes this as batch file and run it in a command prompt window and at the end, closes this window.

    “.cmd” works too.

    – Leo
    31-Mar-2009
    Reply
  3. I have a batch file that works fine for backing up on demand certain files to my 2nd drive, but it fails when I tried backing up an important file in the ProgramData folder. Says “Cant find folder xxxxx”.
    I realize that folder is hidden but I changed my View to “Show Hidden Files”.
    The bat file does a DIR successfully on the folder, but the COPY command on the 2nd line gives that “Cant find folder xxxx” message.
    (I have Vista.)
    What gives?

    Hard to say. The “View Hidden Files” option in Windows Explorer has nothing to do with batch files or the command prompt – is should just work. My guess is that there’s an important detail you’re missing. Which detail? I can’t say.

    – Leo
    01-Apr-2009
    Reply
  4. Batch files! A blast from the past but still useful. I have written way too many in my 20 years in front of a PC. Unfortunately for me, in XP, the batch file runs then immediately closes with nothing to see. Properties does not allow me to un-select close on exit. Solution: Pause as the last line then all is fine. But I know this is not the “real” solution, but… Thanks Leo for another fine “Ask Leo” which I always read and enjoy.

    Reply
  5. Well done as always! Leo, now that you’ve stirred a renewed interest in writing and using batch files. I gently urge you to describe the latest twist on them found in PowerShell. You would do a terrific job introducing people to this seemingly simple, yet powerful application.

    Reply
  6. We often add batch files to jobs in SQL Server to copy files to useful locations so that it doesn’t have to be done manually. For example, a data file located on the server of another system is copied to the SQL server. Or an output data file is copied to a backup folder on another network server.

    Reply

Leave a reply:

Before commenting please:

  • Read the article.
  • Comment on the article.
  • No personal information.
  • No spam.

Comments violating those rules will be removed. Comments that don't add value will be removed, including off-topic or content-free comments, or comments that look even a little bit like spam. All comments containing links and certain keywords will be moderated before publication.

I want comments to be valuable for everyone, including those who come later and take the time to read.