Sign in to follow this  
Followers 0
anyday

find last file in a handle

3 posts in this topic

hey guys, its been awhile since i posted here but i have a simple problem that i need some help with, i searched around here and could not come across anything that was really helpful. Im just trying to find the last filename in a handle returned from FileFindFirstFile(). i have a directory full of images in the form of image-date-num, and i need my script to find the last file in the directory and return it as a $file variable. so if my last file in the directory is image-10-10-2010-15.jpg i need that to end up in my $file variable. any ideas of a clean way of doing this?

thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <array.au3>
$Directory = FileSelectFolder("Select Folder","")
$search = FileFindFirstFile($Directory & '\image-*.jpg')
$sOutput = ''
While 1
    $curfile = FileFindNextFile($search)
    If @error Then ExitLoop
    $sOutput &= $curfile & @LF
WEnd
FileClose($search)
$aFiles = StringSplit($sOutput,@LF,3)
_ArrayDisplay($aFiles)

that's a starter, but as for sorting, since you are using the MM-DD-YYYY format, it will kinda be difficult to create a reasonable sorting without stripping apart each filename to get it in order of date then last image of the last date, I am guessing that is what you are aiming for?

EDIT:

append

$file = 'image-00-00-0000-0.jpg'
For $i in $aFiles
    $aCurrent = StringRegExp($i,'(?i)image-(\d\d)-(\d\d)-(\d{4})-(\d+?).jpg',1 )
    $aOld = StringRegExp($file,'(?i)image-(\d\d)-(\d\d)-(\d{4})-(\d+?).jpg',1 )
    if $aCurrent[2] >= $aOld[2] Then
        if $aCurrent[0] >= $aOld[0] Then
            if $aCurrent[1] >= $aOld[1] Then
                if $aCurrent[3] > $aOld[3] Then
                    $file = $i
                EndIf
            EndIf
        EndIf
    EndIf
Next

not tested (I don't have a directory full of properly named files) but this may be right. worth a try

Edited by nekkutta

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

If you just want the last file found by FileFindNextFile() then keep track in a separate variable:

Global $sSearchDir = "C:\Temp"
Global $hSearch, $sLastFound = "", $sFound = ""

$hSearch = FileFindFirstFile($sSearchDir & "\*.jpg")
If $hSearch = -1 Then
    MsgBox(16, "Error", "No files found in " & $sSearchDir)
    Exit
EndIf

While 1
    $sFound = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    $sLastFound = $sFound
WEnd

MsgBox(64, "Results", "Last file found = " & $sLastFound)

But your definition of "last file" might not be the same as search algorithm used by the function. In that case you have to add your logic as in mekkutta's example.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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