Compair Date returned from DateLastModified

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 16 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sburke@orrick.com
Posts: 17
Last visit: Thu Sep 13, 2007 8:21 am

Compair Date returned from DateLastModified

Post by sburke@orrick.com »

I have the following code. I dont know what to do to convert the date format returned from DateLastModified to something I can use. The FormatDateTime thing isnt working for me. I need something that returns an interger like YYYYMMDD (eg 20070615) so I can compair a date to determine if the folder is old enough to delete.

Any ideas?

Sean



Dim folders
folders = "C:Documents and Settings"DeleteOldProfiles(folders)
'SubFolders PropertyFunction DeleteOldProfiles(folderspec) Dim oshell, f, f1, Folder, sf, f2, t, fb, oFS, objFolder, strSource, DateLastModified, NewDate Set oShell = CreateObject("Wscript.Shell") Set oFS = CreateObject("Scripting.FileSystemObject") Set f = oFS.GetFolder(folderspec) Set sf = f.SubFolders For Each f1 in sf on Error Resume Next Folder = f1.name strSource = "C:Documents and Settings" & Folder Set objFolder = oFS.GetFolder(strSource) DateLastModified = objFolder.DateLastModified
NewDate = FormatDateTime(DateLastModified, vbShortDate) WScript.Echo strSource & " " & NewDate
If NewDate > "15-Jun-2006" Then ' oFS.DeleteFolder "C:Documents and Settings" & Folder, True WScript.Echo "date is old" & strSource & " " & NewDate End If NextEnd Function
User avatar
sburke@orrick.com
Posts: 17
Last visit: Thu Sep 13, 2007 8:21 am

Compair Date returned from DateLastModified

Post by sburke@orrick.com »

I have the following code. I dont know what to do to convert the date format returned from DateLastModified to something I can use. The FormatDateTime thing isnt working for me. I need something that returns an interger like YYYYMMDD (eg 20070615) so I can compair a date to determine if the folder is old enough to delete.

Any ideas?

Sean



Dim folders
folders = "C:Documents and Settings"DeleteOldProfiles(folders)
'SubFolders PropertyFunction DeleteOldProfiles(folderspec) Dim oshell, f, f1, Folder, sf, f2, t, fb, oFS, objFolder, strSource, DateLastModified, NewDate Set oShell = CreateObject("Wscript.Shell") Set oFS = CreateObject("Scripting.FileSystemObject") Set f = oFS.GetFolder(folderspec) Set sf = f.SubFolders For Each f1 in sf on Error Resume Next Folder = f1.name strSource = "C:Documents and Settings" & Folder Set objFolder = oFS.GetFolder(strSource) DateLastModified = objFolder.DateLastModified
NewDate = FormatDateTime(DateLastModified, vbShortDate) WScript.Echo strSource & " " & NewDate
If NewDate > "15-Jun-2006" Then ' oFS.DeleteFolder "C:Documents and Settings" & Folder, True WScript.Echo "date is old" & strSource & " " & NewDate End If NextEnd Function
User avatar
ayush
Posts: 1
Last visit: Tue Jun 12, 2007 8:51 am

Compair Date returned from DateLastModified

Post by ayush »

Try this:

folders = "C:Documents and Settings"
DeleteOldProfiles(folders)
Function DeleteOldProfiles(folderspec)
Set sf = CreateObject("Scripting.FileSystemObject").GetFolder(folderspec).subfolders
on Error Resume Next
For Each fl in sf
NewDate = FormatDateTime(fl.DateLastModified, vbShortDate)
WScript.Echo fl.Path & " " & NewDate
If NewDate > #15/Jun/2006# Then
' f1.Delete(true)
WScript.Echo "date is old - " & f1.path & vbnewline & " " & NewDate
End If
Next
End Function
ayush2007-06-12 15:55:18
User avatar
sburke@orrick.com
Posts: 17
Last visit: Thu Sep 13, 2007 8:21 am

Compair Date returned from DateLastModified

Post by sburke@orrick.com »

Great Stuff. Thanks. Much cleaner. Can someone tell me what the # signs are used for?

If NewDate > #15/Jun/2006# Then

Thanks,
Sean

User avatar
sburke@orrick.com
Posts: 17
Last visit: Thu Sep 13, 2007 8:21 am

Compair Date returned from DateLastModified

Post by sburke@orrick.com »

Thanks for the additonal clean-up and suggestions. I need some help understanding why you would take sDate and wrap it with CDate(). Will CDate covert it to something that can be compaired to the value of DateLastModified?

Also, I would perfer to change the calculation to something that determines if the DateLastModified is older than a year from the current date. Do you have any examples of what that would look like?
Regards,

Sean
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Compair Date returned from DateLastModified

Post by jvierra »

Actually, with days, it's even easier than that.

Try the following:

WScript.echo Now - 365
WScript.Echo Date() - 365
Wscript.Echo CDate( "03/29/2005") - 365

The default granularity for teh "Date" object is "days". DateDiff lets you create a datetime object that represents the difference between two dates and can handle all sorts of interesting conditions. DateAdd allows you to determine what is being added - days, months, years, etc.

Most of the dates and times with MS objects are DateTime objects and allow direct addition and comparison. FOr example:

span = Now - CDate("01/01/2007")

will give you the number of days since the beginning of the year as a double - that is it will be a number like 164.489050925928. The DateDiff object allows you to express this ass a date time object with components like minutes, seconds, months.

x = DateDiff("s", CDate("01/01/2007"), Now, vbSunday, vbFirstJan1)
Will give you the number of seconds as an integer.

The date time object and the date time functions in VBS are very good. You can perform many complex datetime manipulations with them that would otherwise be very tricky.

What about finding the current week?
What about the "accounting" wek that retailers use or that is used in OLAP servers?
Waht about finding the datetime for three days ago but what it was on the seventh quarter hour offset by 3 1/2 minutes?

These are all common calculations in most system and are fully supportd vis the DAteTime susbsystem in WIndows which VBS and WSH expose via objects and functions. or methods. If you master these it's a pretty good bet that it will save you a huge amount of time in the future.


User avatar
sburke@orrick.com
Posts: 17
Last visit: Thu Sep 13, 2007 8:21 am

Compair Date returned from DateLastModified

Post by sburke@orrick.com »

Thanks for the education!
This topic is 16 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked