none
VBscript question: Problem with datediff used in a For Each folder object loop RRS feed

  • General discussion

  • I have a VBscript program that checks a folder for old files.

    ext = "csv"

    age = 30  

    HasOldFiles = False
    For Each objFile In fldr.Files
           strExtNameVar = UCase(objFSO.GetExtensionName(objFile.Name))
           If DateDiff("n", objFile.DateLastModified, Now) > age _
                    And strExtNameVar = ext Then
                    strNow = Now
                    Wscript.Echo "Old File name = " & objFile.Name
                    Wscript.Echo "Last Modified Date = " & strLastModDate
                    Wscript.Echo "Current Date = " & strNow
                    HasOldFiles = True
          End If
    Next

    Wscript.Echo "HasOldFiles = " & HasOldFiles

    The problem is the program is reporting HasOldFiles as True intermittently when there are no files at all when checked.  Files are being dumped into the folder (fldr.Files) all day long but then are removed as they are processed by another program.  The program runs every 5 minutes to check the folder for old files. 

    When the false alert occurs Old File name and LastModifiedDate are blank.

    Any ideas why I am getting false alerts?

    Thanks,


    Fred Schmid

    • Changed type Bill_Stewart Wednesday, September 13, 2017 9:38 PM
    • Moved by Bill_Stewart Wednesday, September 13, 2017 9:39 PM This is not "fix/debug/rewrite my script for me" forum
    Wednesday, August 2, 2017 6:15 PM

All replies

  • I would recommend using Exit For after HasOldFiles = True to make sure you exit the loop when the condition is met.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 2, 2017 6:31 PM
  • ext = "csv"
    age = 30   
    HasOldFiles = False
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("d:\test6")
    For Each objFile In fldr.Files
    	strExt = LCase(FSO.GetExtensionName(objFile.Name))
    	If DateDiff("d", objFile.DateLastModified, Now) > age And strExt = ext Then
    		Wscript.Echo "Old File name = " & objFile.Name
    		Wscript.Echo "Last Modified Date = " & objFile.DateLastModified
    		Wscript.Echo "Current Date = " & Now
    		HasOldFiles = True
    	End If
    Next
    
    Wscript.Echo "HasOldFiles = " & HasOldFiles
    Case issues and date test not correct.

    \_(ツ)_/


    • Edited by jrv Wednesday, August 2, 2017 6:38 PM
    Wednesday, August 2, 2017 6:38 PM
  • In my original program this script is part of a function and instead of using the Exit For I use a Exit Function so it will exit the loop when the condition is met.

    In the DateDiff statement Now will always have a value but is it possible for objFile.LastModifiedDate to be NULL and if it is what will DateDiff do with it?

    When there are no files present in the folder my program report that there are no old files.


    Fred Schmid

    Wednesday, August 2, 2017 9:01 PM
  • Last modified date can never be null.  It is set when the file is created.

    FSO will not work with some Samba implementations.


    \_(ツ)_/

    Wednesday, August 2, 2017 9:03 PM
  • The computer the program is running on is using Windows7 OS and the folder it is checking is on the local hard drive.

    Fred

    Thursday, August 3, 2017 12:05 PM
  • What would cause the For Each objFile in Fldr.Files to pick up an empty string and run with it?

    The process with the folder being checked is:

    1. A file is written to the folder every minute.
    2. The files after 10 minutes are deleted.
    3. My program checks every 5 minutes to make sure they are deleted.

    Is it a timing issue?


    Fred

    Thursday, August 3, 2017 12:53 PM