bneese
asked on
VBSCRIPT DateDiff (current date back x days)
I need a script that will look into a directory and open all files that are from the current date and back say 4 days. I have most of the code to read the files but I can't figure out how to do the datediff.
This is what I currently have:
If DateDiff("d",Now,fil.DateC reated) = -1 Or fil.DateCreated = Now Then
this only does the current date I think.
This is what I currently have:
If DateDiff("d",Now,fil.DateC
this only does the current date I think.
Hi,
Use something like:
If fil.DateCreated >= date - 4 then
HTH
Rory
Use something like:
If fil.DateCreated >= date - 4 then
HTH
Rory
ASKER
Thanks for all the help guys. As I was looking at it after about an hour of trying to figure it out I got it to work using this:
If DateDiff("d",Now,fil.DateL astModifie d) > -3 Then
If DateDiff("d",Now,fil.DateL
Here is an example of using DateAdd() as GrahamSkan suggests...
But are you looking for things OLDER than a certain time period? or NEWER?
If OLDER then:
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
startFolder = "c:\someFolder\" ' folder to start searching (subfolders will also be searched)
OlderThanDate = DateAdd("d", -3, Date) ' 3 days (adjust as necessary)
SearchFiles startFolder, OlderThanDate
Function SearchFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate Then
' do something with the file...
' fso.DeleteFile(file.Path)
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
SearchFiles subFolder.Path, BeforeDate
Next
End Function
***If you want files NEWER, then just switch the If statement so it has a greater than symbol:
If file.DateLastModified > BeforeDate Then
' do something with the file...
' fso.DeleteFile(file.Path)
End If
But are you looking for things OLDER than a certain time period? or NEWER?
If OLDER then:
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.Fi
startFolder = "c:\someFolder\" ' folder to start searching (subfolders will also be searched)
OlderThanDate = DateAdd("d", -3, Date) ' 3 days (adjust as necessary)
SearchFiles startFolder, OlderThanDate
Function SearchFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate Then
' do something with the file...
' fso.DeleteFile(file.Path)
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
SearchFiles subFolder.Path, BeforeDate
Next
End Function
***If you want files NEWER, then just switch the If statement so it has a greater than symbol:
If file.DateLastModified > BeforeDate Then
' do something with the file...
' fso.DeleteFile(file.Path)
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If fil.DateCreated > DateAdd("d", -4, Now) then