How To Write A Simple Batch (.bat) File

Ads by Google

batfile   How To Write A Simple Batch (.bat) FileBatch files are the computer handyman’s way of getting things done. They can automate everyday tasks, shorten the required time to do something, and translate a complex process into something anyone could operate.

Since automation programs like AutoHotKey exist, many people have never written or taken the time to understand bat files, and many don’t even know what they do.

In this article, I’m going to show you how to write a simple batch file and present some basics that a user will need to understand when writing one. I’ll also provide you with a few resources for learning to write batch (.bat) files in case you’d like to go further.

Let’s say that you frequently have network issues; you’re constantly getting on the command prompt and typing in things like “ipconfig” and pinging Google to see what the problem is. After a while you realize that it would be a bit more efficient if you just wrote a simple BAT file, stuck it on your USB stick, and used it on the machines you troubleshoot.

Step 1: Create A BAT File

Create a new text document on your desktop. Double click the file – it should be blank inside. Now, go to file>save as, and in the “Save As” window, input a name for your BAT file and then add a “.bat” on the end (without the quotes). My file was named testBAT.bat, for instance.

Ads by Google

Before hitting save we need make sure that Windows doesn’t stick the standard “.txt” ending on the end of our new file. To do this, change the filetype from “.txt” to “all files” as shown in the screenshot below. That’s it – hit save and close the file.

CropperCapture1   How To Write A Simple Batch (.bat) File

Step 2: Learn Some Quick Code

If you know how to run commands in the command prompt, you’ll be a wiz at creating BAT files because it’s the same language. All you’re doing is telling the command prompt what you want to put in through a file, rather than typing it every time you run the command prompt. This saves you time and effort; but it also allows you to put in some logic (like simple loops, conditional statements, etc. that procedural programming is capable of conceptually).

There are SEVEN simple commands I want to familiarize you with for this program. Commands are NOT case sensitive, so don’t worry about that.

TITLE - The Window name for the BAT file.

ECHO - the “print” statement for BAT files. Anything following the word ECHO will be displayed in the command prompt as text, on its own line.

ECHO OFF – BAT writers typically put this at the beginning of their files. It means that the program won’t show the command that you told it to run while it’s running – it’ll just run the command. I’d recommend that after you run this test program, you try removing this line from your code to see what happens.

PAUSE - This outputs the “press any key to continue…” message that you’ve seen all too many times. It’s helpful because it pauses the BAT file execution until the user tells it to go again. If you don’t put this in your program, everything will speed by and end before you can see it. People typically put this in BAT files to give the user a chance to review the material on the screen before continuing.

CLS - Clears the DOS window (helpful if things get too cluttered!).

IPCONFIG – Outputs a lot of network information into your DOS box (network admins have dreams solely based off this command).

PING - Pings an IP, letting you know if your computer was able to contact it. This command also returns the latency (ping time) and by default pings three times.

Step 3: Do Some Quick Logic

We need to plan our program out. Any good programmer will think about the general framework of their program before they dash into things – it prevents them from making logic mistakes that are hard to back out of.

For this program, we want to check the computer’s network/internet settings with an “ipconfig /all” command, and then review that information by giving the user time to read everything. Afterwards, we want to ping to figure out if we really truly have access to the internet. We’ll pause the program after this as well, because we want to know for sure that they saw it.  OK. Very simple program, very simple logic. Let’s write some code.

Step 4: Write Your BAT File

Right click your BAT file and click “edit” to bring up Notepad. The whole document should be blank – ready for some epic programmer input.

Rather than walking you line by line through the code (it’s extremely short) I’m going to use a code comment (example–   CODE  ::Comment) to let you know what we just did.I’m putting the actual code in bold to make things a bit easier to process.

———–Start Code———–

::CMD will no longer show us what command it’s executing(cleaner)
ECHO As a network admin, I’m getting tired of having to type these commands in! Hopefully, this saves me some time in the long run.
:: Print some text
:: Outputs tons of network information into the command prompt
:: Lets the user read the important network information
:: Ping google to figure out if we’ve got internet!
ECHO All done pinging Google.
::Print some text
:: Give the user some time to see the results. Because this is our last line, the program will exit and the command window will close once this line finishes.

———–End Code———–

Step 5: Run Your BAT File

Save the file you just coded (or copy/paste mine in, it will run as written), and double click it. Your output should be something like the screenshot below.

CropperCapture2   How To Write A Simple Batch (.bat) File

Congratulations! You’ve written a batch file successfully.

If you want to learn more about bat files, I’d recommend you check out the commands available to the language. From there, your best bet is to write your own, or follow more examples online. Feel free to comment here if you have a BAT-related question.

The most useful BAT I’ve made so far is one that allowed me to compile and run Java programs with a single command, saving me countless amounts of command typing in the long run (because I compile/run so often when programming). I’ve also made one that sets file associations up the way I want them when I plug in my flash drive to a PC – this makes it possible for my portable apps to be the default app right from the get-go with a new computer.

Have you written anything cool with BAT files before?

Ads by Google

16 Comments - Write a Comment



Batch files are still kind of fun really just to get them to do some simple things. These are still handy for starting up some simple tasks with a minimal amount of code necessary to get the tasks done.



I Just Loved To Read This Post.
Really I Love Such Type Of Programming.
I Really Love Programming But Sadly Donno As Now I M Only Of 14 Years And In School.
So Don Have Time To Do These.

Paul Bozzay

Glad you enjoyed it! Thanks for the feedback :-)


Ivan Kolevski

I think that batch files will be around for a while. I use batch files on a daily basis and it is very time efficient method of getting things done with minimal input.
Here is my daily batch:
ECHO This is CCleaner silent run with DNS flush. I use this after I log in to my bank and close Firefox.
“C:\Program Files\CCleaner\CCleaner.exe” /AUTO
ipconfig /flushdns
ECHO Privacy is important !!! All good now.

Hope this helps.
Ivan K.



Is there a batch command that would allow snapping a window to the right/left half of the screen in Windows 7?

I am trying to make a .bat file that opens 2 Windows Explorer windows, each on its half screen.

Paul Bozzay

I don’t know of one–I googled it and came up empty. I’ll look around a bit more though. If you get something let me know–I’d like to know this too.


Windows 7 has gestures, you can drag windows to ceertain parts of the screen side and it will snap them into position. Google for more information on it.



Can a batch file open several programs that have to keep running (e.g. I want a batch file that opens Chrome, Itunes, etc. but when I tried it, it stopped after chrome, and only continues when I closed chrome.)
Any solutions?

Paul Bozzay

Sure can.

Try code like this… (replace paths/names accordingly)

start /d “C:\Windows\System32\” calc.exe
start /d “C:\Program Files\Mozilla Firefox” firefox.exe
start /d “C:\Users\user\AppData\Local\Google\Chrome\Application\” chrome.exe

Tested it 30 seconds ago and everything ran flawlessly. Let me know if it works for you!



Nice beginners guide to batch scripting. VBscript and powershell are great tools and have their place but batch files are indispensable. I work in IT and am often surprised at how few of my colleagues actually utilize batch files for daily tasks. In the 10+ years I’ve been doing this I’ve written literally hundreds of batch files with at least 10 that I use daily. The best one I’ve ever written runs against my print servers and pulls out all the print job information (Username, doc type, doc name, pages printed etc) and puts that into a csv file. Very useful for tracking printer usage.

Paul Bozzay

Exactly! Thanks for taking the time to mention this; I think a lot of us feel the same way about batch files.



Paul Bozzay

A lot of people (as you can see) still use batch files…For this reason they are not only relevant, but useful too. With just a few lines of very easy code, you can perform an automated operation on any Windows machine…To each his own, but I personally resort to using BAT files frequently.




Here is a usefull site for scripting not just Batch Scripting but it does have a very comprehensive section on batch scripting and it is an invaluable resource for those who routinely write scripts.



Here is a usefull site for scripting not just Batch Scripting but it does have a very comprehensive section on batch scripting and it is an invaluable resource for those who routinely write scripts.



My job involves setting up new computers for workers at a large corporation. The guy in the job before me would network two computers and copy files manually from folder to folder. Could take hours and failed frequently, forcing you to restart because you had no idea where Windows left off. I wrote a batch script that maps a network drive to the old computer and copies everything that a user has created that is not in our standard image of windows. Now I sit back and watch it run.



Hi to all you guys who are keen on Batch Files.
Can batch files be created to run automatically i.e. triggered by an event? Would a batch file be an appropriate way of deleting an old file when an imaging software starts to create a new file. I have space on a partition to store say 3 files & would like to be able to automatically delete the oldest (by date) file before the next/newest (4th?) file is created.

Your comment