Solved

VBSCRIPT DateDiff (current date back x days)

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

18 Experts available now in Live!

Get 1:1 Help Now