How to write simple batch(.bat) file

Batch files are the computer handyman’s way of getting things done. They can automate everyday tasks, shorten the required time to do something, and translate a complex process into something anyone could operate.

1.Create a .bat file
Create a new text document on your desktop. Double click the file – it should be blank inside. Now, go to file>save as, and in the “Save As” window, input a name for your BAT file and then add a “.bat” on the end (without the quotes). My file was named testBAT.bat, for instance.

Before hitting save we need make sure that Windows doesn’t stick the standard “.txt” ending on the end of our new file. To do this, change the file type from “.txt” to “all files” as shown in the screenshot below. That’s it – hit save and close the file.

2.Have some knowledge on Coding
ECHO - the “print” statement for BAT files. Anything following the word ECHO will be displayed in the command prompt as text, on its own line.

ECHO OFF – BAT writers typically put this at the beginning of their files. It means that the program won’t show the command that you told it to run while it’s running – it’ll just run the command.

PAUSE - This outputs the “press any key to continue…” message that you’ve seen all too many times. It’s helpful because it pauses the BAT file execution until the user tells it to go again.

CLS - Clears the DOS window (helpful if things get too cluttered!).

IPCONFIG – Outputs a lot of network information into your DOS box (network admins have dreams solely based off this command).

PING - Pings an IP, letting you know if your computer was able to contact it. This command also returns the latency (ping time) and by default pings three times.

3.Write here your BAT file

ECHO OFF
-->CMD will no longer show us what command it’s executing(cleaner) ECHO As a network admin.

-->Print some text
IPCONFIG /ALL
-->Outputs tons of network information into the command prompt

PAUSE
-->Lets the user read the important network information

PING www.google.com
-->Ping google to figure out if we’ve got internet!

ECHO All done pinging Google.
-->Print some text

PAUSE
--> Give the user some time to see the results. Because this is our last line, the program will exit and the command window will close once this line finishes.

4.Run your .bat files
Spare the record you simply coded (or duplicate/glue mine in, it will keep running as composed), and double tap it.

No comments

Powered by Blogger.