04 February 2008

Learn to Write .BAT Files

Batch files (and .cmd files) are very useful for automating small tasks on Windows. If you know how to use command line arguments, then this should be simple. Otherwise, it's fairly easy to learn too. This is a small run through of some of the useful command line arguments you can string together and make into .bat (batch) files. Automate your cleanup and defragmenting or a whole bunch of other things.

So what exactly are .bat files? Batch files are basically command line arguments that you've entered into a text file so that they will run all at once. It's really useful when you want to click on one file in order to open up several things.

For people that are using Linux, this is basically shell scripting. If you have any other needs just type 'help' in command prompt to see all these commands and their usages. When you type 'help', it will tell you all of the basic usages, I'm gonna assume you know the basics stuff and how to use help to review the basic stuff. Let's have a look at the stuff 'help' doesn't show.

Quick tip before we begin: I'm not going to go over a lot of the extra features of these programs. Whenever you encount some command line utility you don't know how to use, just use 'utility /?' where 'utility' is the name of the program. This tells you how to use that program. Usages are usually in the form 'utility [/a][/b][/c]...drive:/path'. The things around '[]' are parameters. If you would like to use that feature, just type that specific letter preceded by a slash ('utility /a'). The other things like 'drive' should be self-explanatory and all other information about the parameters are explained in the usage as well.

Task Managing: In Command Line!
There's two very useful command line tools that work a bit better than task manager. There's are a lot of annoying programs that can sometimes hide themselves from Task Manager so it's kind of hard to stop them from running. These two utilities help you handle these tasks:

TASKLIST is used to list out all the programs that are running on your computer. TASKLIST shows EVERYTHING. There a lot of parameters for this but I've mainly used it without anything special. You can view the processes in different ways (table, list, etc) by using these parameters (type 'tasklist /?' for usage).

TASKKILL is used to ..well.. kill a task/process. This is just a quick way of killing a process. Let's go back to the situation where you can't see a program using Task Manager. Then find it using 'tasklist' and then end it using 'taskkill' if you need to. There's only two parameters I've ever used with this. '/f' forces the process to end (doesn't prompt you). '/im' allows you to specify the "image" name. Just type 'taskkill /f /im process' replacing "process" with your program name to end it.

START is used to start another command line window. I was about to leave this one out but it can come in quite handy if known how to use properly. You can look at the usage but the only things I use are the '/min' and '/wait' commands. In command line, you can type in the name of a file (including an executible) and it will open it with the default program. Using 'start' along with this can be used to open several inter-connected programs with one click. The '/min' starts a new window minimized and the '/wait' waits for the original parent window to wait until this new window closes before moving on.

File Management: Spring Cleaning and Such!
So maybe you know how to use Disk Defragmentor and Disk Cleanup (if you don't, you should learn - the two basically clean your hard drive so it runs faster). If you ever want a simple shortcut so you can double-click it and have them run right before you sleep, here's one way to do it.

DEFRAG is used to defragment your hard disk from command line. The usage is 'defrag <volume> [/a][/f][/v][/?]'. So type. Let's defrag the 'C:' with visual (verbose) output. Then type 'defrag C: /v'. Let's say you just want to analyze it. Then just do 'defrag C: /a'. '/f' is to force the defragmentation even if you have low hard disk space which you shouldn't do.

CLEANMGR is used to run Disk Cleanup from command line. The problem with this utility is that you can't view help info for this utility. So I'll tell you what you can do. The usage is 'cleanmgr [/d diskletter][/sageset:n][/sagerun:n]'. This is the most troublesome utility. You can clean ONE drive with the '/d' command but it opens up the actual Disk Cleanup utility window.

Or you can setup a customized clean method (pick what you want to clean up) using the '/sageset:n' where 'n' is any number from 0 to 65535 (example is 'defrag /sageset:123'. This also opens up a window. The last is '/sagerun:n' which runs one of your customized cleaning setting on ALL the drives. What this utility can't do is clean one drive with custom settings.

Batch File Creation
Alright, so with some of these utilities under your belt, here are some quick .bat files I often use.

The first one I use to cleanup and defrag my hard disk right before I go to sleep. Here it is:

  1. Use 'cleanmgr /sageset:n' to set some cleaning settings. I usually choose to clean Temporary Internet Files, Temporary Setup Files, Recycle Bin, Temporary Files, and Temporary Offline Files.
  2. Now have the following in a .bat file with a name of your choosing. Remember to use the same number 'n' you chose above, below as well.

cleanmgr /sagerun:n
defrag C: /v >> defrag.log


What the '>> defrag.log' does is it creates (or appends to) a file called 'defrag.log' whereever you run this batch file from which logs all of the verbose output from defragmenting.

Thunderbird and YPops
The next file is for those of you who use Mozilla Thunderbird and YPops!. This might be a small audience but I find it very useful. For those of you who don't know, the only way you can access a Yahoo account from any email manager such as Thunderbird is using YPops. YPops needs to be running before you run Thunderbird though but I hate processes that waste memory or CPU power when they are not used. So instead of having YPops running all the time, I do this:

@echo off
start "" "E:\Program Files\YPOPs\ypops.exe"
start "" /wait "E:\Program Files\Mozilla Thunderbird\thunderbird.exe"
taskkill /f /im ypops.exe

The first line disables anything from showing in the command window when you run this (if it's not needed, you don't need to see it).

The second line starts up YPops.

The third line starts up Thunderbird and we also tell command line to wait for Thunderbird to close before going on to the next command.

The last line kills YPops because we're done with Thunderbird and we don't need YPops anymore!

Although you may have command line looming in the background while you run this program, you shouldn't be bothered by this since you're using Thunderbird in the front anyways. Close Thunderbird and the command line window goes away too!