Windows Batch Files

By Rossi Fernandes Published Date
01 - Mar - 2008
| Last Updated
01 - Mar - 2008
Windows Batch Files

Automate some mundane tasks in Windows using batch files

Batch scripts were an easy way to make tiny software necessary for drivers back in the days of DOS. DOS batch files can still be run in the same way in the Windows operating system through the command prompt. A few of the commands present in the DOS days don’t exist anymore, but most are still intact. Batch files have the extension .BAT, and any text editor can be used to create them.

Automate Your Net Connection

A lot of us use the dial-up method to connect to our ISPs—yes even the broadband users amongst us usually have to use PPPoE (dial-up broadband). The problem occurs often enough to be irritating—connections drop, for whatever reason, and downloads fail, transfers are know what we mean. To automate the redialling process, make sure you have the account set up by going to Start > Control Panel > Network Connections. Open Notepad, and type in the following:

RASDIAL <Connection Name> <username> <password>

You have to be careful to ensure that you type the connection name exactly as it shows in Network Connections. Now just hit [Ctrl] [S], and save the file as <filename>.bat—here filename is whatever you want to call it. Now you can add a scheduled task to set the connection to dial automatically. Go to Start > Control Panel > Scheduled Tasks > Add Scheduled Task. Click Next and then click Browse and select the BAT file you just created.

Next, set the schedule for the application, so, say, you want to run the BAT file every day, after every half hour, just choose Daily and click Next. Fill in other details such as starting time, entering your user account information, and completing the schedule creation. To make this a repetitive schedule, right-click on the task, select Properties, click on the Scheduled tab and then on the Advanced button. Check the Repeat Task checkbox, and enter a value for the interval. You can be sure that your PC will, more or less, stay connected as long as it’s on.

Start Your Favourite Programs

As soon as you walk into office, you may launch Outlook to get your mail, open IE to start browsing and perhaps launch your RSS reader. Designers may start work by launching another set of programs. Whatever your job, you’re bound to have a few rituals like these, and instead of going through the motions every day, you can let a Batch file do all the work for you.

Start Notepad and create entries (as shown below) for all the applications you want to run one after the other.

CD “C:\Program Files\ wings3d_0.99.00b”
Start Wings3D.exe
CD “C:\Program Files\ Adobe\Adobe Photoshop CS3”
Start Photoshop.exe

CD is used to switch control to the directory of the application and the Start command runs the application itself. Once again, save the file with a .bat extension, and all you have to do is double-click this file when you want to start working.

Start And Stop Services On Demand

Not all of us are blessed with fast computers, and even if you are, you might not like too many services running in the background. Normally, you could go to Start > Run and type net stop <Service> to stop services individually, but if you want, say, 10 services stopped when you’re about to begin gaming, this would be painful. With a Batch file, all you would need to do is create one that contains all the net stop commands in the following format:
NET STOP <Service 1>
NET STOP <Service 2>
...and so on

This way, you can add all the services you want stopped, and just double-clicking the Batch file will stop them all. You can even do the reverse, and use the net start command to restart the same services using another Batch file. To find the exact names of the services, go to Start > Control Panel > Administrative Tools > Services; double-click a service and note down the name given under Service Name.

Killing Tasks

Tasks can be shut in the same way services are stopped. The Taskkill command can forcibly shut applications from the command line. This can be used in a batch file to shutdown multiple applications together. This is especially useful in case you’re usually goofing off and the boss walks in! In the batch file, create entries in this format: Taskkill /im Processname.exe /f.

Quick Shutdown Or Reboot

There are commands that can be used to shutdown the computer and these can be customised as well. The command is shutdown -s -t 0. Create a shortcut with this command and you can shutdown your PC without having to go to Start > Shut Down.

Creating shortcuts for the shutdown command is a quick way to shutdown and restart your PC

This will force the PC to shutdown immediately without any confirmation dialog boxes. To set a time before the shutdown commands replace the value 0 with anything else. You can use this same command along with schedules to automatically shut down the computer. At any time if you want to abort the shutdown, use the command shutdown -a.

Similarly, the command for rebooting the computer without any alerts or confirmation windows is shutdown -r -t 0.

Rossi FernandesRossi Fernandes