Helping people with computers... one answer at a time.

Batch files are often used to group commands, or make shortcuts to more complex commands. We'll touch on the basics of just what batch files are.

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.

Article C3688 - March 29, 2009 « »

Share this article with your friends:

Share this article on Facebook Tweet this article Email a link to this article
Leo Leo A. Notenboom has been playing with computers since he was required to take a programming class in 1976. An 18 year career as a programmer at Microsoft soon followed. After "retiring" in 2001, Leo started Ask Leo! in 2003 as a place for answers to common computer and technical questions. More about Leo.

Not what you needed?

12 Comments
Bill
March 30, 2009 8:30 AM

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

Rahul Mehta
March 30, 2009 9:34 AM

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
Zeta
March 31, 2009 8:22 AM

So the windows downloadable programs, that create batch files will do the same thing?

Mike OBrien
March 31, 2009 9:16 AM

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

Chucko
March 31, 2009 9:18 AM

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.

Meyer Wagner
March 31, 2009 10:17 AM

I aman old man who was raised on DOs. I use batch files and saved your article fo referene Thanks

ed reid
March 31, 2009 10:48 AM

XP ist gut, DOS ist batter!

Rick Saffery
March 31, 2009 11:22 AM

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.

Christine
March 31, 2009 2:57 PM

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.

asdf
March 31, 2009 6:06 PM

".cmd" file is actually a command script and is slightly different than a ".bat" file

Robert Hohlt, Jr.
October 27, 2009 6:09 AM

Since windows will start a command prompt on it's own, I can run a .bat or .cmd file in windows task scheduler with no problems whatsoever huh?

pradeep
February 22, 2012 8:43 PM

how can i delete ntldr file using batch file after one month?plz tell me

Comments on this entry are closed.

If you have a question, start by using the search box up at the top of the page - there's a very good chance that your question has already been answered on Ask Leo!.

If you don't find your answer, head out to http://askleo.com/ask to ask your question.