Timesavers: Batch Files

Monday, April 13, 2009

This is part of an ongoing series on Timesavers. The goal is simple: short, easily-digestible posts designed to help developers get faster and more productive

A batch file on Windows is a .bat file that contains one or more commands. You can execute .bat files by double-clicking, via the command prompt, or even as an "executable" in a scheduled task. Their power as a timesaver is in their ability to encapsulate common functionality so that you don't have to click a lot or remember a lot. This is predicated on the idea that every mouse movement/click is itself a time waster.

Common Example How do you start/stop/cycle ColdFusion? Certainly one way to do it is through the Windows Services panel. Let's count the mouse movements:

  1. Start Menu
  2. Control Panel
  3. Administrative Tools
  4. Services
  5. Scroll to Macromedia JRun CFusion Server
  6. Click the "Stop" or "Start" button up top

And that's just for one action. Multiply that by the other services you frequently cycle. SQLServer? MySQL?

Bat Files for Scenario Above


NET STOP "Macromedia JRun CFusion Server" NET STOP "Macromedia JRun Admin Server"


NET START "Macromedia JRun CFusion Server" NET START "Macromedia JRun Admin Server"


NET STOP "Macromedia JRun Admin Server"

NET STOP "Macromedia JRun CFusion Server"

NET START "Macromedia JRun Admin Server" NET START "Macromedia JRun CFusion Server"

Where to store them? Obviously, you don't want to go putting dozens of bat files on your desktop. So where to put them? I keep mine in a directory, and then add a shortcut to that directory into my windows taskbar by following these steps:

  1. Right click in taskbar
  2. select "Toolbars"
  3. select "New Toolbar"
  4. Navigate to the directory where your bat files live. Click "OK" through to the end.

From there, you can access your bat files from that new toolbar right from your windows taskbar. The nice thing about this approach is that you don't need to double-click on the file... just single click. Here's a screenshot of what it might look like:

Isn't there a better way?

Yup.... Stay tuned for the next Timesaver!


Jon said...

Uhm - not trying to be negative here, but don't you have each one of the bat files mixed up a little bit?
The stop script contains start commands, the start script contains the stop commands, and the cycle only stops the cfusion and starts the admin.
If I am missing something please let me know. Thanks for the great ideas though!

Marc Esher said...

GAH! Thanks dude. Fixed.

Eric Cobb said...

I've had my batch files set up for a while, but I didn't know about the toolbar shortcut. That's awesome!

Marc Esher said...

It gets even awesomer. I'll have the follow-up to this in a day or two.

Aaron Longnion said...

Cool, I've been meaning to set up some batch files for this. Keep the tips comin'

Anonymous said...

I'm trying listing a directory, but i would want in one row. could any give me a hand?.
dir c:\ /ad /b /-p /o:gen

Thanks in advance