Friday 4 May 2012

HOW TO CREATE A BAT FILE


how to write a batch file

Step 1: Create A BAT File

Create a new text document on your desktop. Double click the file – it should be blank inside. Now, go tofile>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 namedtestBAT.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 filetype from “.txt” to “all files” as shown in the screenshot below. That’s it – hit save and close the file.

how to write a batch file










Step 2: Learn Some Quick Code

If you know how to run commands in the command prompt, you’ll be a wiz at creating BAT files because it’s the same language. All you’re doing is telling the command promptwhat you want to put in through a file, rather than typing it every time you run thecommand prompt. This saves you time and effort; but it also allows you to put in some logic (like simple loops, conditional statements, etc. that procedural programming is capable of conceptually).
There are SEVEN simple commands I want to familiarize you with for this program. Commands are NOT case sensitive, so don’t worry about that.
TITLE - The Window name for the BAT file.
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. I’d recommend that after you run this test program, you try removing this line from your code to see what happens.
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. If you don’t put this in your program, everything will speed by and end before you can see it. People typically put this in BAT files to give the user a chance to review the material on the screen before continuing.
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.

Step 3: Do Some Quick Logic

We need to plan our program out. Any good programmer will think about the general framework of their program before they dash into things – it prevents them from making logic mistakes that are hard to back out of.
For this program, we want to check the computer’s network/internet settings with an “ipconfig /all” command, and then review that information by giving the user time to read everything. Afterwards, we want to ping google.com to figure out if we really truly haveaccess to the internet. We’ll pause the program after this as well, because we want to know for sure that they saw it.  OK. Very simple program, very simple logic. Let’s write some code.

Step 4: Write Your BAT File

Right click your BAT file and click “edit” to bring up Notepad. The whole document should be blank – ready for some epic programmer input.
Rather than walking you line by line through the code (it’s extremely short) I’m going to use a code comment (example–   CODE  ::Comment) to let you know what we just did.I’m putting the actual code in bold to make things a bit easier to process.
———–Start Code———–
ECHO OFF 
::CMD will no longer show us what command it’s executing(cleaner)
ECHO As a network admin, I’m getting tired of having to type these commands in! Hopefully, this saves me some time in the long run. 
:: 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. 
———–End Code———–

Step 5: Run Your BAT File

Save the file you just coded (or copy/paste mine in, it will run as written), and double click it. Your output should be something like the screenshot below.
how to write a batch file
Congratulations! You’ve written a batch file successfully.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Sample text


Advertisement

 

Copyright 2008 All Rights Reserved saparavur theme by SA PARAVURr Converted into Blogger Template by SA PARAVUR