Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

"DateCreated" Script returning incorrect values

Basically I am having to write a "work-around" script for some Software an Insurance House has provided us with that is playing up. So while they work on a proper fix I have thought of a work around to the problem.

Basically what is happening is their print monitor is crashing, the best way to solve this is to close it and restart the program which then starts pushing the documents through. This print monitor stores the documents in the Doc & Settings/App Data/%username%/....... area.

I am writing a script so that when a file in that folder is over 2 minutes old it automatically kills the print processor and then restarts it. This is to make sure that the document does get forced through.

The problem I have is no matter what I do the attached code counts the amount of files in the folder even if they were JUST created, and not over 2 minutes old. I think I need a set of far more experienced eyes to see if I am doing anything wrong, I am very new to this so I may have mis-understood something or overlooked something simple for that I apologise if so.

Cheers.
Set colFiles = oFolder.Files
      nFiles = colFiles.Count
 
      For Each oFile In colFiles
         If DateDiff("s", oFile.DateCreated, Now()) >= 120000 Then   '-- ensure file is at least 2 minutes old
         nFiles = colFiles.Count - 1                     '-- to avoid COUNTING NEW FILES
         End If
      Next

Open in new window

0
ADT58
Asked:
ADT58
  • 4
  • 2
2 Solutions
 
BadotzCommented:
If the date created is greater than 120000, you subtract 1 crom the count of files. This is the problem. Try this:

If DateDiff("s", oFile.DateCreated, Now()) <= 120000 Then   '-- ensure file is at least 2 minutes old
0
 
ADT58Author Commented:
nope still counting files, that have JUST been created and not files that are over 2 minutes (120000 seconds) old. :( can't get my head around this.
0
 
BadotzCommented:
Do this:

If DateDiff("s", oFile.DateCreated, Now()) <= 120000 Then
    MsgBox oFile.DateCreated & vbCrLf & Now() & vbCrLf & DateDiff("s", oFile.DateCreated, Now())
    nFiles = colFiles.Count - 1
End If

See if you can determine anything from that.

Alternatively, you could step through the code with a debugger and examine things during run-time.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
exx1976Commented:
This is an easy one.  :-)  Here's the solution:


Set colFiles = oFolder.Files
nFiles = 0
For Each oFile In colFiles
    If DateDiff("s", oFile.DateCreated, Now()) >= 120000 Then nFiles = nFiles + 1
Next
wscript.echo(nFiles)


The way it was written, it would ALWAYS return the number of files, minus one.  you weren't actually decrementing your counter, you were just always setting it to colfiles.count - 1


Give this a shot and let us know.  It should spit out the number of files older than 2 minutes.

-exx
0
 
BadotzCommented:
Well, DUH on me ;-)
0
 
ADT58Author Commented:
Well at first thanks to Badotz I realised that I was trying to count the impossible with the DateDiff, I think because I was asking it to work out the Different Betwen xx/xx/xx xx:xx:xx and xx/xx/xx xx:xx:xx, I was asking it to work it all out by seconds (120000) instead I changed the "s" to "m" and worked it out by stating:

If DateDiff("m", oFile.DateCreated, Now()) >= 2

So Badotz made me realise something I was completely overlooking and then yours exx was a complete solution obvious on the fact I changed the "s" to "m" and the 120000 to 2 again and it works fine.

Cheers to you both, not only do I have a solution but I also learned something which was a win win.
0
 
BadotzCommented:
Strive to learn one new thing a day, I always say. Good job!

Oh, and no worries - glad to help.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now