If you are using Windows computer, you might have heard of the term batch file. If you haven’t, that’s fine! A batch file is a method of performing tasks without actually performing them.
In this post, we have discussed what is a batch file and what is its basic functioning.
What is a Batch File?
Every Windows user must be aware of Windows command line program Command Prompt, shortly known as CMD. You can execute actions by entering commands via a keyboard. A batch file works as a middleman between CMD and you. It is a file in .cmd, .btm or .bat extensions consisting Command Prompt commands. Whenever you run a batch file, the commands in it are executed in CMD in a sequence. Collectively set of commands is known as a batch script.
Usage of Batch File
You must be wondering why to store commands in a batch file. Well, keeping them in a batch file will save time and would save you from a trouble of typing same commands again and again. For example, you can set to restart or shut down your Windows computer at a certain time using CMD.
If you have created a batch file for this operation, you just need to double-click in order to run the file and Windows will shut down at a set time.
This comes in handy when you are a developer and want to use CMD on your PC while installing an app, you just need to include batch file in the setup files.
To summarize, a batch file is a script file which is used to automate tasks in DOS, Windows. When you are creating a batch file, you can use loops(for), control statements (goto), conditional statements (if) etc. You can run batch file just by typing its name in CMD interface. In addition to this, you can call one batch file from another batch file using CALL command.
Batch file modes
There are certain batch files which need you to make a selection to continue execution such as Yes or No to continue. This is known as an interactive mode wherein you need to provide an input to continue. There is another mode, batch mode in which a bat file runs without any interaction with the user.
Batch file commands
There are certain commands and things that you should know before knowing how to create a batch file in Windows computer. It is all about using commands and using them properly to maximizes the usage. Before going further, let’s know about basic commands that are used to make a batch file.
Title: Changes the title text on CMD window
Echo: Shows input string as output. You can use ON or OFF option for Echo. This feature when turned on, CMD shows which command is being executed.
:: : You can add a comment in a batch file. CMD overlooks the text written in batch file comment.
Cls: Clears the CMD screen.
Pause: Stops the Windows batch file’s execution.
EXIT: Exit command prompt
So, these are commands which are used commonly in a batch file.
Create a batch file in Windows 10
As now you know all the basic commands used in a BAT file, it is safe to start with creating a batch file in Windows 10. You can use the same steps in Windows 8, Windows 7 or older. So, let’s get started!
- Open Notepad. Create a new file.
- Type below mentioned commands in the file:
“echo off
Title my test batch file
::See the title at the top.
echo test file executed.
echo writing commands repetitively can be a boring
pause ”
- Now save the file and replace the extension .bat with .txt.
Note: It is necessary to check whether Hide File extensions feature is disabled. For that, locate Control Panel, then navigate to File Explorer Options. Go to View, now locate Hide extensions for known file types and check if this option is disabled. If not, remove the checkmark beside Hide extensions for known file types.
- Now to run the batch file, double-click it. The Command prompt window will come up with File Explorer Options with required output automatically.
- Next time, whenever you run the file, the text that you input will be displayed. You can use the .cmd instead of .bat extension as well.
Run A Batch file in Windows 10
One way of executing a .bat extension file is double-clicking the file. However, you can also run the file from CMD interface itself. In order to open a file with CMD, you have to locate it via folder/directory. You need to type the name of the file that you want to open. Let’s say you want to open TextFile, so type TextFile.bat and hit Enter key.
Modify or Edit A Batch File:
Now that you can create a batch file, let us know how to edit the .bat file. It is a quite simple process, all you need to do is right-click the file & and choose Edit. The text editor will open. Make necessary changes and save the file. So, next time when you run the .bat file, it will reflect the changes.
So, now you know what is a batch file, how to create a batch file or edit it in Microsoft Windows. You must be able to use commands in a batch file as per your preferences.