Creating a Simple Batch File

by Barry Dysert
(last updated January 23, 2017)

3

Windows batch files are great if you want to perform several tasks in quick succession, or if you want to do things to several files in a row, or if you simply don't want to use the GUI interface to perform some tasks (e.g., performing unattended tasks). Batch files are text files with a ".bat" extension and contain Windows' command-line statements. Of course, you need to know the command-line statements in order to enter them into a .bat file. There are many places on the Web that describe the available command-line statements, but one place is the following:

http://technet.microsoft.com/en-us/library/cc754340.aspx#BKMK_Index

There are various ways to create a batch file, but since a batch file is nothing but a plain text file, perhaps the simplest way is to use Notepad. Since I use batch files a lot, I created a C:\Bat folder where I keep them all. To create a new one I use Windows Explorer or File Explorer to navigate to C:\Bat, then I right-click in that folder and select New | Text Document. (See Figure 1.)

Figure 1. Creating a new text document.

Immediately after selecting the Text Document menu item, type the name you want to give to your batch file (say you want it to be called "Directory") and press Enter. Now, pressing Enter a second time will launch Notepad, and you can begin typing in the command-line statements that constitute your batch file.

For example, say you wanted to create a batch file that produces a directory listing of a folder, and puts that listing into a file named "Dirlist.txt". The command-line statement that produces a directory listing is DIR, and to direct DIR's output to a file you use the ">" operator. So the line you type into Notepad would be:

DIR > Dirlist.txt

Command-line statements are not case sensitive, so you don't need to type DIR in uppercase if you don't want to. After you've typed this into Notepad, save the file and exit. The last thing to do before actually invoking your batch file is to rename it to have a ".bat" extension instead of the default ".txt" extension. Here again, under Windows Explorer or File Explorer, right-click the file you just created ("Directory.txt") and select Rename from the Context menu. Use the right-arrow key to move over to the ".txt" portion of the file name and replace ".txt" with ".bat" and then press Enter. (Windows will ask you if you're sure you want to rename the file. Click Yes.) You should now have a file named "Directory.bat" in your folder.

One way to invoke a batch file is to double-click on it, so to actually execute your DIR command, double-click the "Directory.bat" file. Immediately, a window will briefly appear and disappear, and you'll now see a file named "Dirlist.txt" in your folder. This file was created by the commands in the batch file. By opening that file you'll see a directory listing of your C:\Bat folder.

Obviously, having a batch file that produces a directory listing of your C:\Bat folder is of limited value, but this tip was meant to give you an introduction to creating and invoking a simple batch file. But the batch processing capabilities are very powerful. See other tips to get a feel for using slightly more sophisticated batch files.

 This tip (13100) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Understanding Virtual Memory

Computers can address far more locations than may exist in physical memory. The key to this feature is the use of virtual ...

Discover More

Using Media Player to Burn an Audio CD

One of the many things you can do with Windows Media Player is to burn audio CDs. This tip shows you how.

Discover More

WizMouse

WizMouse was developed with one goal in mind: scroll the window under the mouse wheel even if it's not the active window. It ...

Discover More
More WindowsTips

PowerShell Input and Output

When dealing with a scripting language like PowerShell, one of the first things you need to learn is how to get data into and ...

Discover More

Using Batch Files, Part 3: The IF Command

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

Discover More

Using the SHIFT Statement

Sometimes your batch file needs to accept an unknown number of parameters. This is easy to deal with if you know about the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 + 0?

2017-01-23 21:18:24

John B. Windham

Brian C Doutaz,
As a work-a-round, try 'print' the tip (select the Microsoft printer) and then navigate to the folder where you want the file printed (saved).

That should save a *.PDF file to the folder where you can always go back there to re-read it as needed.

Works for me.


2017-01-23 15:20:29

Brian C Doutaz

Can anyone give me some idea of why, when I do a copy of this Tip (13100) (and any other tips in this series for that matter) (W10 on a Surface) the image that you have shown does NOT copy to the clipboard nor paste into any other program? As a matter of fact, pasting the tip into a Word doc (2003) instantly crashes the program and it has to be restarted.


2017-01-23 09:13:14

Jennifer Thomas

If you can write simple vba and simple html, you will have limited confusion writing bat files - just copy code and replace the variables.

For example, I know very little about the topic, but wrote a bat script to make an external copy of any file in our environment that can be customized by a user (eg custom PDF stamps, customized dictionaries or auto-correct lists, preference files, etc.) and then another script to put all that back after a same-system re-image and that has saved hours of time!

My point is don't be afraid of this level of programming - just have a backup plan in case you make a mistake, and go for it!


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.