Table of Contents
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 or create a new text file using Notepad or WordPad document or any text editor.
2️⃣ Add your commands:
- @echo off : 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 This is your first batch script: 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.
- echo Welcome to batch scripting: Print “Welcome to batch scripting”
- 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.
@echo off
title This is your first batch script.
echo Welcome to batch scripting.
pause
3️⃣ Save your file with the file extension . Navigate to the File menu | Select Save As…
4️⃣ In the Save As window, change Save as type to All Files then type a filename with .bat as the file extension.
5️⃣ Now, double-click on your newly created batch file to run it. To edit the batch file, let’s right click on it then select Edit.
And here’s the corresponding command window for the example above:
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.
More basic parameters for batch scripting:
- 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.
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.
Examples of batch scripts
We’ll create examples of batch scripts which can simplify your daily online and offline activities.
The below script stacks one start “” parameter on top of the other to open some websites using a batch file on multiple tabs. You can replace the links provided with ones of your choosing.
Without the pause parameter, the batch file will close automatically after executing the last command.
@echo off
start "" http://www.bbc.com
start "" https://google.com/
start "" https://bonguides.com/
Batch file that opens multiple programs
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 file location. Let’s say you need to do some work, and you want to open Excel, the Calculator, and Microsoft Edge. Here’s the code for that:
@echo off
cd "C:\Program Files\Microsoft Office\root\Office16\"
start excel.exe
start calc.exe
cd "C:\Program Files (x86)\Microsoft\Edge\Application"
start msedge.exe
Batch file to restart your computer
The below script will restart your computer after 5 seconds.
@echo off
shutdown -r -t 5
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 in masse, or creating copies of important documents, you can make tedious tasks simple with batch scripts.