When you’re writing a batch script on Windows, it’s important to be aware of the different types of input that can be used. In this article, we’ll take a look at how to write a batch script that takes input from a text file and prints out the results. First, we need to create a new file called “bat_script.txt” in the same directory as our script. This file will contain our batch script’s instructions. Next, we need to create an empty text file called “results.txt” in the same directory as our bat_script.txt file. This file will contain the results of running our batch script. Now, we need to add some code to our bat_script.txt file so that it can run properly. We’ll start by adding the following line to the bottom of our textfile: #!/usr/bin/env bash #This is your basic batch script code # #This is where you put your actual data # export PATH=$PATH:$HOME/.local/bin
Do you know how to use the Command Prompt? If you do, you can write a batch file. In its simplest form, a batch file (or batch script) is a list of several commands that are executed when you double-click the file. Batch files go all the way back to DOS, but still work on modern versions of Windows.
PowerShell scripts and Bash scripts may be more powerful, but batch files can still be plenty useful if you need to run basic Windows commands.
Batch File Basics
A batch file is simply a text file saved with the .bat file extension. You can write one using Notepad or a more advanced text editor like Notepad++, but don’t use a word processor like Microsoft Word.
Let’s create a simple batch file. First, open Notepad. Type the following lines into it:
Next, save the file by clicking File > Save. Give it any name you like, but replace the default .txt file extension with the .bat extension.
For example, you might want to name it hello_world.bat .
You now have a batch file with the .bat file extension. Double-click it to run it. This particular batch file sets ECHO off (which cleans up the output by hiding the commands from being printed at the prompt, prints the text “Hello World” to the screen, and then waits for you to press a key before it ends.
If you didn’t add PAUSE to the file, the batch file would simply run its commands and then automatically close. In this case, it would print “Hello World” to the window and then immediately close the Command Prompt window. When you want to quickly run commands without seeing the output, you can omit this. If you’re running several commands, you could place the PAUSE command in between them.
Writing a More Complex Batch File
It’s fundamentally simple to create a batch file. The only thing you need to change is what you type into Notepad. To run several commands, you type each one on its own line and the batch file will run each one in order.
For example, let’s say we want to write a batch file that runs several network diagnostic commands. We might want to run ipconfig /all to view network information, ping google.com to see if Google’s servers are responding, and tracert google.com to run a traceroute to google.com and see if there are any problems on the way.
In the most basic form, we could simply place all those commands in a batch file, one after the other, like so:
When we run this file, we’d just see the output of each command right after the other. But this isn’t necessarily the ideal way to write a batch file.
For example, you might want to add comment lines. Any line that begins with a :: is a comment line and won’t be executed. That makes them a useful way to explain what’s happening in the file for anyone you might give it to–or for your future self, who might forget why you put a certain command in there.
You might also want to add the “ECHO OFF” command to the beginning of the file. This is typically added to the start of most batch files. When you do this, the commands themselves won’t be printed to the Command Prompt, but the results will be. For example, you’ll see the network connection details but not the “ipconfig /all” line. Most people don’t care to see the commands, so this can clean up the output.
So here’s what that might look like:
There are other directions you could go with a batch file like this. For example, you might want to have your batch script run the above commands and then dump the output to a text file you can view later. To do so, you’d use the » operator after each command to append its output to the text file. As we’re going to read the output from the text file anyway, we can omit the PAUSE command.
After you run the above script, you’d find a file named results.txt in the same folder as the batch file with the output of the commands. The Command Prompt window will automatically close once the batch file is done running.
The example we’re using above relies on actually printing information to the Command Prompt so the user can read it. However, many batch files are designed to be run non-interactively. For example, you could have a batch file that deletes multiple files or directories whenever you double-click it. You’d just need to use the del command to delete files or the deltree command to delete directories. Remember, you’re just using the same commands you’d run in a Command Prompt window.
Fundamentally, that’s the point of most batch files–just running a few commands one after another. However, batch files can actually be significantly more complex than this. For example, you can use “IF” statements along with the “GOTO” command to check the value of something and then skip to different lines depending on the result. This is more like writing an actual small program than a quick and dirty script. That’s one reason why .bat files are sometimes called “batch programs.” If you want to do something more complex, you’ll find plenty of guides to doing specific things with batch programming online. But now, you know the basics of how to throw a simple one together.