While using your computer, you may have come across files with a .bat extension. These are batch files. The batch files are text files saved typically with a .bat extension. These files contain command lines that can be interpreted by the Command Prompt. When you execute the batch file, the Command Prompt will run the saved commands in the batch file in sequence.
This means that you can do any tasks on your PC by creating a specific batch file for it. This saves a lot of time. But, how do you create a batch file? In this article, I have discussed the basics of how to write a batch file.
Also to save more time, learn how to pin the batch file on the taskbar to access it easily once you start your PC.
What Is A Batch File?
Contents
As mentioned above, batch files are scripts with command lines that can be interpreted by Command Prompt and run in sequence. Writing batch files to take care of certain tasks, rather than spending time on them is a wise decision.
You can change system settings, automate routines, and start apps, features, or websites using specific batch files. Below I have described the basics of how to write a batch file.
How Do You Create A Batch File?
Creating a batch file is a simple process. Essentially, all you need to do is write the codes for the desired function in any text editor and save it as .bat extension. The main thing about creating a batch file is writing the code. This is where you need to learn about the batch script.
The batch script is the language that you will use for writing codes, on any text editor, to create a batch file. If you have previous knowledge of coding, then you won’t likely have any issue learning the basics of batch script. Here are some basic commands in the batch script that should introduce you to the language:
ECHO: This command allows the programmer to view the working script in the command prompt. If you want to view the working code, then you will need to use this command.
TITLE: Using this command the programmer can add the title of the batch script in the Command Prompt.
CLS: This command is used to clear the command prompt.
REM: Rem is the comment command that programmers use to share information about the code among each other. Anything written with the rem command won’t be applicable in the batch file and will only remain as comments in the batch script.
PAUSE: This is a break command that stops the logical chain and allows users to read the command lines and proceed with the function.
START “” [website]: This command opens the mentioned website using the default browser.
COPY: As the command name suggests, it is used to copy files from one location to the other.
EXIT: This command is used to terminate the command prompt window.
There are other commands that you will need to know for creating batch files with advanced functions. So, how do you create a batch file? Let’s create some simple batch files to help you understand.
How To Write A Batch File That Returns “Hello World”?
We will start with a simple batch file that returns the “Hello World” value in the command prompt. To write this simple code, we will use some of the above commands.
- First, open Notepad or any other text editor on your PC.
- Enter the following commands:
ECHO OFF
ECHO “Hello World”
PAUSE
- Save the file with any name but include the .bat extension in the end. This will create the batch file.
- Now, run the created batch file.
After you have run the batch file, you will see the following output in command prompt.
These are the basics of how to write a batch file. Next, we will learn how to write some batch files to execute some network diagnostic commands.
Writing A Batch To Run Basic Network Diagnostics
Batch files are used to execute long command lines to save the users’ time. Network issues are pretty common for Windows users and it can be annoying to run the network diagnostic commands one by one each time it happens.
Here is how you can write a simple batch file to run ipconfig /all, ping and tracert command all at once.
- Open the Notepad or any other text editor again.
- Type the following command:
ECHO OFF
ipconfig /all
ping digicruncher.com
tracert digicruncher.com
PAUSE
- Save the file with .bat extension to create the batch file.
Now, run the batch file on your computer. First, it will output the network information, then check if DigiCruchers’s servers are responding and finally traceroute to the website to check for any issues. Similarly, you can create other batch files to conduct specific tasks for you.
How To Edit The Batch File?
When you are creating a complex batch file, you will likely have to repeatedly edit it to make it work as you desire. It is very simple to edit a batch file. Right-click on the batch file in your PC and select the Edit option from the right-click context menu.
The text editor will open, and you will be able to edit the codes. Save the file by press Ctrl + S or from the File tab to save the changes made in the batch script.
How To Run Batch File As Administrator?
Many command lines require administrative privileges. But, when you simply run a batch file by double-clicking on it, the batch file runs in normal command prompt. To run batch file as administrator, follow the steps given below:
- Right-click on the batch file in your PC.
- Select the Run as administrator option from the right-click context menu.
- Click on the Yes option in the UAC prompt.
Now, the batch file should run as an administrator.
Wrapping Up
So, there you have it. “How do you create a batch file?” question should be answered for you now. This article shows the basics of the batch script to create simple batch files. To create advanced batch files, you will need to increase your knowledge of the batch script. If you have any questions related to this article, ask them in the comment section below.
Leave a Reply