Search Helium

Home > Computers & Technology > Software > Software & Web Development

Batch file: What it does

by Jason Garland

Created on: January 12, 2009   Last Updated: January 15, 2009

If you were using computers back in the days of MS-DOS, then you probably remember how tedious it was to type a series of commands at the DOS prompt to do something as simple as create a directory in a specific folder or delete a block of files. The batch file anticipated that users would sometimes perform functions requiring multiple sequential DOS commands often enough that they would want to be able to easily compile those commands into a simple, intuitive script.

Well, you can't get more intuitive than a batch file. Requiring nothing more than a plain text editor (Notepad, anyone?), creating a batch file is as simple as listing your DOS commands one after the other in plain text and then saving the file with a .BAT extension rather than the default .TXT extension.

The best way to grasp the potential of batch files is to actually create one. Copy and paste the following into Notepad, save it with a .BAT extension, and then try running it.

ipconfig /all pause

If you run this batch file, what you'll get is a screen displaying technical information for your current network connections. "Ipconfig /all" is the command for viewing your network configuration information, and we've added "pause" to prevent the window from closing prematurely before we've been able to have a look at the information we've requested.

Both commands are DOS commands in this case, but they could also just as easily be utilities or programs that we'd like to run. For example:

cd %SystemRoot% notepad.exe

Run this batch file and it will open up Notepad for you. In this case, the first command changes the directory to the variable "%SystemRoot%", which is a stand-in for whatever directory you installed Windows into. The second command executes "notepad.exe", the Notepad program built-in to Windows.

It's that simple. You can see that if you have some extremely complicated work to do at the command line that bears automation, batch files are invaluable.

As a final example, let me show you a batch file I created to help me keep the Windows Desktop free of clutter on some public kiosk computers I manage.

cd c:\Documents and Settings\%username%\Desktop del *.* /q

The first line moves us into the Desktop directory of the user currently logging on to the machine. The Desktop directory happens to be where all the shortcuts, folder and files that the user has created on his desktop are stored. The second line then deletes everything in that directory, so anything that was created there disappears from the desktop. The "*.*" is understood by DOS as a wildcard for everything in the directory, and the "/q" prevents the "del" command, which deletes objects, from prompting the user for confirmation.

On my Windows server I have attached this batch file to my users' accounts as a logon script, so that everytime a user logs on this batch file cleans off their desktop.

Batch files are a quick, intuitive way to automate DOS command functions that need to to be run often. They've saved me lots of headaches and time, and with a little practice they're sure to do the same for you!

Learn more about this author, Jason Garland.
Click here to send this author comments or questions.

Helium Debate

Cast your vote!

Which is a better Web application framework: Ruby on Rails or PHP?

Click for your side.

130384

Featured Partner

1H2O

1H2O endeavors to create an international network of journalists and media makers with the purpose of generating the most compelling journalism relating to water and human life. 1H2O is a collaboration between the Knight Center for ...more


CONNECT WITH US

Read
our blog
Helum for writers

Write and get published
Share with other writers
Polish your freelancing skills

Join our active writing community
Helium Content Source for Publishers

Quality articles from proven freelancers
Exclusive rights, fast turnaround
Brand engagement, business blogging -- our writers do it all

Get custom content today!

INFORMATION


Helium, Inc.
200 Brickstone Square Andover, MA 01810 USA
#