Find newest txt file in directory

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Find newest txt file in directory

23 Jan 2017, 05:24

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?
ManualColdLock
Posts: 175
Joined: 15 Dec 2016, 04:27

Re: Find newest txt file in directory

23 Jan 2017, 06:47

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
ManualColdLock
Posts: 175
Joined: 15 Dec 2016, 04:27

Re: Find newest txt file in directory

23 Jan 2017, 06:49

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
just me
Posts: 9451
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Find newest txt file in directory

23 Jan 2017, 08:03

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%
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Find newest txt file in directory

23 Jan 2017, 09:52

Thanks "ManualColdLock" and "just me".
I've taken the version of "just me" because it's smaller ;)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Find newest txt file in directory

23 Jan 2017, 11:43

I would check for A_LoopFileTimeModified rather than
A_LoopFileTimeAccessed, just thought that someone should mention that!
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Find newest txt file in directory

23 Jan 2017, 11:53

@jeeswg
already done but didn't mention it ;)

Thanks for the reminder.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Find newest txt file in directory

23 Jan 2017, 12:21

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Find newest txt file in directory

23 Jan 2017, 13:49

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.
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Find newest txt file in directory

23 Jan 2017, 15:57

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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], mikeyww, Nerafius, scriptor2016 and 95 guests