How Can I Delete All Files Older Than a Specified Date?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’d like to have a script that can search my computer for all files older than a certain date, and then automatically delete those files. Can I do that?

— GM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, GM. 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 November 2, 2003 and then echoes back the name of each file:

strDate = “20031102000000.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 November 2, 2003? After all, November 2, 2003 doesn’t appear anywhere in the script.

Believe it or not, it does; it might not look like a date, but 20031102000000.000000+000 – the value we assign to the variable strDate – represents November 2, 2003. The date looks weird because WMI uses the UTC (Universal Time Coordinate) format. With the UTC format, the first four digits (2003) represent the year; the next two (11) represent the month; and the next two (02) represent the day. In other words, 20031102 is the same as 11/02/2003.

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 November 2, 2003). 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 2003, however, there’s a new WMI object – SWbemDateTime – that allows you to type in a regular old date (like 11/2/2003), and then automatically converts that date to UTC format.

Want to know more about SWbemDateTime? Watch the webcast “Thing the Scripting Guys Never Told You,” from Scripting Week 2.

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 11/2/2003. 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:

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 November 2, 2003? 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 November 2, 2003:

strDate = “20031102000000.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 November 2, 2003:

strDate = “20031102000000.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:

strDate = “20031102000000.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


0 comments

Discussion is closed.

Feedback usabilla icon