Automating Computer Maintenance

All computer users–especially those who use P2P applications–should take a few steps to keep their computers in good shape. In this article I will outline the steps I’ve taken to automate most of the process using the Scheduled Tasks functionality of Windows. I have observed that many users of Windows don’t know about, or at least don’t take advantage of Scheduled Tasks; if you are unfamiliar with it, it allows one to create jobs to run at scheduled dates/times, and repeat on a schedule if desired.

Introduction to Batch Files

A batch file is a text document with the extension .cmd or .bat (If you are using Windows XP or 2000, .cmd is preferred) that contains a list of commands to be run in a command prompt sequentially. Sound complicated? It’s not really, check out this simple example:

@echo off
title Simple batch file example
echo Hello world!
The first line (@echo off) simply tells the command interpreter to only show the output of the commands–excluding the commands themselves. It’s not necessary, but looks cleaner if you happen to be watching your batch file.

The second line (title Simple batch…), as you may have guessed, sets the title of the command window to the string specified after the title command. This is another superflous command, but makes the script look clean.

The final line simply outputs the text Hello world! to the console.

The same simple format can be used over and over again for scripts to do many repetitive tasks, such as routine maintenance. Let’s take a look at some more useful examples.

 

Disk Cleanup

For this batch file, we’ll be using the cleanmgr tool. Before we create the batch file, we’ll need to tell the program which files we would like to have cleaned. Click the Start Menu, select Run…, and type in cleanmgr /sageset. Choose the items you would like to have removed on your schedule, and push the OK button.

Warning: I do not recommend that you use the ‘Compress Old Files’ option. In fact, I even remove the option from my installations. It takes a significant amount of time to scan for files to compress during this process, and compressing files may slow system performance.

After you have set the parameters for cleanmgr, you can create this batch file, diskcleanup.cmd. (You can, of course, name it whatever you like.)

@echo off
title Disk Cleanup
cleanmgr /sagerun
Additional information about cleanmgr

If that doesn’t remove all the files you would like, you can add commands to remove other files and directories as well. You could add something like this to your batch file:

cd %TEMP%
del /s /f /q *
Note about cd: If you intend to change to a directory on another partition, include the /d switch. (Example: cd /d E:Temp).

Note about del: Be VERY careful using recursive/without-prompt/wildcard delete commands. Check to see that your cd command takes you where you expect to be before allowing the batch file to run any del commands. Use dir in place of del to list the directory entered, rather than deleting its contents, while testing. (The switches and wildcard (*) are not necessary when using the dir command)

%TEMP% is an environment variable that contains the path where many temporary files are stored, but interestingly are not (I don’t believe) removed by cleanmgr. The del command is short for delete. For more information about the switches used, issue del /? in a command prompt.

Put this file in a safe place, and I’ll go over adding a job in Scheduled Tasks later in this document.

 

Disk Error Checking

Checking your disk(s) for errors periodically can help you to predict drive failure or data loss before it happens by scanning for inconsistencies on the disk. Here’s an example disk checking batch file, chkdsk.cmd:

@echo off
title Checking disks for errors
echo Checking C:
echo ————–
chkdsk C:
echo ————–
echo Checking E:
echo ————–
chkdsk E:
echo ————–
pause
This checks two partitions, C: and E:, for errors. But why the pause? Running chkdsk like we did without any switches only runs in read-only mode. In other words: if it finds any errors, it won’t fix them until you run chkdsk /F or /R. Using either of those switches in this batch file would be impractical because the system volume is in use and it would likely be necessary to reboot to do that kind of scan. The pause is there so you can examine the results after the scan has finished. If it mentions any errors you can run it again with different options to attempt to fix the problem(s).

 

Disk Defragmentation

When writing data to a disk, all of the bits aren’t necessarily saved in a contiguous space–they are saved at the most convenient position for the read-write head at the moment. Reading these non-contiguous files is analagous to walking into a room with pieces from many different jigsaw puzzles scattered across the floor and putting one of them together. Disk defragmentation moves data around on the platter so that it is as contiguous as possible. This allows for faster read times, and less stress on the drive.

Many third party disk defragmenting software packages are available (Diskeeper, PerfectDisk, O&O Defrag, more?), and probably outperform the standard Windows defragmenter. They also offer advanced scheduling of defragmentation, but are beyond the scope of this guide. I’ll be using the standard Windows defragmenter in this batch file, defrag.cmd:

@echo off
title Defragmenting Disk(s)
defrag C: /f
defrag E: /f
Note: The Windows defragmenter can defragment only one volume at a time. If you wish to defragment multiple volumes simultaneously, you will need to purchase a third party disk defragmenter.

For users with multiple hard disks: Many P2P applications have the option to configure a folder for temporary files, and another folder to move the files to when completed. If you set these folders to be on different physical disks or partitions, the files will be effectively defragmented when moved to the "Completed" folder. (This is Krell’s tip, but I don’t think he will mind seeing it here 😀 )

 

Setting up jobs in Scheduled Tasks

To access the Scheduled Tasks, open up the Control Panel from the Start Menu and select Scheduled Tasks. (If you are using Category view of the Control Panel, Scheduled Tasks is under Performance and Maintenance). Now double click on Add Scheduled Task. The wizard will guide you through the process of setting up which program to run (select your batch file) and at what time(s) to run it. You will need to create a new task for each batch file you have created.

I recommend running chkdsk and defragmenting monthly, and doing disk cleanup on a weekly basis.

Spyware/Antivirus

The great number of different antivirus and anti-spyware programs make it difficult to include any specific information about automating their usage. You will find that most, if not all, antivirus programs have the ability to automatically update their virus definitions and perform a scheduled virus scan. Some anti-spyware clients may also have this functionality.

"Automating Computer Maintenance" was written for Zeropaid.com by cpugeniusmv. Last modified on May 25, 2005.