Sign in to follow this  
Followers 0
brobot

copy the latest folder in a directory to another location

4 posts in this topic

hi - noob autoit user here. i've been able to do most everything i've needed up to this point. here's the situation...

in a directory, there are several dumps throughout the day of sub-folders (containing gobs of files). i would like to check this directory, find the most recently created folder and copy that folder to another location.

i've read up on FileGetTime, but i'm just not figuring out how to get that to pinpoint the correct folder.

can anyone help me out here?

Share this post


Link to post
Share on other sites



I didn't go as far as doing the file copy for you, but I wrote out a quick function that should work for you. I commented throughout the function so you can see what I did to get the result.

There may be a much easier way to do this, but this is what I came up with off the top of my head.

#include<file.au3>
#include<array.au3>
#include<date.au3>

$Folder = @DesktopDir & '\test time'
$NewDir = NewestFolder($Folder)
If Not @error Then; If NewestFolder() didn't error out, report the newest directory, otherwise, report the error.
    MsgBox(0, 'Dir time', "Newest directory in " & $Folder & ':' & @CRLF & $NewDir)
Else
    MsgBox(0, 'Error', 'No folders in directory')
EndIf

Func NewestFolder($Directory)
    $aFolders = _FileListToArray($Directory, '*', 2); Get a list of all the single level sub-folders in a specific folder
    If Not IsArray($aFolders) Then; Check if we didn't find any folders, and return 0 with @error = 1 if we do not
        SetError(1)
        Return 0
    EndIf
    _ArrayDelete($aFolders, 0); Remove the first entry containing the total number of entries
    Dim $aEPOCH[UBound($aFolders)][2]; Create a 2d array to hold epoch time and the folder names
    For $a = 0 To UBound($aFolders) - 1
        $aTime = FileGetTime($Directory & '\' & $aFolders[$a], 1, 0); Get time folder was created in an array ($aTime)
        $aEPOCH[$a][0] = _DateDiff('s', "1970/01/01 00:00:00", $aTime[0] & '/' & $aTime[1] & '/' & $aTime[2] & ' ' & $aTime[3] & ':' & $aTime[4] & ':' & $aTime[5]); Convert to Epoch time
        $aEPOCH[$a][1] = $aFolders[$a]; Store directory name
    Next
    _ArraySort($aEPOCH, 1); Sort the $aEPOCH array by EPOCH time, so index 0 contains the newest item
    Return $aEPOCH[0][1]; Return the newest items directory name
EndFunc   ;==>NewestFolder
1 person likes this

Share this post


Link to post
Share on other sites

thanks danwilli!

that totally does the trick! i didn't know about _DateDiff & i'm just feeling my legs as far as array management in autoit. thanks for commenting everything out. really, really helpful & it was a good tutorial for a beginner like me!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is another method I use at work all the time. (We can have multiple folders show up in a day and all need "captured".)

Func folder()
    $Log5 = _FileListToArray("H:\Client\EBER\DAILY\PRINT\","*", 2);adds all folders in specified path to an array
    $yyyymmdd9 = 0 ;sets current folder timestamp to 0
    $yyyymmdd10 = IniRead("C:\variables.ini","time",1,"0") ;sets time to check against to last read folder from previous run
    If IsArray($Log5) Then
    For $i5 = 1 to UBound($Log5)-1
        $aTime5 = FileGetTime("H:\Client\EBER\DAILY\PRINT\"&$Log5[$i5], 0) ;sets the timestamp of the first folder to varianble
        $yyyymmdd9 = $aTime5[0] & $aTime5[1] & $aTime5[2] & $aTime5[3] & $aTime5[4] ;formats the time for comparison
        If $yyyymmdd9 > $yyyymmdd10 Then ;if the new folder's time is greater than the last folder checked at last run, then change the check time
            $yyyymmdd10 = $yyyymmdd9
            $EBERRecent = $Log5[$i5] ;if it's more recent, set the folder path to a single variable (this is where you would do what you need with that folder)
        EndIf
        IniWrite("C:\variables.ini","time",1,$yyyymmdd10) ;commit the last checked folder time for next run
    Next
    EndIf
EndFunc
Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

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