Solved

VBSCRIPT DateDiff (current date back x days)

Posted on 2006-11-27
7
1,852 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

809 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