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 learn 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 Write a Batch File in Windows
Before going into the details, here is a quick summary
- Open a text file, such as a Notepad or WordPad document.
- 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.
- Save your file with the file extension .bat, for example, test.bat.
- To run your batch file, double click the BAT file you just created.
- To edit your batch file, right-click the BAT file and select Edit.
Your raw file will look something like this:
And here’s the corresponding command window for the example above:
If this was too quick or if you want to learn more about 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.
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 — welcome.bat for example — and click OK. This will finalize the batch process. Now, double-click on your newly created batch file to activate it.
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, 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 through 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 which holds the extensive library of batch script parameters and variables at your disposal.
Step 3: Write and Run Your BAT File
We’ll create two examples of batch scripts which can simplify your daily online and offline activities.
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 start "" 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 (*.*).
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.
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 the my desktop before, a loose assortment of image files.
Here are those same files afterward.
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.
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.
Take things even further by incorporating IF statements into your batch scripts. And if you’re really keen, you may even want to learn PowerShell scripting instead.
Read the full article: How to Write a Simple Batch (BAT) File