Batch files can be used to store a series of commands which can then used by the command line interpreter CMD as an input. As we know, CMD or Command Prompt can take various commands as input and processes them. To enter the command we open CMD and type the commands directly. The thing is, we can save all our commands in a text file and save them. This is where batch file comes into play.
A batch file is a text file with .bat, .cmd, or .btm file extensions, containing the CMD commands. When we run a batch file, the commands written in it are executed one by one in the Command Prompt.
Why use a batch file?
A batch file can be used to complete repetitive or common tasks. It is a script file used to automate tasks. In a batch file we can use loops (for), conditional statements (if), control statements (goto), etc. We can run a batch file directly from the command prompt by typing its name. Also, we can run one batch file from another batch file using the CALL command.
Batch file prerequisites
Before we can create a batch file, we have to be familiar with writing CMD commands. We need to be familiar of some basic Windows CMD Commands which will help us create basic batch files. Also, when creating batch files we should know that:
- title: used to change the title text displayed on top to CMD window
- echo – used to display the string as the output. We can use ON or OFF option for ECHO to turn the echoing feature on or off. If we turn on the ECHO, the CMD will display the command it is executing
- pause – used to stop the execution of Windows batch file
- exit – used to exit the Command Prompt
- cls – used to clear the command prompt screen
- :: or REM – used to add a comment in the batch file
How to open a batch file
To open a batch file using cmd, we need to navigate to that folder/directory using the command line and then type the name of that file along with its file extension. For example, if we have a file in “C:\test.bat”, we would first navigate to the folder using the command:
Then we would run the batch file by entering the name of the file and press Enter:
We can also simply double-click the .bat file and it should run.
How to create a batch file in Windows?
Let’s see how to create a simple batch file.
Open a new notepad file. You can also use any similar text file editor, like Notepad++.
We will enter the following commands in the text file:
echo off title saadz26.sg-host.com Batch file :: This line is a comment echo This line will be written to the terminal pause
Next, we have to save the text file with the extension .bat instead of .txt. In our case we will name it test
Keep in mind that Notepad will first offer to save the file with .txt extension, so we have to make sure to change that to the .bat extension. If you don’t see the extension, you can go to Control Panel and then choose File Explorer Options > View tab > Uncheck Hide extensions for known file types.
Now that we have a test.bat file, we can simply double-click it to run it.
If we change the first line to echo on, we will get the following:
The command echo off is typically added to the start of most batch files. With that commands themselves won’t be printed to the Command Prompt, but the results will be. If we put “@” in front of “echo off”, we won’t see that line in the output. Also note that CMD will pause and wait for us to press any key. If we remove the pause command from the test.bat file, CMD would simply run the file and then close. We can have as many pause commands as we want. For example, we can put pause between other two commands.
Something a bit more complex
Let’s now try to check connectivity to saadz26.sg-host.com. For that we will enter the following set of commands in our .bat file:
@ECHO OFF title saadz26.sg-host.com Batch file :: This line is a comment echo I will now test connectivity with saadz26.sg-host.com echo Results will be saved to results.txt ping www.saadz26.sg-host.com >> results.txt echo Done! pause
This line will execute the ping command. The “>>” part means that we want to save the command results to the “results.txt” file. This is great when we want to save data to be reviewed later.
Let’s see another example. Let’s go trough every file in a folder where our .bat file resides and save the file names to the “results.txt” file. The script will look like this:
In the example above, the REM stands for “remark” and is used for commenting our code. The FOR command will iterate over files in a folder, which are marked with %%f. The (*) part is actually a list of elements, and with the “*” we actually say “any file”. Next, the DO part will, for each file, echo the file name to the “results.txt” file (lines will be appended).