Page 1 of 1

Find newest txt file in directory

Posted: 23 Jan 2017, 05:24
by ozzii
Hi,

How can I find the newest txt file in an folder.
with Loop, Files, c:\tmp\*.txt the files are not in the right order that I need.

So, how can I do to have the newest txt file from the folder ?
Surely something with A_LoopFileTimeAccessed. But I don't know how to do it.
Maybe in an array and sort it?

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 06:47
by ManualColdLock
This isn't pretty but it works

Code: Select all

AccessedArray := []
Loop, Files, c:\tmp\*.txt 
{
	Accessed .= A_LoopFileTimeAccessed "`n"
	AccessedArray[A_LoopFileTimeAccessed] := A_LoopFileFullPath 
}

Sort, Accessed,N 

Loop, Parse,Accessed,`n
	output .= AccessedArray[A_LoopField] "`n"

msgbox, % output

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 06:49
by ManualColdLock
Here it is a bit fancier.

Code: Select all

AccessedArray := []
Loop, Files, c:\tmp\*.txt 
{
	FormatTime, Formattedtime,A_LoopFileTimeAccessed, MM/dd/yy 'at' hh:mm:ss tt 
	Accessed .= A_LoopFileTimeAccessed "`n"
	AccessedArray[A_LoopFileTimeAccessed] := A_LoopFileFullPath " was last accessed on " Formattedtime 
}

Sort, Accessed,NR  backwards

Loop, Parse,Accessed,`n
	output .= AccessedArray[A_LoopField] "`n"

msgbox, % output

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 08:03
by just me
How can I find the newest txt file in an folder.

Code: Select all

LastAccess := 0
Loop, Files, c:\tmp\*.txt
{
   If (A_LoopFileTimeAccessed > LastAccess) {
      LastAccess := A_LoopFileTimeAccessed
      NewestFile := A_LoopFileLongPath
   }
}
MsgBox, %NewestFile%

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 09:52
by ozzii
Thanks "ManualColdLock" and "just me".
I've taken the version of "just me" because it's smaller ;)

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 11:43
by jeeswg
I would check for A_LoopFileTimeModified rather than
A_LoopFileTimeAccessed, just thought that someone should mention that!

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 11:53
by ozzii
@jeeswg
already done but didn't mention it ;)

Thanks for the reminder.

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 12:21
by jeeswg
Hahaha cool. Thanks for letting me know.

[EDIT:]
Btw for C:\Users\%A_UserName%\Pictures,
I would use A_LoopFileTimeCreated,
because when you save an image via Internet Explorer,
I believe the modified date is the date the image was downloaded to IE's cache,
whereas the created date is the date when you saved the file.

Also I often skip these files when listing files in a folder:
if (vName = "desktop.ini") OR (vName = "Thumbs.db")
continue

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 13:49
by Almost_there
The "created date" is the time when the file was created (obvious). It differs from "modified date" in the way that it doesn't change when file is overwritten (i.e. you open a file in an editor and hit Save).
Also - if you copy the file to an new location - the "created date" will be set to the time the new copy was made.

Re: Find newest txt file in directory

Posted: 23 Jan 2017, 15:57
by ozzii
For me I want the newest file. Non depending of wich time is taken.
So something like a 'ls -lrt' in unix.
This is why I think that the modified time suit my need.

But if anyone have a better/safer way for finding the newer file.
But my need is not so 'complicated' because I need to open that file from 'lotus notes', so it will always be here and at least 5 seconds ago.