Link to home
Start Free TrialLog in
Avatar of bneese
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.DateCreated) = -1 Or fil.DateCreated = Now Then

this only does the current date I think.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Is there a DateADd in VBScript?

If fil.DateCreated >  DateAdd("d", -4, Now) then
Hi,
Use something like:
If fil.DateCreated >= date - 4 then


HTH
Rory
Avatar of bneese
bneese

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.DateLastModified) > -3 Then
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.FileSystemObject")
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
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial