Sign in to follow this  
Followers 0
TonyP

Search directory for newest file

9 posts in this topic

I have been scouring the forums looking for a script that just might do what I want, but I haven't found anything. The situation is this, I have about 14 directories that get daily updates to them and I'm trying to make a script to look inside the folder, find the newest file and then check that file against the current date. If that file is more than 1 day old then shoot off a email... I'm not concerned with the email part, yet... And I figured out how to do the date check, so I was wondering if someone could help me with searching the directories for the newest file...

Here's what I have so far:

#include<date.au3>
$file = "c:\bug.txt"
$fdate = FileGetTime($file,1)
$cdate=$fdate[0]  & "/" & $fdate[1] & "/" & $fdate[2]
$ndate=_NowCalcDate()
$datediff = String(_DateDiff ('d',$cdate,$nDate))

If $datediff > 0 then
MsgBox(0, "Creation date of " & $file, $datediff)
Else
Exit
EndIf

Share this post


Link to post
Share on other sites



Try this...

#include <Date.au3>
#include <Array.au3>
#include <File.au3>

$FolderList = _ArrayCreate("C:\temp123\1", "C:\temp123\2", "C:\temp123\3", "C:\temp123\4", "C:\temp123\5");<----Substitute these with your folders

For $Fdlr = 0 to 4; <----Number of entries above minus 1
    EvaluateFiles($FolderList[$Fdlr])
Next

Func EvaluateFiles($Folder)
    $avFiles = _FileListToArray($Folder & "\", "*",1)
    If @Error<>0 Then
        Return
    EndIf
    
    $iNewestTime = 11111111111111; YYYYMMDDhhmmss
    $iNewestIndex = 0; Array index of newest file
; Find the newest file
    For $p = 1 To $avFiles[0]
        $iFileTime2 = Number(FileGetTime($Folder & "\" & $avFiles[$p], 0, 1))
        If $iFileTime2 > $iNewestTime Then
            $iNewestTime = $iFileTime2
            $iNewestIndex = $p
        EndIf
    Next

    
    If $iNewestIndex > 0 Then
        $t = FileGetTime($Folder & "\" & $avFiles[$iNewestIndex])
        $iDateCalc = _DateDiff( 'd',$t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5],_NowCalc())
        If $iDateCalc > 0 Then;<---Flags files that are older than today
            MsgBox(16,"ATTENTION","This files in - " & $Folder & " - have not been updated today." & @CRLF & @CRLF & $avFiles[$iNewestIndex] & " is the newest file with a modified date of" & @CRLF & @CRLF & $t[1] & "/" & $t[2] & "/" & $t[0] & " " & $t[3] & ":" & $t[4] & ":" & $t[5])
        ;PUT YOUR CODE FOR EMAILING HERE.  USE $avFiles[$iNewestIndex] TO REFERENCE THE FILE NAME IF YOU NEED TO
        EndIf
    Else
        MsgBox(16, "Error", "Failed to find a newest file.")
    EndIf
    
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Wow, Erik that is awesome, did you write that just now? It looks like it'll work for what I need. Thanks!

Uten: Thanks for replying but I must say that your post isn't very helpful. I had looked at that function but it didn't help because if you'll notice the keyword in my post was "searching the directories for the newest file." I know how to search for files, it was finding the newest one that was my problem.

Edited by xenon2050

Share this post


Link to post
Share on other sites

Wow, Erik that is awesome, did you write that just now? It looks like it'll work for what I need. Thanks!

Uten: Thanks for replying but I must say that your post isn't very helpful. I had looked at that function but it didn't help because if you'll notice the keyword in my post was "searching for the newest file." I know how to search for files, it was finding the newest one that was my problem.

I used a snippet of code that I used before and wrote the rest just now...

Share this post


Link to post
Share on other sites

Nice. I'm hoping that I'll get that good with AutoIt someday.. :)

Share this post


Link to post
Share on other sites

Sorry, did not realize you wanted someone to do the work for you. Your code indicated that you know how to compare the file dates. Nothing indicated that you knew how to use the File* functions.

Share this post


Link to post
Share on other sites

Sorry, did not realize you wanted someone to do the work for you. Your code indicated that you know how to compare the file dates. Nothing indicated that you knew how to use the File* functions.

Well I didn't ask for someone to do the work for me, that was a unexpected surprise. I expected to get a piece of code someone had used in something else to search for a file, or some mention of a UDF that did what I was looking for.

Yes I figured out how to compare dates, and I probably should have specified that I had looked at the File functions.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0