Sign in to follow this  
Followers 0
locutus243

Finding the latest file

6 posts in this topic

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

Share this post


Link to post
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)

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
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

Share this post


Link to post
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...

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
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

Share this post


Link to post
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,

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

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