Sign in to follow this  
Followers 0
GAM

Latest file in a folder

6 posts in this topic

Hi, I have a folder where new builds are copied everyday. Now my task is "HOW TO FIND THE LATEST FILE in that folder?" Help!


Neil

Share this post


Link to post
Share on other sites



Hi, I have a folder where new builds are copied everyday. Now my task is "HOW TO FIND THE LATEST FILE in that folder?" Help!

You need _FileListToArray() and FileGetTime(). Exists another examples on forum, try to search.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Since the function is pretty easy to write, I made it for you.

#include <File.au3>

MsgBox(0, "", _FindLatest(@ScriptDir))

Func _FindLatest($dir)
    Local $latest[2]
    $files = _FileListToArray($dir, "*", 1)
    For $i = 1 To UBound($files) - 1
        If FileGetTime($files[$i], 1, 1) > $latest[0] Or $i = 1 Then
            $latest[0] = FileGetTime($files[$i], 1, 1)
            $latest[1] = $i
        EndIf
    Next
    Return $files[$latest[1]]
EndFunc  ;==>_FindLatest

:P


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#include <File.au3>

MsgBox(0, "", _FindLatest(@ScriptDir))

Func _FindLatest($dir)
    Local $latest[2]
    $files = _FileListToArray($dir, "*", 1)
    For $i = 1 To UBound($files) - 1
        If FileGetTime($files[$i], 1, 1) > $latest[0] Or $i = 1 Then
            $latest[0] = FileGetTime($files[$i], 1, 1)
            $latest[1] = $i
        EndIf
    Next
    Return $files[$latest[1]]
EndFunc  ;==>_FindLatest

Your code saved me some time, but I'm having a bit of a problem with it...:

When I run the script from inside the folder I want to check this works like a charm, but once I add the exact same code into a script lying somewhere else and (ofc) changes the specified dir to a dir variable, the scrips just over and over gives me the same folder name! Even when I'm creating new folders into that same folder it will only give me the name of the (now) oldest folder and not the latest one.

So in short: Code works when run from a script inside the folder to check, but not when used from a script in a distant folder (and yes, the @ScriptDir has been replaced with a variable for the dir path).

Does anyone have an answer to this? ;)

Best regards,

zvvyt

Share this post


Link to post
Share on other sites

zvvyt,

I imagine that you need to add the full path to the filename when using FileGetTime so that it knows where to look. :)

FileGetTime($dir & "" & $files[$i], 1, 1)

Am I right? ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Shit .. I totally forgot about that! >__<'

You really eased my headache!

Once again, thanks a lot M23! ^__^

Best regards,

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