If I let it, my hard drive would fill to capacity with crap I don't need. Throughout the course of one day I get my paws on all sorts of throwaway files: video, images and songs meant for a single viewing or listen, PDF's I have to print, software installers and big ol' zip files I extract and do whatever I need to with the contents. The end result is a bunch of stuff hogging space on my hard drive for no good reason.

I'm lazy and I don't want to have to clean up after myself every time I work with a set of files. Instead, I've scheduled a cleanup script that sweeps through my hard drive every evening while I sleep. My virtual janitor deletes any temporary file that's been sitting around for more than x days, like old garbage starting to stink. This way space on my hard drive is constantly recovered, and I don't have to worry about getting the dreaded "Low disk space" message at the critical moment I'm about to conceive my opus. Because you know if you were going to run out of disk space, that's when it would happen.

Today I present that janitor script and its correct usage, so you too can automatically empty out the old stuff from your virtual junk drawer and recover disk space you need to get things done - and not have to think about it.

Advertisement

Note: This script and the instructions for use are for Windows PC's only, and require comfort with executable script editing and automated file deletion, which are not to be taken lightly. Sorry, newbies, Mac* and Unix folks. I owe you one.

To get this train rolling, first we have to create our virtual junk drawer. Then we have to configure our janitor to clean out the files older than a number of days you determine. Finally, we'll schedule the janitor to clean up at a regular, convenient time.

Let's get started.

Create a directory in your main documents directory which is going to serve as a holding pen for temporary, disposable files. You hard core folks can call it temp. Mine is in C:\Gina\junkdrawer\, so that's how I'll refer to it for the rest of this article.

Set your web browser and e-mail client to save downloaded files and message attachments in your junkdrawer. Firefox users, in the Tools menu, Options, click on Downloads. Set the directory Firefox should download to automatically there, like so. (Click to enlarge.)

Note: Because my VB script skills are small and weak, the most excellent and generous Lifehacker reader Brian Plexico lent his scripting-fu and put this bad boy together. Brian - Lifehacker readers, my C: drive and I all thank you.

Using a plain text editor like NotePad or EditPlus, open up the Janitor.vbs file. Edit the following sections to set the path to your junk drawer directory, and how many days old files should be when they get deleted.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'ENTER THE PATH THAT CONTAINS THE FILES YOU WANT TO CLEAN UP
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Path to the root directory that you're cleaning up
PathToClean = "C:\Gina\junkdrawer"
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'ENTER THE NUMBER OF DAYS SINCE THE FILE WAS LAST MODIFIED
'
'ANY FILE WITH A DATE LAST MODIFIED THAT IS GREATER OR EQUAL TO
'THIS NUMBER WILL BE DELETED.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Specify the how many days old a file must be in order to be deleted.
numberOfDays = 7
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

So you'll see above, in the line that begins PathToClean, replace C:\Gina\junkdrawer with the path to your junk drawer. Also, change the line numberOfDays to your preferred time frame. I'm good with deleting stuff 7 days old, but you might want to start with a month (30) or two weeks (14.)

Next we're going to test the script, but before we go there, for the love of all that is holy, read this first. Don't make me use the blink tag.

BIG HONKING MUST-READ WARNING NOTE!!!

This script deletes files from your hard drive, from any directory that you specify. Which means if you specify, oh, I don't know, C:\Windows\, you will be screwed. Meaning, you could do irreparable damage to your computer for which neither I nor Brian will be held accountable. So! Make sure the PathToClean is set to a directory with stuff in it Windows doesn't need to function, and stuff you don't care about losing.

Also, this script deletes files x days older than the files's "Last Modified" date. It is possible that a file's Last Modified date is already older than the number of days you specify, even if you've just downloaded the file. Keep this in mind when you're saving files to your junk drawer. Only save stuff in your junk drawer which you're ok losing.

Ok, now that's out of the way.

Once your junk drawer is set up and the script is configured, you can double-click Janitor.vbs to make it go. It will check your junk drawer and all its subdirectories for old files and delete them if they exist. Empty subdirectories will be deleted as well. If all goes right, you will see a popup box that reads, "The directory has been cleaned up!" like so:

Also, many viruses and spyware use VB, the language this script is written in, to do bad things like spam your friends and steal your identity. This doesn't mean VB is inherently bad, it means it's oft been used for evil. As a result, many spyware scanners and virus protection programs will sound off all sorts of alarms and even disable VB entirely in the name of protecting you. I personally guarantee that this script is benign because it's been running on my machine for months and I've personally verified the source and it does nothing harmful except delete files from a directory you specify. Whitelist and allow it to execute no matter what your virus protection and spyware cleaner says; it's kosher.

Once your script is pointing to the right place and you've run it and nothing's blown up, you want to start accumulating disposable files in your junk drawer and having them cleaned out every day.

In Control Panel, go to Scheduled Tasks. In the File menu, choose New, and name your new task "Clean out junk drawer." Double-click on the task, and set the Run: line to where your script lives, like this:

Click on the Set Password button and enter your Windows password. This means you authorize this script to run.

Then, on the Schedule tab, set the job to run each day at a convenient time. On my home computer which I leave on all night, the script runs at 3AM each morning. Perhaps lunch time will work for you. If you have lunch at different times each day, you can set the "Only start this task if the computer has been idle at least" settings so that the deletion doesn't run when you're in the middle of something.

To test your new task, right-click on it in Task Manager and choose Run. You should get the "The directory has been cleaned up!" message again.

And that's that! Your virtual janitor is set to clean up after you. Remember, the key to this system is to move files out of your junk drawer if you'll need them long term. Otherwise, they will be irrecoverably lost. That's why the junk drawer works best for email attachments and web browser downloads, which are disposable since presumably there's a copy online which you can re-download any time.

Enjoy putting your personal hard drive janitor to work!

* Mac note: I'm sure that this would be a trivial programming task to replicate in AppleScript or with Automator. Mac programmer types, if you know of an existing script that does this or are willing to write one, let me know at tips at lifehacker.com.

** Credit where credit's due: The concept of the janitor script is entirely Danny O'Brien's, who, coincidentally, started this whole life hacks movement to begin with. Read more of Danny's take on hacks, computing and productivity in this Lifehacker interview from back in March.