Sign in to follow this  
Followers 0
memnon

find oldest file(s) in directory

12 posts in this topic

#1 ·  Posted (edited)

hello,

i need to find the oldest file(s) in directory to zip them away and delete them after.

I therefor tried to use Filegettime and _fileListToArray

Local $aFileList = _FileListToArray("d:tmptestold", "*.conf")

for $i=1 to $aFilelist[0]
        $FileTime=FileGetTime("d:tmptestold" & $aFilelist[$i], $FT_MODIFIED, 0)
        $FileX=$aFilelist[$i] & "," & $Filetime[0] & "," & $Filetime[1] & "," & $Filetime[2]
        Local $array2=StringSplit($fileX, ",")

       _ArrayDisplay($Array2, "$Array2")
next

so i get Filename,Year,Month,Day within $Array2

but how can i get that $Array2 (within the for-next loop) into a new Array and how can i "sort" the rows from oldest to newest then ?

someone with an idea for me ?

Edited by memnon

das beste Windows Support Forum: Windows 2000 Helpline und tschüss den WindowsfehlernProgrammieren: Autoit 3 - wer braucht noch VBS ?!Programmieren: Autoit 3 Forum?

Share this post


Link to post
Share on other sites



Create a two dimensional array where you have filename + timeinformation on every item. Then sort it using _ArraySort on the colum for time

Share this post


Link to post
Share on other sites

Here's a function that I use to retrieve the most recently modified file. You could use this as a starting point...

Func _GetLastFile($sPath, $sFileSpec, $lDirectory = False, $nStampType = 0)
    Local $aList, $sResult

    $sPath = AddBackslash($sPath)

    ; Get array of file / directory names
    $aList = _FileListToArray($sPath, $sFileSpec, _Iif($lDirectory, 2, 1))

    ; Create a suitably sized 2D array to hold the names and sizes - count in the [0][0] element
    Local $aName_Time[$aList[0] + 1][2] = [[$aList[0]]]

    ; Now loop through the original array
    For $i = 1 To $aList[0]
        ; Transfer the names to the [0] element
        $aName_Time[$i][0] = $sPath & $aList[$i]

        If $lDirectory And StringRight($aName_Time[$i][0], 1) = "\" Then
            ; remove trailing slash \
            $aName_Time[$i][0] = StringLeft($aName_Time[$i][0], StringLen($aName_Time[$i][0]) - 1)
        EndIf

        ; Get the requested time to put in the [1] element
        $aName_Time[$i][1] = FileGetTime($aName_Time[$i][0], $nStampType, 1)
    Next

    ; Sort array by modified time
    _ArraySort($aName_Time, 0, 1, 0, 1)

    ; Get name of file at the bottom of the list
    $sResult = $aName_Time[$aName_Time[0][0]][0]

    Return $sResult
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Another example

#include <File.au3>
#include <array.au3>

Local $sSearchPath = "c:\test\"
Local $aFileList = _FileListToArray($sSearchPath, "*.*")
Local $aExtendedFileList[$aFileList[0]+1][2]

for $i=1 to $aFilelist[0]
    $aExtendedFileList[$i][0] = $aFileList[$i]
    $aExtendedFileList[$i][1] = FileGetTime($sSearchPath & $aFileList[$i] , $FT_MODIFIED, 1)
next

_ArraySort($aExtendedFileList, 0,0,0, 1)
_ArrayDisplay($aExtendedFileList, "$aFileList")
Edited by Geir1983

Share this post


Link to post
Share on other sites

thx very much Geir1983, works like a charm


das beste Windows Support Forum: Windows 2000 Helpline und tschüss den WindowsfehlernProgrammieren: Autoit 3 - wer braucht noch VBS ?!Programmieren: Autoit 3 Forum?

Share this post


Link to post
Share on other sites

Func AddBackslash($sPath)
    If StringRight($sPath, 1) <> "\" Then
        $sPath &= '\'
    EndIf

    Return $sPath
EndFunc

_Iif is defined in Misc.au3

; #INDEX# =======================================================================================================================
; Title .........: Misc
; AutoIt Version : 3.3.7.20++
; Language ......: English
; Description ...: Functions that assist with Common Dialogs.
; Author(s) .....: Gary Frost, Florian Fida (Piccaso), Dale (Klaatu) Thompson, Valik, ezzetabi, Jon, Paul Campbell (PaulIA)
; Dll(s) ........: comdlg32.dll, user32.dll, kernel32.dll, advapi32.dll, gdi32.dll
; ===============================================================================================================================

Share this post


Link to post
Share on other sites

_Iif doesn't exist any more, it was removed back in 3.3.9.6 in 2013. Use the ternary operator in its place.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

a last question to Geir1983 - i dont understand the line

Local $aExtendedFileList[$aFileList[0]+1][2]

i have never seen a definition for an array like that before - what does [0]+1] [2] mean ?


das beste Windows Support Forum: Windows 2000 Helpline und tschüss den WindowsfehlernProgrammieren: Autoit 3 - wer braucht noch VBS ?!Programmieren: Autoit 3 Forum?

Share this post


Link to post
Share on other sites

You are declaring an array where the size of the first dimension is determined by the value of $aFileList[0]+1.

Share this post


Link to post
Share on other sites

got it now, man i´m stupid^^

thx danp2


das beste Windows Support Forum: Windows 2000 Helpline und tschüss den WindowsfehlernProgrammieren: Autoit 3 - wer braucht noch VBS ?!Programmieren: Autoit 3 Forum?

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