Solved

VBSCRIPT DateDiff (current date back x days)

Posted on 2006-11-27
7
1,850 Views
Last Modified: 2008-02-20
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.
0
Comment
Question by:bneese
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18020757
Is there a DateADd in VBScript?

If fil.DateCreated >  DateAdd("d", -4, Now) then
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 18020885
Hi,
Use something like:
If fil.DateCreated >= date - 4 then


HTH
Rory
0
 

Author Comment

by:bneese
ID: 18021000
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18021119
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
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18449456
PAQed with points refunded (500)

DarthMod
Community Support Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now