ScriptFu Posted October 23, 2012 Share Posted October 23, 2012 Hello, I need to get the latest file in a folder and then compress it. Has anyone done this before? Thanks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 23, 2012 Moderators Share Posted October 23, 2012 ScriptFu,Not that I know of, but a combination of _FileListToArray, FileGetTime and wraithdu's ZIP UDF should be able to do it fairly easily. Give it a try yourself first - you know where we are if you run into problems. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
LurchMan Posted October 23, 2012 Share Posted October 23, 2012 This would be my example for how to find the latest file..its probably not done in the best fashion but its the way I know how to do it. expandcollapse popup#include <file.au3> #include <array.au3> #include <date.au3> $rtn = _LatestFile ("C:dell") ConsoleWrite($rtn & @LF) Func _LatestFile ($sDir) Local $aTimes[1] If StringRight($sDir, 1) <> "" Then $sDir &= "" $aFiles = _FileListToArray ($sDir, "*", 1) $err = @error If $err > 0 Then Switch $err Case 1 SetError (1) ;Path not found Return -1 Case 4 SetError (2) ;No files found Return -1 EndSwitch EndIf ReDim $aTimes[$aFiles[0]+1] $aTimes[0] = $aFiles[0] For $n = 1 To $aFiles[0] $atmp = FileGetTime ($sDir & $aFiles[$n]) $aTimes[$n] = $atmp[0] & "/" & $atmp[1] & "/" & $atmp[2] & " " & $atmp[3] & ":" & $atmp[4] & ":" & $atmp[5] Next For $n = 1 To $aTimes[0] If $n = 1 Then $sCurrHigh = $aTimes[1] $diff = _DateDiff ("s", $sCurrHigh, $aTimes[$n+1]) If $n+2 > $aTimes[0] Then ExitLoop If StringLeft ($diff, 1) <> "-" Then $sCurrHigh = $aTimes[$n+1] Next For $n = 1 To $aTimes[0] If $sCurrHigh = $aTimes[$n] Then $sLatestFile = $aFiles[$n] ExitLoop EndIf Next Return $sLatestFile EndFunc Elena 1 Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end. Link to comment Share on other sites More sharing options...
Spiff59 Posted October 23, 2012 Share Posted October 23, 2012 (edited) There's also the DOS route, which has the advantage of speed and the disadvantage of not processing unicode filenames: Global $sDirectory = @WindowsDir, $sStdOut $PID = Run(@ComSpec & ' /c DIR "' & $sDirectory & '" /A-D /-C /O-D /B', "", @SW_HIDE, 2) ; 2 = $STDOUT_CHILD While Not @error $sStdOut &= StdoutRead($PID) WEnd $sStdOut = StringStripWS($sStdOut, 7) $aStdOut = StringSplit($sStdOut, @CRLF) MsgBox(0,"",$aStdOut[1]) Edited October 23, 2012 by Spiff59 Link to comment Share on other sites More sharing options...
DaveYuhas Posted August 7, 2013 Share Posted August 7, 2013 This would be my example for how to find the latest file..its probably not done in the best fashion but its the way I know how to do it. expandcollapse popup#include <file.au3> #include <array.au3> #include <date.au3> $rtn = _LatestFile ("C:dell") ConsoleWrite($rtn & @LF) Func _LatestFile ($sDir) Local $aTimes[1] If StringRight($sDir, 1) <> "" Then $sDir &= "" $aFiles = _FileListToArray ($sDir, "*", 1) $err = @error If $err > 0 Then Switch $err Case 1 SetError (1) ;Path not found Return -1 Case 4 SetError (2) ;No files found Return -1 EndSwitch EndIf ReDim $aTimes[$aFiles[0]+1] $aTimes[0] = $aFiles[0] For $n = 1 To $aFiles[0] $atmp = FileGetTime ($sDir & $aFiles[$n]) $aTimes[$n] = $atmp[0] & "/" & $atmp[1] & "/" & $atmp[2] & " " & $atmp[3] & ":" & $atmp[4] & ":" & $atmp[5] Next For $n = 1 To $aTimes[0] If $n = 1 Then $sCurrHigh = $aTimes[1] $diff = _DateDiff ("s", $sCurrHigh, $aTimes[$n+1]) If $n+2 > $aTimes[0] Then ExitLoop If StringLeft ($diff, 1) <> "-" Then $sCurrHigh = $aTimes[$n+1] Next For $n = 1 To $aTimes[0] If $sCurrHigh = $aTimes[$n] Then $sLatestFile = $aFiles[$n] ExitLoop EndIf Next Return $sLatestFile EndFunc When I run the code I get a "Subscript used with non-Array variable" error. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 7, 2013 Moderators Share Posted August 7, 2013 DaveYuhas,Welcome to the AutoIt forum. That error indicates that you are trying to access what you think is an array, but is actually not. I suggest you modify the script to use IsArray to check on all the arrays before you try to access them. Then once you have identifed the non-existent array, you can look to see why it is not being created as expected. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Elena Posted April 3, 2018 Share Posted April 3, 2018 Let me also introduce you my function, I guess it's simple and rather clear. ; Func: _FindLatestLog ; Searches the latest file in specified directory with specified mask ; Example of usage: FindLatestLog (@UserProfileDir & "\AppData\Roaming\Application", "*Waiter*.log") Func _FindLatestLog ($dir, $mask) Local $latestTime = 0, $currentTime = 0 Local $files = _FileListToArray($dir, $mask, 1) If @error = 1 Then ConsoleWrite('Func: FindLog, Error: Path was invalid.' & @CRLF) Return False EndIf If @error = 4 Then ConsoleWrite('Func: FindLog, Error: Log file was not found.' & @CRLF) Return False EndIf For $i = 1 To UBound($files) - 1 $currentFile = $dir & "\" & $files[$i] $currentTime = FileGetTime($currentFile, $FT_MODIFIED, 1) If $currentTime > $latestTime Or $i = 1 Then $latestTime = $currentTime $latestFile = $currentFile EndIf Next ConsoleWrite( "Latest log file: " & $latestFile & ", time modified: " & $latestTime & @CRLF) Return $latestFile EndFunc ;==>_FindLatestLog snooffy and NassauSky 2 Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted April 3, 2018 Share Posted April 3, 2018 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