Creating a batch file to automate repetitive tasks is easy, once you know how.

Batch 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.

In this article, you'll see how to write a simple batch file. You'll learn the basics of what batch files can do, and how to write them yourself. I'll also provide you with further resources for learning to write batch (BAT) files.

How to Create a Batch File on Windows

To create a Windows batch file, follow these steps:

  1. Open a text file, such as a Notepad or WordPad document.
  2. Add your commands, starting with @echo [off], followed by, each in a new line, title [title of your batch script], echo [first line], and pause.
  3. Save your file with the file extension BAT, for example, test.bat.
  4. To run your batch file, double-click the BAT file you just created.
  5. To edit your batch file, right-click the BAT file and select Edit.

Your raw file will look something like this:

A simple batch file with the most basic elements.

And here's the corresponding command window for the example above:

Test BAT CMD Prompt

If this was too quick, or if you want to learn more about BAT file commands and how to use them, read on!

Step 1: Create a BAT File

Let's say that you frequently have network issues; you constantly find yourself on the command prompt, typing in ipconfig, and pinging Google to troubleshoot network problems. 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.

Create a New Text Document

A batch file simplifies repeatable computer tasks using the Windows command prompt. Below is an example of a batch file responsible for displaying some text in your command prompt. Create a new BAT file by right-clicking an empty space within a directory and selecting New, then Text Document.

Open New Text File in Windows

Add Code

Double-click this New Text Document to open your default text editor. Copy and paste the following code into your text entry.

          @echo            off
title This is your first batch script!
echo Welcome to batch scripting!
pause

Save as BAT File

The above script echoes back the text "Welcome to batch scripting!" Save your file by heading to File > Save As, and then name your file what you'd like. End your file name with the added BAT extension, for example welcome.bat, and click OK. This will finalize the batch process. Now, double-click on your newly created batch file to activate it.

Test BAT CMD Prompt

Don't assume that's all batch scripting can do. Batch scripts parameters are tweaked versions of command prompt codes, so you are only limited to what your command prompt can do. For those unfamiliar with the program, the command prompt is capable of quite a lot.

Step 2: Learn the Basics of Batch Scripting

Batch files use the same language as the command prompt. All you're doing is telling the command prompt what you want to input using a file, rather than typing it out in the command prompt. This saves you time and effort. It also allows you to put in some logic, like simple loops, conditional statements, etc. that procedural programming is capable of conceptually.

@echo: This parameter will allow you to view your working script in the command prompt. This parameter is useful for viewing your working code. If any issues arise from the batch file, you will be able to view the issues associated with your script using the echo function. Adding a following off to this parameter will allow you to quickly close your script after it has finished.

  • title: Providing much of the same function as a <title> tag in HTML, this will provide a title for your batch script in your Command Prompt window.
  • cls: Clears your command prompt, best used when extraneous code can make what you're accessing had to find.
  • rem: Shorthand for remark provides the same functionality as <!-- tag in HTML. Rem statements are not entered into your code. Instead, they are used to explain and give information regarding the code.
  • %%a: Each file in the folder.
  • (".\"): The root folder. When using the command prompt, one must direct the prompt to a particular directory before changing a files name, deleting a file, and so on. With batch files, you only need to paste your BAT file into the directory of your choosing.
  • pause: Allows a break in the logical chain of your BAT file. This allows for users to read over command lines before proceeding with the code. The phrase "Press any key to continue..." will denote a pause.
  • start "" [website]: Will head to a website of your choice using your default web browser.
  • ipconfig: This is a classic command prompt parameter that releases information concerning network information. This information includes MAC addresses, IP addresses, and sub-net masks.
  • ping: Pings an IP address, sending data packets through server routes to gauge their location and latency (response time).

The library for batch variables is huge, to say the least. Luckily there is a Wikibook entry that holds the extensive library of batch script parameters and variables at your disposal.

Step 3: Write and Run Your BAT File

We'll create three examples of batch scripts which can simplify your daily online and offline activities.

News Script

Let's create an immediately useful batch script. What if you wanted to open all your favorite news websites the moment you wake up? Since batch scripts use command prompt parameters, we can create a script that opens every news media outlet in a single browser window.

To re-iterate the batch-making process: first, create an empty text file. Right-click an empty space in a folder of your choosing, and select New, then Text Document. With the text file open, enter the following script. Our example will provide the main American news media outlets available online.

          @echo            off
cd " " http://www.cnn.com
start " " http://www.abc.com
start " " http://www.msnbc.com
start " " http://www.bbc.com
start " " http://www.huffingtonpost.com
start " " http://www.aljazeera.com
start " " https://news.google.com/

The above script stacks one start "" parameter on top of the other to open multiple tabs. You can replace the links provided with ones of your choosing. After you've entered the script, head to File, then Save As. In the Save As window, save your file with the BAT extension and change the Save as type parameter to All Files (*.*).

Saving a Text File as a BAT File

Once you'd saved your file, all you need to do is double-click your BAT file. Instantly, your web pages will open. If you'd like, you can place this file on your desktop. This will allow you to access all of your favorite websites at once.

File Organizer

Have you been downloading multiple files a day, only to have hundreds of files clogging up your Download folder? Create a batch file with the following script, which orders your files by file type. Place the BAT file into your disorganized folder, and double-click to run.

          @echo            off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ " " if "%%~dpxa" NEQ "%~dpx0" (
rem check if extension folder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Move the file to directory
move "%%a" "%%~dpa%%~xa\"
))

Here is an example of my desktop before, a loose assortment of files types.

Messy Windows Desktop

Here are those same files afterward.

Organized Windows Desktop

It's that simple. This batch script will also work with any type of file, whether it's a document, video, or audio file. Even if your PC does not support the file format, the script will create a folder with the appropriate label for you. If you already have a JPG or PNG folder in your directory, the script will simply move your file types to their appropriate location.

Program Launcher

If you find yourself opening the same set of apps over and over again, you can now create a custom launcher batch file that opens multiple programs with a single click. All you need to find out is the Windows file location. Let's say you need to do some work, and you want to open Excel, the Calculator, and Spotify. Here's the code for that:

          @echo            off
cd "C:\Program Files\Microsoft Office\root\Office16\"
start EXCEL .EXE

You could even have your batch file open specific documents or websites, along with a set of apps. The trick is to mix-and-match all the different elements a batch file can do for you. Eventually, you'll be incorporating IF statements into your batch scripts.

Automate the Simple Stuff With Batch Scripts

This is just a taste of what batch scripts have to offer. If you need something simple done over and over, whether it be ordering files, opening multiple web pages, renaming files en masse, or creating copies of important documents, you can make tedious tasks simple with batch scripts.