Jump to content
ScriptFu

Get latest file in folder

Recommended Posts

ScriptFu

Hello,

I need to get the latest file in a folder and then compress it.

Has anyone done this before?

Thanks.

Share this post


Link to post
Share on other sites
Melba23

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._______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
LurchMan

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.

#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
  • Like 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.

Share this post


Link to post
Share on other sites
Spiff59

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 by Spiff59

Share this post


Link to post
Share on other sites
DaveYuhas

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.

 

#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.

Share this post


Link to post
Share on other sites
Melba23

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._______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
Elena

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

 

Share this post


Link to post
Share on other sites
ViciousXUSMC

5-years-later.jpg

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

×