World of computers is growing at a very good pace. Here this post is on how to create a batch file. No matter how complex things get in it, people are still getting attracted towards it. There are various problems that people face while working with computers like create a batch file to run exe. One of them is writing the same code over and over again. The biggest problem is that people indulged in the world of computers cannot even get out of this scene of coding. The problem persisted until batch files came to the rescue.
What is a batch file and why do you need it?
Many of us work on windows and anything that does not have a good graphical interface is a sudden turn off for us. Command prompt or commonly known as CMD is one of those things that do not have a good graphical interface. This is a window that accepts inputs as commands from the keyboard and then executes them in the order written. Now, imagine a scenario that you need to write a set of batch files commands over and over again. Batch file takes you out of the scenario. This can be thought of an executable file that executes commands written in it blindly in the order that you wrote them in. This saves a lot of time and is super convenient for the coder.
The batch file can also be understood as doing some task, without actually doing it. How easily does this make things, you can reuse the code you once wrote. For example, if you want to switch off your computer after one hour automatically. You just need to write a code for it and then you can reuse it just by clicking on the icon like you are opening an application. You can even make use of for, if and goto statements. You can open the file directly in the cmd by writing its name and you can even open another batch file by using the call command.
Things to know before you create a batch file.
Computers have grown excessively easy to learn. But still, there are certain things that you need to know before you start working on it. Working with a batch file is not that a usual task. A very limited amount of people work on it! There are certain things that you should know before you start working on a batch file.
- Title- as the name suggests, the title is the name you give that will be further used to refer to your batch file. The thing to be kept in mind while giving a title is to give it something related to the task that is carried out. Imagine a file named ABC having a function to restart the computer, every time you want to use it you will have to open the file, check the contents and then use it. Whereas if you rename it as restart then you can easily use it.
- Echo- this is a feature of cmd that you can toggle between on or off. If you toggle it to on then cmd will start displaying the processes that it will be performing.
- Pause-this is a command that stops the execution of the batch file.
- Cls- this command is used to clear the screen of command prompt.
For opening a file in cmd you need to get to that directory and then simply write the file name with extension.
How to create a batch file & How to run a batch file
Steps to create the batch file are as follows-
- Open notepad to start coding your batch file. You can also use any similar text editor to start the process.
- Type the commands that you wish to include in your batch file.
- Save the batch file with .bat instead of .txt, this is the only difference between a batch file and text file.
One thing that needs to be kept in mind is that the hide extension feature in windows should be turned off else you will not be able to change the extension of the file. For opening this file, you just need to double-click on the file icon, then a black command prompt window will open and without doing anything you will get the desired output. this also helps you create a batch file to run command.
Conclusion
Code reusability is one of the best things in the computer coding. The coders are always desirable to reduce their work and code reusability gives that conform to them. At initial levels there are only shortcodes to be written but as and when you elevate in your career the length of code gets huge. This is when the real importance of code reusability comes out. Writing the same commands on a black screen over and over again is probably the most boring work of this world but there is no need to worry because our ancestors found us the solution, the batch file.