Once in a while you have found yourself running some cmd commands over and over, well from personal experience, I will tell you it's very cumbersome. Most people end up using softwares such as FastKeys and Auto HotKey to accomplish this tasks. On the other hand, if you like getting your hands dirty like me, batch files are the way to go.
According to Wikipedia " In DOS, OS/2, and Windows, a batch file is a type ofscript file, a text file containing a series of commands to be executed by thecommand line interpreter." Read more here
Creating a batch file
Creating a batch file is easy. Simply open notepad and create a new file then save it using a .bat, .cmd or .btm extension. On notepad or any other text editor( do not confuse with word processor e.g word Pad), make sure to set the "save as type" to All. This will make sure the .bat, .cmd or .btm file is not overwritten with a .txt extension at the end.
That's it, the file is now ready to be filled with some commands.
Some few commands you should know
Apart from the cmd commands you often execute, here are a few key words you should know in order to write a good batch file.
- echo - in a batch file, this is used to output text
- echo off - this tells the command interpreter to not display the commands
- pause -makes the window freeze and asks the user to press any key to continue
- cls - this is used to clear the window
- :: - this is used to write comments.
NB: In Windows, commands are not case sensitive.
Editing the batch file
I will be creating a simple batch file to check whether the computer is connected to the internet by pinging Google. If the ping is successful we will know the internet connection is working. Type this into your batch file and save it.
echo off
:: disables viewing of the cmd commands
echo this batch file pings google
ping www.google.com
echo Done pinging google.
:: disables viewing of the cmd commands
echo this batch file pings google
ping www.google.com
echo Done pinging google.
:: it will out put done pininging google when the ping is done whether successful of not
pause
:: this makes the window not to auto close when done
pause
:: this makes the window not to auto close when done
Notice how the comments start on a new line (::), well that's how they are supposed to be written or else they will be output as text on the command prompt.
How a batch file looks like
To run the batch file, just double click it or right-click and choose open file.
No comments: