AmphetaMarinE Posted May 14, 2007 Share Posted May 14, 2007 Hi guys, I am writing a sort of backup script, and as a part of it, I need to check a directory containing multiple backup files, and put the filename of the newest file into a variable. I have tried everything I can find, and am not even getting close. Please guys, could someone give me a hand? Assuming I have this sort of thing... $backuppath = 'C:\backup' $dest = 'C:\newbackup' $backupfilename = with C:\backup containing up to 8 files. How would I populate $backupfilename with the filename of the newest created file in C:\Backup?? Link to comment Share on other sites More sharing options...
martin Posted May 14, 2007 Share Posted May 14, 2007 (edited) Hi guys,I am writing a sort of backup script, and as a part of it, I need to check a directory containing multiple backup files, and put the filename of the newest file into a variable.I have tried everything I can find, and am not even getting close.Please guys, could someone give me a hand?Assuming I have this sort of thing...$backuppath = 'C:\backup'$dest = 'C:\newbackup'$backupfilename = with C:\backup containing up to 8 files.How would I populate $backupfilename with the filename of the newest created file in C:\Backup??Look up FileFindFirstFile and related, and FileGetTime. Edited May 14, 2007 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
xcal Posted May 14, 2007 Share Posted May 14, 2007 (edited) A few ways to skin this cat... and since I'm bored, here's one way... $path = 'C:\Windows' MsgBox(0, '', _GetNewestFile($path)) Func _GetNewestFile($arg) If StringRight($arg, 1) <> '\' Then $arg &= '\' Local $first = FileFindFirstFile($arg & '*.*'), $array[1], $hold, $ret, $split While 1 $found = FileFindNextFile($first) If @error Then ExitLoop If StringInStr(FileGetAttrib($arg & $found), 'D') Then ContinueLoop $array[UBound($array) - 1] = FileGetTime($arg & $found, 1, 1) & '|' & $arg & $found ReDim $array[UBound($array) + 1] WEnd For $items In $array $split = StringSplit($items, '|') If $split[1] > $hold Then $hold = $split[1] $ret = $split[2] EndIf Next Return $ret EndFunc edit - fixed a boo boo with filegetattrib() Edited May 14, 2007 by xcal How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
AmphetaMarinE Posted May 15, 2007 Author Share Posted May 15, 2007 A few ways to skin this cat... and since I'm bored, here's one way... $path = 'C:\Windows' MsgBox(0, '', _GetNewestFile($path)) Func _GetNewestFile($arg) If StringRight($arg, 1) <> '\' Then $arg &= '\' Local $first = FileFindFirstFile($arg & '*.*'), $array[1], $hold, $ret, $split While 1 $found = FileFindNextFile($first) If @error Then ExitLoop If StringInStr(FileGetAttrib($arg & $found), 'D') Then ContinueLoop $array[UBound($array) - 1] = FileGetTime($arg & $found, 1, 1) & '|' & $arg & $found ReDim $array[UBound($array) + 1] WEnd For $items In $array $split = StringSplit($items, '|') If $split[1] > $hold Then $hold = $split[1] $ret = $split[2] EndIf Next Return $ret EndFunc edit - fixed a boo boo with filegetattrib() Wow xcal, I only wanted a nudge in the right direction, sorta helps me to learn quicker.... But I gotta say, you definately saved me some time... That worked perfectly. I just wish I actually understood what you did there... I think I have some more reading of the manual to do But, Thank you very much, you guys are awesome! Link to comment Share on other sites More sharing options...
xcal Posted May 15, 2007 Share Posted May 15, 2007 Well, you caught me with nothing to do, so no biggie. Here's an explanation of what's going on. expandcollapse popup$path = 'C:\Windows' MsgBox(0, '', _GetNewestFile($path)) Func _GetNewestFile($arg) ; check if path has a trailing '\' and if there isn't, give it one. ; you could leave that out, really, since you know it needs one. If StringRight($arg, 1) <> '\' Then $arg &= '\' ; tell the script where to start searching, initialize an array, declare a few variables. Local $first = FileFindFirstFile($arg & '*.*'), $array[1], $hold, $ret, $split ; run the following commands in a loop until @error. ; @error will occur with FileFindNextFile when there's no files left to find. While 1 $found = FileFindNextFile($first) If @error Then ExitLoop ; check if a found file is a directory. if it is, skip it, and ; continue to the next iteration in the loop. If StringInStr(FileGetAttrib($arg & $found), 'D') Then ContinueLoop ; put the file's date/time and path/name in the array. ; example entry: 20070514095200|c:\windows\somefile.ext $array[UBound($array) - 1] = FileGetTime($arg & $found, 1, 1) & '|' & $arg & $found ; increase the size of the array by 1 to make room for another entry. ReDim $array[UBound($array) + 1] WEnd ; at this point, $array is holding all the time/dates and path/file in each element. ; $items represents an item in an array index, for each iteration. For $items In $array ; split the array element's string at '|' making $split[1] hold the date and ; split[2] hold the path/file $split = StringSplit($items, '|') ; if $split[1], the date, is greater than $hold... If $split[1] > $hold Then ; ...then we want $hold to become the value of the date... $hold = $split[1] ; ...and if the date is a higher date, we want to save the path/file in $ret. $ret = $split[2] EndIf Next ; when all is finished, $ret holds the path/file with the highest date, so we Return it. Return $ret EndFunc How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
AmphetaMarinE Posted May 15, 2007 Author Share Posted May 15, 2007 Well, you caught me with nothing to do, so no biggie.Here's an explanation of what's going on.Wow man! Thank you so much for taking the time to go through all that xcal.It is all very clear now.I really do appreciate you taking that much time to explain what happens there... I'm very new to autoit, and scripting in general, so this kind of help is golden to me. Cheers,Amph. Link to comment Share on other sites More sharing options...
xcal Posted May 15, 2007 Share Posted May 15, 2007 Np, and welcome to Autoit. You'll be addicted before you know it. How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
jmeerkat Posted May 25, 2007 Share Posted May 25, 2007 A few ways to skin this cat... and since I'm bored, here's one way... $path = 'C:\Windows' MsgBox(0, '', _GetNewestFile($path)) Func _GetNewestFile($arg) If StringRight($arg, 1) <> '\' Then $arg &= '\' Local $first = FileFindFirstFile($arg & '*.*'), $array[1], $hold, $ret, $split While 1 $found = FileFindNextFile($first) If @error Then ExitLoop If StringInStr(FileGetAttrib($arg & $found), 'D') Then ContinueLoop $array[UBound($array) - 1] = FileGetTime($arg & $found, 1, 1) & '|' & $arg & $found ReDim $array[UBound($array) + 1] WEnd For $items In $array $split = StringSplit($items, '|') If $split[1] > $hold Then $hold = $split[1] $ret = $split[2] EndIf Next Return $ret EndFunc edit - fixed a boo boo with filegetattrib() When I ran this, it produced the 2nd newest file in my chosen subdirectory. Since I am a newbie here, could I be doing something wrong? I changed the subdirectory in the $path line, and changed the *.* to *.hcu in the Local $first line, everything else left as is. Did I tamper with something unknown? Thanks Terry Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 25, 2007 Share Posted May 25, 2007 (edited) Got interested in that function and made a few... um, modifications: expandcollapse popup$path = 'C:\Temp' $file = _GetNewestFile($path, 1, 1) If Not @error Then MsgBox(0, 'Newest file', $file) Else MsgBox(16, "Error", "Error occured:" & @CRLF & _ "@error = " & @error & @CRLF & _ "@extended = " & @extended) EndIf ; --------------------------------------- ; Function _GetNewestFile() ; Call with: _GetNewestFile($DirPath [, $DateType [, $Recurse]]) ; Where: $DirPath is the directory to search ; $DateType (0ptional) is the type of date to use [from FileGetTime()]: ; 0 = Modified (default) ; 1 = Created ; 2 = Accessed ; $Recurse (Optional): If non-zero causes the search to be recursive. ; On success returns the full path to the newest file in the directory. ; On failure returns 0 and sets @error (see code below). ; --------------------------------------- Func _GetNewestFile($DirPath, $DateType = 0, $Recurse = 0) Local $Found, $FoundRecurse, $FileTime Local $avNewest[2] = [0, ""]; [0] = time, [1] = file If StringRight($DirPath, 1) <> '\' Then $DirPath &= '\' If Not FileExists($DirPath) Then Return SetError(1, 0, 0) Local $First = FileFindFirstFile($DirPath & '*.*') If $First = -1 Or @error Then Return SetError(2, @error, 0) While 1 $Found = FileFindNextFile($First) If @error Then ExitLoop If StringInStr(FileGetAttrib($DirPath & $Found), 'D') Then If $Recurse Then $FoundRecurse = _GetNewestFile($DirPath & $Found, $DateType, 1) If @error Then ContinueLoop Else $Found = StringReplace($FoundRecurse, $DirPath, "") EndIf Else ContinueLoop EndIf EndIf $FileTime = FileGetTime($DirPath & $Found, $DateType, 1) If $FileTime > $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf WEnd If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile Works for me in minimal testing. It now has error handling, can use any of the three timestamps from FileGetTime, and can search recursively through subdirectories. Be carefull with that Recurse option, it can take a long time to search recursively through something like the Windows directory. Cheers! P.S. Actually, after some more testing, I can't make it take more than about 3 sec to do the Windows directory, or about 90 sec to do the whole C: drive. Seems to do OK for speed. Edited May 25, 2007 by PsaltyDS 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 Link to comment Share on other sites More sharing options...
jmeerkat Posted May 25, 2007 Share Posted May 25, 2007 Got interested in that function and made a few... um, modifications: expandcollapse popup$path = 'C:\Temp' $file = _GetNewestFile($path, 1, 1) If Not @error Then MsgBox(0, 'Newest file', $file) Else MsgBox(16, "Error", "Error occured:" & @CRLF & _ "@error = " & @error & @CRLF & _ "@extended = " & @extended) EndIf ; --------------------------------------- ; Function _GetNewestFile() ; Call with: _GetNewestFile($DirPath [, $DateType [, $Recurse]]) ; Where: $DirPath is the directory to search ; $DateType (0ptional) is the type of date to use [from FileGetTime()]: ; 0 = Modified (default) ; 1 = Created ; 2 = Accessed ; $Recurse (Optional): If non-zero causes the search to be recursive. ; On success returns the full path to the newest file in the directory. ; On failure returns 0 and sets @error (see code below). ; --------------------------------------- Func _GetNewestFile($DirPath, $DateType = 0, $Recurse = 0) Local $Found, $FoundRecurse, $FileTime Local $avNewest[2] = [0, ""]; [0] = time, [1] = file If StringRight($DirPath, 1) <> '\' Then $DirPath &= '\' If Not FileExists($DirPath) Then Return SetError(1, 0, 0) Local $First = FileFindFirstFile($DirPath & '*.*') If $First = -1 Or @error Then Return SetError(2, @error, 0) While 1 $Found = FileFindNextFile($First) If @error Then ExitLoop If StringInStr(FileGetAttrib($DirPath & $Found), 'D') Then If $Recurse Then $FoundRecurse = _GetNewestFile($DirPath & $Found, $DateType, 1) If @error Then ContinueLoop Else $Found = StringReplace($FoundRecurse, $DirPath, "") EndIf Else ContinueLoop EndIf EndIf $FileTime = FileGetTime($DirPath & $Found, $DateType, 1) If $FileTime > $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf WEnd If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile Works for me in minimal testing. It now has error handling, can use any of the three timestamps from FileGetTime, and can search recursively through subdirectories. Be carefull with that Recurse option, it can take a long time to search recursively through something like the Windows directory. Cheers! P.S. Actually, after some more testing, I can't make it take more than about 3 sec to do the Windows directory, or about 90 sec to do the whole C: drive. Seems to do OK for speed. PsaltyDS, thanks for that fast response. I knew I must have been mistaken, and it turns out that the file I thought was newest, 328217.HCU, showed a "modified date" of 11-25-2006 12:10am, while the file that the first program found, (and was confirmed by your new program) was 328216.HCU, which showed a "modified date" of 11-19-2006, 12:30PM. Since the 11-19 file was shown as "newer" than the 11-25 file, I was questioning my sanity. In desperation, I added a new column to my Windows Explorer layout ("Date Created".) Oddly enough, both of the above files were created on 12-13-2006 at 6:38pm. (A tie must go to one or the other I guess). Now all this newbie needs to do is figure out how a file can be created at a later date than it was originally modified, I guess I've got a lot to learn. Again, thanks for your program, I'm learning a lot by dissecting and comparing things line by line Thanks Terry Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 26, 2007 Share Posted May 26, 2007 PsaltyDS, thanks for that fast response. I knew I must have been mistaken, and it turns out that the file I thought was newest, 328217.HCU, showed a "modified date" of 11-25-2006 12:10am, while the file that the first program found, (and was confirmed by your new program) was 328216.HCU, which showed a "modified date" of 11-19-2006, 12:30PM. Since the 11-19 file was shown as "newer" than the 11-25 file, I was questioning my sanity. In desperation, I added a new column to my Windows Explorer layout ("Date Created".) Oddly enough, both of the above files were created on 12-13-2006 at 6:38pm. (A tie must go to one or the other I guess). Now all this newbie needs to do is figure out how a file can be created at a later date than it was originally modified, I guess I've got a lot to learn. Again, thanks for your program, I'm learning a lot by dissecting and comparing things line by line Thanks Terry Note that the time stamp returned by FileGetTime() includes seconds, but the GUI display in explorer doesn't. To see what that looks like make this temporary change at the bottom of the _GetNewestFile() function: If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else MsgBox(64, "Debug", "Time = " & $avNewest[0]) Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile If you use that with the recurse option, you'll get a pop up from each recursive iteration also. If the time stamps match down to the second, then the function will return the first one found with that time stamp, because it only replaces the current newest in $avNewest if the new time stamp is less than (does not compare for equal) than the previous newest. Glad you liked it. I learned a little from coding it! 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 Link to comment Share on other sites More sharing options...
jmeerkat Posted May 26, 2007 Share Posted May 26, 2007 Note that the time stamp returned by FileGetTime() includes seconds, but the GUI display in explorer doesn't. To see what that looks like make this temporary change at the bottom of the _GetNewestFile() function: If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else MsgBox(64, "Debug", "Time = " & $avNewest[0]) Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile If you use that with the recurse option, you'll get a pop up from each recursive iteration also. If the time stamps match down to the second, then the function will return the first one found with that time stamp, because it only replaces the current newest in $avNewest if the new time stamp is less than (does not compare for equal) than the previous newest. Glad you liked it. I learned a little from coding it! Very cool! I"ll add that to my bag of tricks, amazing the power this program has! Link to comment Share on other sites More sharing options...
AmphetaMarinE Posted December 10, 2007 Author Share Posted December 10, 2007 (edited) I hate to dig up an old thread, but I'm revisiting this one.I have no idea how that revised _FindNewestFile function works, but what would it take to add an extra switch to enable it to find the oldest file in a directory? (By modified Date)It would then be the perfect function for what I need. Cheers for any help. Edited December 10, 2007 by AmphetaMarinE Link to comment Share on other sites More sharing options...
picaxe Posted December 10, 2007 Share Posted December 10, 2007 (edited) Try this, added optional param to func for backward compatibility expandcollapse popup$path = 'C:\Temp' $Oldestfile = _GetNewestFile($path, 1, 1, 1) ; oldest $Newestfile = _GetNewestFile($path, 1, 1) ; newest If Not @error Then MsgBox(0, 'Newest & Oldest File', "Newest File = " & $Newestfile & @CRLF & "Oldest File = " & $Oldestfile) Else MsgBox(16, "Error", "Error occured:" & @CRLF & _ "@error = " & @error & @CRLF & _ "@extended = " & @extended) EndIf ; --------------------------------------- ; Function _GetNewestFile() ; Call with: _GetNewestFile($DirPath [, $DateType [, $Recurse] [, $Oldest]) ; Where: $DirPath is the directory to search ; $DateType (0ptional) is the type of date to use [from FileGetTime()]: ; 0 = Modified (default) ; 1 = Created ; 2 = Accessed ; $Recurse (Optional): If non-zero causes the search to be recursive. ; $Oldest (Optional): If non-zero find oldest file ; On success returns the full path to the newest file in the directory. ; On failure returns 0 and sets @error (see code below). ; --------------------------------------- Func _GetNewestFile($DirPath, $DateType = 0, $Recurse = 0, $Oldest = 0) Local $Found, $FoundRecurse, $FileTime Local $avNewest[2] = [0, ""]; [0] = time, [1] = file If StringRight($DirPath, 1) <> '\' Then $DirPath &= '\' If Not FileExists($DirPath) Then Return SetError(1, 0, 0) Local $First = FileFindFirstFile($DirPath & '*.*') If $First = -1 Or @error Then Return SetError(2, @error, 0) If $Oldest Then $avNewest[0] = FileGetTime($DirPath & FileFindNextFile($First), $DateType, 1) $avNewest[1] = $DirPath & FileFindNextFile($First) EndIf While 1 $Found = FileFindNextFile($First) If @error Then ExitLoop If StringInStr(FileGetAttrib($DirPath & $Found), 'D') Then If $Recurse Then $FoundRecurse = _GetNewestFile($DirPath & $Found, $DateType, 1) If @error Then ContinueLoop Else $Found = StringReplace($FoundRecurse, $DirPath, "") EndIf Else ContinueLoop EndIf EndIf $FileTime = FileGetTime($DirPath & $Found, $DateType, 1) If $Oldest Then If $FileTime < $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf Else If $FileTime > $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf EndIf WEnd If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile Edited December 10, 2007 by picaxe Link to comment Share on other sites More sharing options...
AmphetaMarinE Posted December 10, 2007 Author Share Posted December 10, 2007 Try this, added optional param to func for backward compatibility expandcollapse popup$path = 'C:\Temp' $Oldestfile = _GetNewestFile($path, 1, 1, 1) ; oldest $Newestfile = _GetNewestFile($path, 1, 1) ; newest If Not @error Then MsgBox(0, 'Newest & Oldest File', "Newest File = " & $Newestfile & @CRLF & "Oldest File = " & $Oldestfile) Else MsgBox(16, "Error", "Error occured:" & @CRLF & _ "@error = " & @error & @CRLF & _ "@extended = " & @extended) EndIf ; --------------------------------------- ; Function _GetNewestFile() ; Call with: _GetNewestFile($DirPath [, $DateType [, $Recurse]]) ; Where: $DirPath is the directory to search ; $DateType (0ptional) is the type of date to use [from FileGetTime()]: ; 0 = Modified (default) ; 1 = Created ; 2 = Accessed ; $Recurse (Optional): If non-zero causes the search to be recursive. ; On success returns the full path to the newest file in the directory. ; On failure returns 0 and sets @error (see code below). ; --------------------------------------- Func _GetNewestFile($DirPath, $DateType = 0, $Recurse = 0, $Oldest = 0) Local $Found, $FoundRecurse, $FileTime Local $avNewest[2] = [0, ""]; [0] = time, [1] = file If StringRight($DirPath, 1) <> '\' Then $DirPath &= '\' If Not FileExists($DirPath) Then Return SetError(1, 0, 0) Local $First = FileFindFirstFile($DirPath & '*.*') If $First = -1 Or @error Then Return SetError(2, @error, 0) If $Oldest Then $avNewest[0] = FileGetTime($DirPath & FileFindNextFile($First), $DateType, 1) $avNewest[1] = $DirPath & FileFindNextFile($First) EndIf While 1 $Found = FileFindNextFile($First) If @error Then ExitLoop If StringInStr(FileGetAttrib($DirPath & $Found), 'D') Then If $Recurse Then $FoundRecurse = _GetNewestFile($DirPath & $Found, $DateType, 1) If @error Then ContinueLoop Else $Found = StringReplace($FoundRecurse, $DirPath, "") EndIf Else ContinueLoop EndIf EndIf $FileTime = FileGetTime($DirPath & $Found, $DateType, 1) If $Oldest Then If $FileTime < $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf Else If $FileTime > $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf EndIf WEnd If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile Nope. No Good. It returns for me the second created file as the oldest. When modifying them, it still returns the same results... I wonder if its something on my end? I am testing with a 2 folders on my c drive, c:\backup1 and c:\backup2 In each of these i create 4 new text files, named 1.txt, 2.txt, 3.txt and 4.txt in that order. I then modify them from 1 to 4, and the oldest file returned should move up in number as each preceeding file is modified.... But then again, could be just me... Wish I understood more about this function... it is very complex to me.... But thank you picaxe, i think it is definately a big step up from where i was trying.. I could not get anything to work after modifying the original... >.< Link to comment Share on other sites More sharing options...
picaxe Posted December 10, 2007 Share Posted December 10, 2007 I've doen some testing with recursing and there's a problem recursing subdirs. When the date of the subdir is newest. The results are wrong I'm test the original. Link to comment Share on other sites More sharing options...
picaxe Posted December 10, 2007 Share Posted December 10, 2007 (edited) Try this and use the "0" = modified date, as the 2nd param. Corrected an oversight, I also forgot to pass $oldest param in the recursive call to itself. If you copy a subdirectory into c:\temp (as for testing) the "created date" will be newer that the "modified date". My explorer window was not set to display the "created date" so the problem wasn't immediately obvious. expandcollapse popup$path = 'C:\Temp' $Oldestfile = _GetNewestFile($path, 0, 1, 1) ; oldest $Newestfile = _GetNewestFile($path, 0, 1) ; newest If Not @error Then MsgBox(0, 'Newest & Oldest File', "Newest File = " & $Newestfile & @CRLF & "Oldest File = " & $Oldestfile) Else MsgBox(16, "Error", "Error occured:" & @CRLF & _ "@error = " & @error & @CRLF & _ "@extended = " & @extended) EndIf ; --------------------------------------- ; Function _GetNewestFile() ; Call with: _GetNewestFile($DirPath [, $DateType [, $Recurse] [, $Oldest]) ; Where: $DirPath is the directory to search ; $DateType (0ptional) is the type of date to use [from FileGetTime()]: ; 0 = Modified (default) ; 1 = Created ; 2 = Accessed ; $Recurse (Optional): If non-zero causes the search to be recursive. ; $Oldest (Optional): If non-zero find oldest file ; On success returns the full path to the newest file in the directory. ; On failure returns 0 and sets @error (see code below). ; --------------------------------------- Func _GetNewestFile($DirPath, $DateType = 0, $Recurse = 0, $Oldest = 0) Local $Found, $FoundRecurse, $FileTime Local $avNewest[2] = [0, ""]; [0] = time, [1] = file If StringRight($DirPath, 1) <> '\' Then $DirPath &= '\' If Not FileExists($DirPath) Then Return SetError(1, 0, 0) Local $First = FileFindFirstFile($DirPath & '*.*') If $First = -1 Or @error Then Return SetError(2, @error, 0) If $Oldest Then $avNewest[0] = FileGetTime($DirPath & FileFindNextFile($First), $DateType, 1) $avNewest[1] = $DirPath & FileFindNextFile($First) EndIf While 1 $Found = FileFindNextFile($First) If @error Then ExitLoop If StringInStr(FileGetAttrib($DirPath & $Found), 'D') Then If $Recurse Then $FoundRecurse = _GetNewestFile($DirPath & $Found, $DateType, 1, $Oldest) If @error Then ContinueLoop Else $Found = StringReplace($FoundRecurse, $DirPath, "") EndIf Else ContinueLoop EndIf EndIf $FileTime = FileGetTime($DirPath & $Found, $DateType, 1) If $Oldest Then If $FileTime < $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf Else If $FileTime > $avNewest[0] Then $avNewest[0] = $FileTime $avNewest[1] = $DirPath & $Found EndIf EndIf WEnd If $avNewest[0] = 0 Then Return SetError(3, 0, 0) Else Return $avNewest[1] EndIf EndFunc ;==>_GetNewestFile Edited December 10, 2007 by picaxe Link to comment Share on other sites More sharing options...
picaxe Posted December 10, 2007 Share Posted December 10, 2007 Maybe this time. I've edited previous post, using the wrong param in the script. Test's ok for me Link to comment Share on other sites More sharing options...
AmphetaMarinE Posted December 11, 2007 Author Share Posted December 11, 2007 (edited) Working fine except for one thing, The script picks up the second oldest file, rather than the oldest, if there is no subfolder in the directory stored in $path I guess its something to do with the while loop, but loops is the part of autoit i have trouble with. (Along with arrays) It is this that stops me from actually understanding how the script works... Guess I'll have to read over the help file once again to try and wrap my head around looping... Edited December 11, 2007 by AmphetaMarinE Link to comment Share on other sites More sharing options...
picaxe Posted December 11, 2007 Share Posted December 11, 2007 Working fine except for one thing,The script picks up the second oldest file, rather than the oldest, if there is no subfolder in the directory stored in $pathI've done some more testing, works ok for me when there is no subfolder. I get the correct oldest file. I am using $DateType=0 for "modified date". Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now