Automatic file maintenance under XP?
March 25, 2006 6:03 PM   Subscribe

I'm looking for some software to be run under Windows XP that I can set to move or remove files which are past a certain date after specified intervals. Ideally the software will have a small footprint and relatively easy to use. Is this the sort of task that can be done through some sort of Windows based CRON program? Ahhhh help me Mefi!
posted by a. to Computers & Internet (12 answers total)
 
past a certain date after specified intervals.

I don't get it. Files that are older than a certain interval period, like in a temp directory, or files that are set to delete at a certain datetime, like cookies, or all the files created or modified during a certain interval, like a system restore, or what?
posted by ChasFile at 6:38 PM on March 25, 2006


The Windows equivalent of cron is AT, right?

According to this helpful page it's got a nice interface/GUI controller called Windows Task Scheduler.

So, all you need to do now is write a batch file which accomplishes your file deletion task, which is a bit obscure at the moment.
posted by AmbroseChapel at 6:44 PM on March 25, 2006


Response by poster: Yup Chas, like in a temp directory. Life would be so much easier if you didn't need to manually clear it out yourself. I had a quick look at the Windows Task Scheduler, but i knew i would end up needing to use a batch file. Its been so long since i've played with them, and other than being a little messy with a command window opening in the middle of working i now wouldnt know how to script this task using them anyway.
posted by a. at 7:07 PM on March 25, 2006


If you install Cygwin you will have access to a multitude of standard Unix utilities from Windows, including the invaluable find. Find is a general utility to traverse file systems, pick out files that meet criteria you specify (such as "created more than 7 days ago") and optionally perform operations on them (such as "delete" or "copy to backup directory").

For example,
find ./ -ctime +7 -exec rm -f {} \; -ls
This should print the names of and delete all files within or under the root directory created more than 7 days ago. Cygwin also includes a cron utility so it would be rather straightforward to accomplish your goal this way if I understand your question correctly. (Be aware that you should be very sure that the logic embodied in your predicates is correct when executing rm -f in an automated fashion :)
posted by harmfulray at 7:10 PM on March 25, 2006


Oops, I meant "under the current directory" and not "under the root directory" for that example.
posted by harmfulray at 7:12 PM on March 25, 2006


Best answer: Argh- another one of these "install Cygwin!" answers. People, people, people- Windows has plenty of batchin' capabilites if you only learn how! It seems that every single "I'm trying to batch something in windows" question has at least one "install a package of utilies, so you can do it the Unix way!" answer.

When I've dealt with this problem in the past (for example, auto-cleaning web log directories of anything older than 30 days) I've most commonly used forfiles.exe, which comes standard on some of the MS OSes although I don't think it's there on XP. However, it can be found in the (free) reskits, or easier if you just want the one file instead of a big install package you can search around for a copy on the net and dump it in your c:\windows\system32\ directory. If you know anyone with a copy of Windows 2003 you can get it from them, too.

Forfiles.exe command/batch file
Once you have a copy of forfiles.exe you write a simple batch file- pretty much one line:
forfiles /P PATHTOFOLDER /S /D -30 /M *.* /C "cmd /C del @path /Q"
Where PATHTOFOLDER is the full path to the directory in question (c:\windows\temp in your case?), the /S means "... and all subdirectories" if desirable, and the -30 after /D is "older than 30 days"; you can change to -10 if you want 10 days, etc. /M is file mask (*.* for all files) and /C is the command you'd run on any file matching these parameters, in this case "delete" with the /Q switch to avoid prompting.

Also, in some cases you may want it to get rid of folders as well, in which case you can add a second command to use /C "cmd /C rd @path /Q" at the end instead of the del @path /Q, which should quietly error out on non-folders but otherwise remove old folders after the files have been cleaned up. That might make your batch file look like this:
forfiles /P PATHTOFOLDER /D -30 /M *.* /C "cmd /C del @path /Q"
forfiles /P PATHTOFOLDER /S /D -30 /M *.* /C "cmd /C rd @path /Q"
Try this on a copy of a folder first to test it out... I can't stress this enough, copy some folder to c:\TEST or something, and try all your batch jobs out there as the PATHTOFOLDER, so you're sure you've done the syntax as I have above and it works... before you point it at any actual directory for the love of god and all that is holy.

Schedule in AT or task manager
Save this command line in a simple text CleanTemp.bat file using notepad- you can repeat the line multiple times in the CleanTemp.bat file if you want to do this with more than one folder as a target, or using the "remove directory" example I mentioned above. Every time you run it, it'll execute those commands in the background. Then have that scheduled to run via task scheduler and it's GUI, or the AT command at a command propmt, whichever is simpler for you. Ex:
at 9:00a /every:Su,M,T,W,Th,F,S c:\scripts\CleanTemp.bat
where c:\scripts\cleantemp.bat is the full path to the batch file. This will run it automatically at 9am every single day. You can type AT at the command line with no arguments to see any existing schedule jobs. Adjust to taste, add seasoning.
posted by hincandenza at 11:01 PM on March 25, 2006


Response by poster: Ahhhh I think that looks more like what I'm after hincandenza! the cygwin tip was close... but I must admit that I'm glad I don't need to go there.

The purpose of the question was for a folder on the desktop I'm calling "Transitory" to save files to when I know that after a while i wont need them like setup files, formatted images that are being uploaded etc. The chances are that if I havn't grabbed the files out of that directory after 30 days, i won't be needing them... and its crazy to manually sort through the directory when this task is better suited to the computer.
posted by a. at 11:37 PM on March 25, 2006


..."manually"?
posted by cps at 11:47 PM on March 25, 2006


Response by poster: manually.
posted by a. at 11:57 PM on March 25, 2006


I understand the word, but wasn't sure what you mean by it here. In case you were manually going through the list to find each one older than 30 days:

You can sort by date by clicking the Date Modified column header in Explorer (in detail view, View->Details). This will sort the files from oldest to newest. Select the block of oldest files by either a) clicking the first one and shift-clicking the last one you want to delete or b) dragging a box around them; then hit Delete.
posted by cps at 11:50 PM on March 26, 2006


Response by poster: Wouldn't be so much better if the computer did it itself? What I'm trying to do is make a folder with a 30 day file buffer, and if I were to do all that myself I'd no doubt give up especially since its really unnecessary to process it manually.
posted by a. at 12:11 AM on March 27, 2006


Agreed, the automatic self-cleansing folder is neater. But every now and then going in a folder and deleting the top X oldest files doesn't sound "crazy" to me, unless you were picking the files one by one.

Any case, you've got a good solution above, so all's well that ends well.
posted by cps at 2:05 AM on March 27, 2006


« Older Good magazine. Bad typeface.   |   Simple Bathroom Repair Newer »
This thread is closed to new comments.