Enumerating Folders by Date

Microsoft® Windows® 2000 Scripting Guide

Folder date properties - CreationDate, LastAccessed, and LastModified - are very useful in managing folders. For example, you might use the CreationDate property to return a list of all the folders created in the past week. Or, you might use the LastAccessed property to identify rarely used folders, folders that might be candidates for compression or for removal from the file system.

The primary complication in working with folder dates is the fact that WMI stores date and time information using the UTC (Universal Time Coordinate) format. In this format, dates are displayed as yyyymmddHHMMSS.xxxxxx±UUU, where:

  • yyyy represents the year.

  • mm represents the month.

  • dd represents the day.

  • HH represents the hour (in 24-hour format).

  • MM represents the minutes.

  • SS represents the seconds.

  • xxxxxx represents the milliseconds.

  • ±UUU represents the difference, in minutes, between the current time zone and Greenwich mean time.

For example, in UTC format, a folder created on October 18, 2002, at 10:45:39 A.M. Pacific Standard Time returns this CreationDate value:

20021018104539.000000-480

This means that you cannot specify a standard date, such as 10/18/2002, in your search query; WMI will not be able to interpret this date. Instead, you will need to convert any dates used in your queries to UTC format. Your converted date should use the items in Table 11.4 in each character position in the 25-character UTC string.

Table 11.4 Converting a Date to UTC Format

Character Positions

Description

Sample Value

1-4

Four digits representing the year (such as 2001 or 2002).

2002

5-6

Two digits representing the month. For example, January is represented by the digits 01; November by the digits 11.

10

7-8

Two digits representing the day of the month. For example, the 5th day is represented by the digits 05; the 23rd day by the digits 23.

18

9-14

Six zeros representing the hours, minutes, and seconds of the day (in 24-hour format). If you prefer, you can specify values other than zero to create more finely-targeted searches. For example, to search for folders created after 1:47 P.M. on a given day, set these characters to 134700, where 13 represents the hours (1:00 P.M. in 24-hour format), 47 represents the minutes, and 00 represents the seconds.

000000

15

A period (.).

.

16-21

Six zeros representing the milliseconds.

000000

22-25

The number of minutes difference between your local time and Greenwich mean time.

-480

You can determine the offset from Greenwich mean time by using this script:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colTimeZone = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_TimeZone")
For Each objTimeZone in colTimeZone
    Wscript.Echo "Offset: "& objTimeZone.Bias   
Next

To search for folders using the date October 18, 2002, you would use a value similar to this (depending on your time zone):

20021018000000.000000-480

Scripting Steps

Listing 11.5 contains a script that returns a list of all the folders on a computer that have been created since March 1, 2002. To carry out this task, the script must perform the following steps:

  1. Create a variable named dtmTargetDate and set it to the value "20020301000000.000000-420." This value, which represents the target date, March 1, 2002, can be parsed as follows:

    • 2002 -Year.

    • 03 -Month (in two-digit format).

    • 01 -Day (in two-digit format).

    • 000000 -Hours, minutes, and seconds of the day.

    • .000000 -Milliseconds.

    • -480 -Offset (in minutes) from Greenwich mean time.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Directory class.

    To limit data retrieval to a specified set of folders, a Where clause is included restricting the returned folders to those with a CreationDate later than the target date of March 1, 2002.

  4. For each folder in the collection, echo the folder name.

Listing 11.5 Enumerating Folders Using Dates

  
1
2
3
4
5
6
7
8
9
10
dtmTargetDate = "20020301000000.000000-420"
strComputer = "."
Set objWMIService = GetObject _
 ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE CreationDate > '" & _
 dtmtargetDate & "'")
For Each objFolder in colFolders
 Wscript.Echo objFolder.Name
Next