We all have heard about the batch file. So what exactly a batch file is? Let’s find out in this post.
A batch file is a sequence of commands written in a file which executes them one by one sequentially without any external force. In windows batch file ends with an extension .bat and is an executable file.
A batch file is used to load a program, or to perform some actions one by one. There are many control statements that can be used in a batch file. For example, if-else, while loop, break, continue can be used in a batch file to somehow get some control over the execution flow.
Batch files were added to the system keeping in mind the end users so that tasks which are redundant in nature can be automated efficiently. There are many software which uses the batch file for their first time initialization such s creation of software in the backend when they are installed.
One of the main advantages of a batch file is non-interactive execution, means the system can be used for other tasks at the same time while a batch file execution is in progress.
In other operating systems like Linux or Unix, a batch file can be in the form of a shell script file which can execute same as a batch file in windows but it is more flexible.
As by now, since you know the basics about a Batch file now, let’s create one:
But before creating a batch file there are some basic CMD commands which you have to understand. Creating a batch file all about how properly you craft the commands. Each command tells your system to do something. So how you make them work in your favor is completely dependent upon you. Here is the list of basic commands I found on WIKIHOW:
- ECHO – Displays text on the screen
- @ECHO OFF – Hides the text that is normally output
- START – Run a file with it’s default application
- REM – Inserts a comment line in the program
- MKDIR/RMDIR – Create and remove directories
- DEL – Deletes a file or files
- COPY – Copy a file or files
- XCOPY – Allows you to copy files with extra options
- FOR/IN/DO – This command lets you specify files.
- TITLE – Edit the title of the window.
So now let’s create a basic batch file:
Batch file for creating a new directory.
Open notepad or any text editor which you like and type in the following:
Save this file as abc.bat and double click it to see the magic.
Save this file as abc.bat and double click it to see the magic. But this time, be aware to save your work as this will shut down your system in 60 seconds unless until you kill the process before it.
There are n number of things that you can do with the batch file. Almost every windows software uses this utility for running many functions in the background. You can even run your PowerShell script from the batch file.
Here is the CMD reference if you are planning to get to the pro level in the game.