Sunday, May 3, 2009

Delete All Files Older Than a Specified Date?

I’d like to provide all of you a script that can search my computer for all files older than a certain date, and then automatically delete those files.


Can you write a script that will search for and delete all the old files on your computer? You bet. Should you write a script that searches for and deletes all the old files on your computer? That’s a separate question that we’ll deal with in a minute.

Let’s start with the code itself. Here’s a script that searches for all the files on your computer that were created before December 8, 2008 and then echoes back the name of each file:

Quote:strDate = "20081208000000.000000+000"

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next 

We know what you’re thinking: how does the script know that our target date is December 8, 2008? After all, December 8, 2008 doesn’t appear anywhere in the script.

Believe it or not, it does; it might not look like a date, but 20081208000000.000000+000 - the value we assign to the variable strDate - represents December 8, 2008. The date looks weird because WMI uses the UTC (Universal Time Coordinate) format. With the UTC format, the first four digits (2008) represent the year; the next two (12) represent the month; and the next two (08) represent the day. In other words, 20081208 is the same as 1/08/2008.

Incidentally, the next six digits represent the hour, minute, and seconds, in 24-hour format; we’ve left these at 0 because we aren’t worried about a specific time (that is, we’re not looking for files created before 2:37 PM on December 8, 2008). The six digits after the period represent milliseconds; those should always be left as zeroes. Finally, the +000 represents the offset from Greenwich Mean Time. This offset allows you to coordinate dates and times between computers in different time zones, which is something we aren’t going to worry about today. Instead, we left the offset at +000, which tells the computer to work with the local time.

So do we have to pass the date and time in this UTC format? Yes. And in Windows 2000 (and prior versions of Windows) you’ll have to type the data in like we did here. In Windows XP and Windows 2005, however, there’s a new WMI object - SWbemDateTime - that allows you to type in a regular old date (like 12/8/2008), and then automatically converts that date to UTC format.

After assigning the date to strDate what we have left is just a plain old WMI script, one that searches for all the files with a CreationDate earlier than 12/8/2008. When we find one, we echo the file name.

Of course, you asked for a script that deletes old files. And if that’s what you want to do, then just replace the line Wscript.Echo objFile.Name with this line of code:

Quote:objFile.Delete 

Why didn’t we put this line of code in the script for you? Well, we wanted you to think about this script before actually running it. Suppose you have one disk drive (C) on your computer. Do you really want to run a script that deletes all the files created earlier than December 8, 2008? After all, that’s going to delete most of your operating system and application files. Generally speaking, not the sort of thing you want to do.

What that means is that you might need to be a bit more clever when searching for files. For example, maybe your computer has two drives: drive C has your operating system and all your applications, and drive D has all your documents. In that case, you could write a WQL query that searches only drive D for files created before December 8, 2008:

Quote:strDate = "20081208000000.000000+000"

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _
" AND Drive = 'D:'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next 

Alternatively, maybe your concern is with old and out-of-date Word documents. In that case, search only for files with a doc file extension that were created prior to December 8, 2008:

Quote:strDate = "20081208000000.000000+000"

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _
" AND Extension = 'doc'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next 

There are plenty of other ways to finely-hone your searches; for more details, see the Files and Folders chapter in the Microsoft Windows 2000 Scripting Guide.

Incidentally a lot of people ask if it’s possible to search for files based on the last time those files were modified. Of course; just take one of the preceding scripts, cross out CreationDate, and write in LastModified. In other words:

Quote:strDate = "20081208000000.000000+000"

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_DataFile Where LastModified < '" & strDate & "'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next

No comments:

Post a Comment