Solved

VBSCRIPT DateDiff (current date back x days)

Posted on 2006-11-27
7
1,856 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month5 days, 7 hours left to enroll

627 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