Login with Facebook

Monday, March 28, 2011

OMW: List files on folder that didn’t modified within 30 minutes Part 2

 

nodeName = "."
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate( DateAdd("n",-30,now()))
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & nodeName & "\root\cimv2")
Set colFiles = _
objWMIService.ExecQuery("Select * From CIM_DataFile Where Drive = 'C:' and Path = '\\Scripts\\' ")
'Wscript.Echo colFiles.Count 'echo number of files
For Each objFile in colFiles
IF objFile.LastModified < dateTime.Value Then
strNewFile = objFile.FileName & "." & objFile.Extension & " File last modified : " & objFile.LastModified
Wscript.Echo strNewFile
END IF
next


This script is for displaying files that are last modified date is older than 30 minutes


Here is the script breakdown with explanation


I tried to get the current time using Now() function then I deducted the time by using DateAdd() and used a native value


I called the files using WMI class  CIM_DataFile and I defined the file location through Where clause to filter the files that are located in drive C: and located under folder named \Scripts


objWMIService.ExecQuery("Select * From CIM_DataFile Where Drive = 'C:' and Path = '\\Scripts\\' ")


The problem that the time stored in WMI object is different that the time value used in VB script function like now() like the below


VB Time is 03/28/2011 9:42:03 PM


WMI Time is 20110328214201.225172+240


I have used WbemScripting.SWbemDateTime class to convert the date to WMI date


Set dateTime = CreateObject("WbemScripting.SWbemDateTime")

Then I converted the value received using a function named SetVarDate()


dateTime.SetVarDate( DateAdd("n",-30,now()))


I tried to compare it within the WMI query and failed so I compare it using if statement


If objFile.LastModified < dateTime.Value then
Wscript.Echo strNewFile

No comments:

Post a Comment