Jump to content

Finding the latest file


Recommended Posts

Hi guys,

I think this is a very simple problem but I've experimented with FindNextFile and can't seem to work it out.

I have a folder which has Excel files, a new file is put in their daily. I want my autoIT script to open up the most recently created file (when a button is pressed in a GUI, although this seems the easy bit to me!)

I assume something can be done through the FileGetTime Function but I'm not sure how to get my script to run through the files in the folder doing this???

Any help would be muhc appreciated!!

Thanks

Mark

Link to comment
Share on other sites

Hi guys,

I think this is a very simple problem but I've experimented with FindNextFile and can't seem to work it out.

I have a folder which has Excel files, a new file is put in their daily. I want my autoIT script to open up the most recently created file (when a button is pressed in a GUI, although this seems the easy bit to me!)

I assume something can be done through the FileGetTime Function but I'm not sure how to get my script to run through the files in the folder doing this???

Any help would be muhc appreciated!!

Thanks

Mark

it sounds like you've got it figured out for the most part, except the FileFindFirstFile() and FileFindNextFile() here's a sample....

$folder = "c:\"
$search = FileFindFirstFile($folder & "*.xls")
$now = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC 
$oldest = ""
While 1
    $file = FileFindNextFile($search)
if @error Then ExitLoop
    if FileGetTime($folder & $file,0,1) < $now Then 
        $now = FileGetTime($folder & $file,0,1)
        $oldest = $folder & $file
    EndIf
WEnd
MsgBox(0,"Oldest",$oldest & " is your oldest file, created : " & $now)
Link to comment
Share on other sites

i have been on this way toooooo long

fileGetTime doesnt seem to work on xls... and some other files like .doc

$folder = @MyDocumentsDir 
$search = FileFindFirstFile($folder & "*.xls")
$now = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC
$oldest = ""
While 1
    $file = FileFindNextFile($search)
    if @error Then ExitLoop
    if FileGetTime($folder & $file,0,1) < $now Then
        $now = FileGetTime($folder & $file,0,1)
        $oldest = $folder & $file
    EndIf
WEnd
MsgBox(0,"Oldest",$oldest & " is your oldest file, created : " & $now)

i only went after this because the poster asked for "the most recent file"

so i did testing

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
$Location = @MyDocumentsDir 

$FileList=_FileListToArray($Location, "*.doc", 1)
_ArrayDisplay($FileList,"My Documents")
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

$newest = FileGetTime($FileList[1],0,1)
MsgBox(64,"First File", "File: " & $FileList[1] & @CRLF & "Info: " & $newest)
$newname = "(None)"
For $x = 2 to $FileList[0]
    if FileGetTime($FileList[$x],0,1) > $newest Then
        $newest = FileGetTime($FileList[$x],0,1)
        $newname = $FileList[$x]
    ;MsgBox(64,"Newest", $newest)
    EndIf
    
Next
MsgBox(64,"Newest",$newname & " is your most recent file   " & @CRLF & "created : " & $newest)

?????

8)

NEWHeader1.png

Link to comment
Share on other sites

i have been on this way toooooo long

fileGetTime doesnt seem to work on xls... and some other files like .doc

$folder = @MyDocumentsDir 
$search = FileFindFirstFile($folder & "*.xls")
$now = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC
$oldest = ""
While 1
    $file = FileFindNextFile($search)
    if @error Then ExitLoop
    if FileGetTime($folder & $file,0,1) < $now Then
        $now = FileGetTime($folder & $file,0,1)
        $oldest = $folder & $file
    EndIf
WEnd
MsgBox(0,"Oldest",$oldest & " is your oldest file, created : " & $now)

i only went after this because the poster asked for "the most recent file"

so i did testing

#include <GUIConstants.au3>
#Include <File.au3>
#Include <Array.au3>
$Location = @MyDocumentsDir 

$FileList=_FileListToArray($Location, "*.doc", 1)
_ArrayDisplay($FileList,"My Documents")
If (Not IsArray($FileList)) and (@Error=1) Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

$newest = FileGetTime($FileList[1],0,1)
MsgBox(64,"First File", "File: " & $FileList[1] & @CRLF & "Info: " & $newest)
$newname = "(None)"
For $x = 2 to $FileList[0]
    if FileGetTime($FileList[$x],0,1) > $newest Then
        $newest = FileGetTime($FileList[$x],0,1)
        $newname = $FileList[$x]
;MsgBox(64,"Newest", $newest)
    EndIf
    
Next
MsgBox(64,"Newest",$newname & " is your most recent file   " & @CRLF & "created : " & $newest)

?????

8)

it worked for me, except i messed up my code and grabbed the oldest file instead of the newest, but i tested my code and it does work to find the oldest... one thing that i had to do to get a result was add the path $folder to the FileGetTime(), i've tried doing it with FileGetLongName($file) but that didn't work...
Link to comment
Share on other sites

Hey Cameron,

The script worked a treat for me except it finds the oldest file. I've looked at it and tried to work out how I can alter it so that its the most recently created file that I get but I don't seem to be able to manipulate it that way...any ideas???

Thanks for your response Valuater...I tried your script but cannot seem to get it to work, I think its because of my inadequacies using the Array function (its something I've never used before!) So it might be looking at the wrong file paths or something which could be why its failing.

Any help much appreciated

Cheers

Mark

Link to comment
Share on other sites

Hey Cameron,

The script worked a treat for me except it finds the oldest file. I've looked at it and tried to work out how I can alter it so that its the most recently created file that I get but I don't seem to be able to manipulate it that way...any ideas???

Thanks for your response Valuater...I tried your script but cannot seem to get it to work, I think its because of my inadequacies using the Array function (its something I've never used before!) So it might be looking at the wrong file paths or something which could be why its failing.

Any help much appreciated

Cheers

Mark

just switch the < (in the 8th line of code) to >, and change the assignment for $now to 0. that way when it's called, the date of the first file will be compared to 0, will be greater, and each newer file after that will be saved to the $oldest (sorry the variable names will be all confused since i made it initially to find the oldest) and will be output at the end,
Link to comment
Share on other sites

  • 11 years later...
#include <Array.au3>
#include <File.au3>

Local $sFilePath = @MyDocumentsDir
If @error Then Exit
Local $FileList = _FileListToArrayRec($sFilePath, "*.xlsx", 1, 0, 0, 2)


For $x = 2 to $FileList[0]
    if FileGetTime($FileList[$x],1,1) > $newest Then
        $newest = FileGetTime($FileList[$x],1,1)
        $newname = $FileList[$x]
;MsgBox(64,"Newest", $newest)
    EndIf

Next
MsgBox(64,"Newest",$newname & " is your most recent file   " & @CRLF & "created : " & $newest)

here is a modification to your code hopefully , it can help someone you just need to add  1  for most recent  date-time  for creation or 0  for date-time modified.

  The path to the file or directory to check.
option [optional] Flag to indicate which timestamp
    $FT_MODIFIED (0) = Last modified (default)
    $FT_CREATED (1) = Created
    $FT_ACCESSED (2) = Last accessed

Constants are defined in FileConstants.au3
Link to comment
Share on other sites

Here is a simple function to return either an array sorted on the FT.. Flags or Filename

#include <Array.au3>
#include <File.au3>

Local $sLatestFileCreated = _FileVersion(@MyDocumentsDir, "*.xlsx", 1, False)
MsgBox(64, "Latest Created File Version", $sLatestFileCreated)
Local $aLatestFileCreated = _FileVersion(@MyDocumentsDir, "*.xlsx", 1, True)
_ArrayDisplay($aLatestFileCreated)

; #FUNCTION# ====================================================================================================================
; Name ..........: _FileVersion
; Description ...: 
; Syntax ........: _FileVersion($sFilePath[, $sMask = "*.*"[, $iFlag = 0[, $bFormat = True]]])
; Parameters ....: $sFilePath           - Folder Path to Search
;                  $sMask               - [optional] File Mask. Default is "*.*".
;                  $iFlag               - [optional] Flag to indicate which timestamp
;                                       -     $FT_MODIFIED (0) = Last modified (default)
;                                       -     $FT_CREATED (1) = Created
;                                       -     $FT_ACCESSED (2) = Last accessed
;                  $bFormat             - [optional] To specify type of return
;                                       -     True = Retrun an Array (default)
;                                       -     False = Return latest Filename for iFlag Type
; Return values .: An array or string
; Author ........: Subz
; Modified ......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _FileVersion($sFilePath, $sMask = "*.*", $iFlag = 0, $bFormat = True)
    Local $aFileList = _FileListToArrayRec($sFilePath, $sMask, 1, 0, 0, 2)
        If @error Then Return 0
    Local $aFileVersion[0][4]
        _ArrayAdd($aFileVersion, UBound($aFileList) - 1 & "|Modified|Created|Accessed")
    For $i = 1 to $aFileList[0]
        _ArrayAdd($aFileVersion, $aFileList[$i] & "|" & FileGetTime($aFileList[$i], 0, 1) & "|" & FileGetTime($aFileList[$i], 1, 1) & "|" & FileGetTime($aFileList[$i], 2, 1))
    Next
    Switch $iFlag
        Case 1
            _ArraySort($aFileVersion, 1, 1, 0, 2)
        Case 2
            _ArraySort($aFileVersion, 1, 1, 0, 3)
        Case Else
            _ArraySort($aFileVersion, 1, 1, 0, 1)
    EndSwitch
    Return $bFormat = True ? $aFileVersion : $aFileVersion[1][0]
EndFunc

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...