December 12th, 2007

Hey, Scripting Guy! How Can I Delete Files That Are a Specified Number of Hours Old?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’ve seen scripts that let you delete all the files in a folder that are X number of days old. I have a different problem: I’d like to delete files that are X number of hours, or even X number of minutes, old. How can I do that?

— DF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DF. Did you see where some researchers are predicting that, by the year 2050, humans and robots might actually marry one another? In fact, outside of possible ethical concerns, these researchers see absolutely no reason why such marriages won’t take place by the year 2050, at the latest.

Although he’s hardly an expert in robotics or artificial intelligence, the Scripting Guy who writes this column is a bit more skeptical. After all, look at the state of robotics today. For example, robots might be good at doing one thing (spot-welding an automobile on the assembly line; vacuuming a living room) but they’re still more idiot savant than jack-of-all-trades. Robots aren’t very good at conversation; robots pay no attention to your feelings; robots are highly unlikely to give you a little something – not even a card – on your birthday. Robots can’t fend for themselves; you have to do pretty much everything for them. Robots –

You know, come to think of it, for the typical female, transitioning from the typical male to a robot probably won’t be that big of a deal, will it?

Interestingly enough, researchers are not predicting that, by the year 2050, robots will be writing the Hey, Scripting Guy! column. Many would say that’s because some tasks are too trivial and too mindless even for robots. However, we like to believe that’s because that no robot could ever write a script that, like this one, deletes all the files in the folder C:\Scripts that are more than 8 hours old:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFiles = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Scripts’} Where ” _ & “ResultClass = CIM_DataFile”)

strCurrentDate = Now

For Each objFile In colFiles strFileDate = WMIDateStringToDate(objFile.CreationDate) intHours = DateDiff(“h”, strFileDate, strCurrentDate) If intHours >= 8 Then Wscript.Echo objFile.Delete End If Next

Function WMIDateStringToDate(dtmInstallDate) WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & “/” & _ Mid(dtmInstallDate, 7, 2) & “/” & Left(dtmInstallDate, 4) _ & ” ” & Mid (dtmInstallDate, 9, 2) & “:” & _ Mid(dtmInstallDate, 11, 2) & “:” & Mid(dtmInstallDate, _ 13, 2)) End Function

OK, let’s see if we can figure out how this baby works. (And no, we don’t need any help from any of the robots reading this column. Although we apparently need help mixing a Tequila Sunrise, we humans can still explain our own scripts, thank you very much.)

As you can see (unless you’re a robot, in which case your laser vision probably caught the computer screen on fire), we start out by connecting to the WMI service on the local computer. Just a moment; it looks like we question. You in the back, the one with the metallic voice. Can you run this script against a remote computer? You bet you can; all you have to do is assign the name of that remote computer to the variable strComputer, like so:

strComputer = “.”

Note. No, we don’t have the remote computer’s IP address. You’ll have to ask her for that yourself.

After connecting to the WMI service we then use this line of code, and an Associators Of query, to retrieve a collection of all the files (instances of the CIM_DataFile class) found in the folder C:\Scripts. Once we have the collection in hand, we then assign the current date and time to a variable named strCurrentDate. That’s what this line of code is for:

strCurrentDate = Now

At this point, we’re ready to go and delete some files.

To do that, our first step is to set up a For Each loop that loops through all the files in the folder C:\Scripts. Inside that loop, the first thing we do is execute this line of code:

strFileDate = WMIDateStringToDate(objFile.CreationDate)

What’s going on here? Well, what going on here is that we’recalling a function named WMIDateStringToDate, passing the file’s CreationDate property as the function parameter. WMIDateStringToDate is going to take the CreationDate, perform a little formatting magi on that date, then store the reformatted value in a variable named strFileDate.

Why do we go through all those gyrations? Well, as you know (and as even most robots know) WMI stores date-time values in the UTC (Universal Time Coordinate) format. Is that a problem? You bet it is; that means dates and times look like this:

20071211094327.000000+420

As you might expect, date arithmetic is none-too-easy when you have dates and times that look like that. Our function is designed to do something about that.

We aren’t going to talk about UTC dates and times today, nor are we going to explain the function WMIDateStringToDate function in any detail; if you’d like more information on either of those topics, see the Microsoft Windows 200 Scripting Guide. Instead, we’ll simply note that the individual pieces of a typical date are actually stored at predefined locations within a UTC value; for example, the first four digits (2007) represent the year, the two digits after that represent the month; and so on. What our function is doing is simply taking a UTC value (the CreationDate property) and constructing a more readable (OK, OK: more human readable) date from those various pieces. The net result? The variable strFileDate gets assigned a value like this:

12/11/2007 09:43.27

That’s a value that humans find much easier to deal with. More importantly, it’s also a value that VBScript finds easier to deal with.

Speaking of which, as soon as we get the reformatted date-time value back from the function we hand that value off to VBScript and its DateDiff function:

intHours = DateDiff(“h”, strFileDate, strCurrentDate) 

What DateDiff does is determine the amount of time that has (or that will) elapse between two dates. As you can see, we pass three parameters when we call DateDiff:

“h”. This represents the time interval we want to get back. We want to determine the age of a file in hours; hence we pass the value “h”. To get the age in minutes we’d use this value: “n”. For a complete list of parameters available to the DateDiff function see – you guessed it: see the Microsoft Windows 2000 Scripting Guide.

strFileDate. The date and time that the file was created.

strCurrentDate. The current date and time. (Or at least the current date and time when the script first started.)

As we noted, DateDiff will calculate the age of the file in hours, then store that value in the variable intHours. We then use this line of code to determine if the file is more than 8 hours old:

If intHours >= 8 Then

Note. OK, technically we check to see if each file is greater than or equal to 8. Why? Because the DateDiff function returns integer values only, and constructs those integers by removing any decimal points (not by rounding up or down). For example, suppose a file is 8.5 hours (8 hours and 30 minutes old). In that case, DateDiff will report that the file is 8 hours old; because 8 isn’t greater than 8, this file wouldn’t be deleted. Using the greater than or equal to operator takes care of that problem.

If the file is older than 8 hours we then call the Delete method and delete the file. If it isn’t, then we simply zip back to the top of the loop and repeat the process with the next file in the collection. By the time we’re done the only files left in C:\Scripts will be those files less than 8 hours old. That’s all we have to do.

In case you’re wondering, as part of his research the Scripting Guy who writes this column took a quick tour around the Internet looking for information about robots. When he did so, he stumbled upon Carebot 3.4. One of Carebot’s primary purposes in life is to follow you around everywhere you go, verbally reminding you of all the things you’re supposed to be doing but apparently aren’t doing at that very moment.

You know, come to think of it, for the typical male, transitioning from the typical female to a robot probably won’t be that big of a deal, will it?

Author

0 comments

Discussion are closed.