Sign in to follow this  
Followers 0
microera

Last created file in directory?

15 posts in this topic

Hi

How to get name of the file (last created file) in (specified) directory?

Thanks

Share this post


Link to post
Share on other sites



Try this

Share this post


Link to post
Share on other sites

HI

Thanks for quick answer! Very good forum..... I'm new....

.... ERROR: can't open include file <_FileListToArrayFaster1f.au3>

Where is _FileListToArrayFaster1f.au3 located?

Thanks!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <Array.au3>
Global $time
$search = FileFindFirstFile("*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    If StringInStr($file,".") Then
        $t =  FileGetTime($file, 1,1)
        If $time = "" Then
            $time = $t&"\"&$file
        Else
            $time = $time&"/"&$t&"\"&$file
        EndIf
    EndIf
WEnd
$array = StringSplit($time,"/")
_ArrayDelete($array,0)
_ArraySort($array,1)
_ArrayDisplay($array)
FileClose($search)

try this, hope that i didnt made some error in code, if i did sry :)

reedited code and coment: on $array[0] shud b last created file with time and its name

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

WOOOOW

MAgic!

:)

Working!

T H A N K S !!

Share this post


Link to post
Share on other sites

HI

Thanks for quick answer! Very good forum..... I'm new....

.... ERROR: can't open include file <_FileListToArrayFaster1f.au3>

Where is _FileListToArrayFaster1f.au3 located?

Thanks!

I don't know were that error comes from. I had in mind post 17 in that thread. For future reference here is the code I should have pointed to (Confirmed Ok with AutoIt v3.3.0.0)
$path = 'C:\Temp'

$Oldestfile = _GetNewestFile($path, 0, 1, 1)    ; this is oldest modified (recursing subdirs)
$Newestfile = _GetNewestFile($path, 0, 1)   ; this is newest modified (recursing subdirs)

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

and I would have just done a

$tempfile = @TempDir & "\" & Random(1000,9000,1)
RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,@SW_HIDE)
$asArray = StringSplit(FileRead($tempfile),@CRLF,3)
FileDelete($tempfile)

all you need to do is set the working directory ($workdir) and your newest created file will be in ($asArray[0])

but I'm sure all of that other code is cleaner.

EDIT: used Run instead of Runwait, D'oh

Edited by nekkutta

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

@picaxe

YES! Working OK with 3.3.0.0. and latest beta version!

VERY GOOD!

@nekkutta

C:\Program Files\AutoIt3\Examples\tmp3.au3(2,77) : WARNING: $workdir: possibly used before declaration.

RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\tmp3.au3(2,77) : ERROR: $workdir: undeclared global variable.

RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\tmp3.au3 - 1 error(s), 1 warning(s)

Thanks to all!

Share this post


Link to post
Share on other sites

@nekkutta

C:\Program Files\AutoIt3\Examples\tmp3.au3(2,77) : WARNING: $workdir: possibly used before declaration.

RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\tmp3.au3(2,77) : ERROR: $workdir: undeclared global variable.

RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\tmp3.au3 - 1 error(s), 1 warning(s)

Thanks to all!

Like I said, you need to set $workdir ie. $workdir = FileSelectFolder("","",0,"")

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

How to display filename?

In bellow example $asArray isn't visible! (or filename maybe blank?)

?

$workdir = FileSelectFolder("Choose a folder!", "")

$tempfile = @TempDir & "\" & Random(1000,9000,1)

MsgBox(4096, "Result", $tempfile)

Sleep(2000)

RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,@SW_HIDE)

$asArray = StringSplit(FileRead($tempfile),@CRLF,3)

MsgBox(4096, "Result", $asArray)

FileDelete($tempfile)

Share this post


Link to post
Share on other sites

Sorry! my mistake!

MsgBox(4096, "Result", $asArray[0])

VEERY GOOD!

Share this post


Link to post
Share on other sites

You might want to avoid using

RunWait(@comspec & ' /c dir /b /a-d /o-d /tc *.* > ' & $tempfile , $workdir ,@SW_HIDE)
because it will not find windows filenames that contain special characters (even for English). See this post and thread for more info.

Share this post


Link to post
Share on other sites

#include <Array.au3>
Global $time
$search = FileFindFirstFile("*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    If StringInStr($file,".") Then
        $t =  FileGetTime($file, 1,1)
        If $time = "" Then
            $time = $t&"\"&$file
        Else
            $time = $time&"/"&$t&"\"&$file
        EndIf
    EndIf
WEnd
$array = StringSplit($time,"/")
_ArrayDelete($array,0)
_ArraySort($array,1)
_ArrayDisplay($array)
FileClose($search)

try this, hope that i didnt made some error in code, if i did sry :D

reedited code and coment: on $array[0] shud b last created file with time and its name

If you repeat above procedure you get wrong (doubled files) ..... Why?

Please test with copyed code again or make function and run it twice!

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

If you repeat above procedure you get wrong (doubled files) ..... Why?

Please test with copyed code again or make function and run it twice!

dont see the point, if you dont post your code with this bug then i will not know where it is

probably after every func call youl need to do $time = "" to clean it and $array, but until you dont post your code i will newer know if im correct about it.

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

dont see the point, if you dont post your code with this bug then i will not know where it is

probably after every func call youl need to do $time = "" to clean it and $array, but until you dont post your code i will newer know if im correct about it.

Hi

YEES!

$time = ""

do the job!

ALL IS OK!

p.s

as I say: if EXACTLY repeat code twice this errors accoure.....

but after $time="" in begining all is OK!

Thanks again!

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