Open NEWEST file in a directory

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Blazko
Posts: 14
Joined: 24 Nov 2018, 05:00

Open NEWEST file in a directory

26 Nov 2018, 05:16

Hello,
I want to make a hotkey that opens the NEWEST file in a given directory. Actually I already have a code for this but its not very elegant and has some caveats: I just have the Explorer window open with the files ordered by date and the newest file first. Then the hotkey will perform a doubleclick on the newest file and it opens.
It works reliably but the disadvantage is that I always have to have the Explorer window open and on top. And also the speed of it is not optimal. I know that you can open a document file (in my case pictures) with the "Run" command. But I havent found any way to tell AHK to open the NEWEST file in that directory. Is there a parameter I can add to the Run command? Or use some other command?
Thanks for answers. :-)
hymal7
Posts: 66
Joined: 14 Sep 2016, 05:37

Re: Open NEWEST file in a directory

26 Nov 2018, 07:55

assign your folder location

Code: Select all

Folder=C:\Users\...\Desktop
getlastfolder(folder, file)
run %folder%/%file%


getlastfolder(byref Folder,byref file="")
	{
	Loop, %Folder%\*
		{
		FileGetTime, Time, %A_LoopFileFullPath%, C
		If (Time > Time_Orig)
			{
			Time_Orig := Time
			File := A_LoopFileName
			}
		}
	}
;#######################################################################
exitapp
esc::ExitApp
Blazko
Posts: 14
Joined: 24 Nov 2018, 05:00

Re: Open NEWEST file in a directory

26 Nov 2018, 14:19

Thanks for the code. Will take some time for me to try it out because there are a lot of new things for me in it which i like to look up in the help file. Will tell you if i succeeded.
hymal7
Posts: 66
Joined: 14 Sep 2016, 05:37

Re: Open NEWEST file in a directory

27 Nov 2018, 04:56

OK good.
for now, you can try to replace "C:\Users\...\Desktop" with your folder location. and this AHK script is ready to go. you dont have to modify the rest of it.
"getlastfolder" is a custom function.
Blazko
Posts: 14
Joined: 24 Nov 2018, 05:00

Re: Open NEWEST file in a directory

27 Nov 2018, 10:26

Ah ok, good to know - I thought I have to replace the word "folder" whereever it occurs...
But still I will check all that up in the Help file because I want to learn more about the language. :-)
Blazko
Posts: 14
Joined: 24 Nov 2018, 05:00

Re: Open NEWEST file in a directory

28 Nov 2018, 05:55

Ok, I changed the Folder and the routine is working perfectly alright now. So far thanks for the code. But now I have another problem: As I said earlier, the routine is for opening pictures in that folder and so Win10 automatically launches the Photos-App when it opens a picture file. With my old code (when I launched the file via the file explorer with a mouse click) I was able to browse through the pictures of that folder with either the mousewheel or the arrow keys left and right. But now, when using your routine, this browsing is not possible anymore; it just opens the newest file in that folder but it seems that the photosApp does not "know" that there are other pictures in that folder and thus browsing is not possible. Is there a solution for that? Thanks for your effort
hymal7
Posts: 66
Joined: 14 Sep 2016, 05:37

Re: Open NEWEST file in a directory

28 Nov 2018, 06:17

hmm..it works on my PC.
what photsApp do you use?
I've got Windows Photo Viewer. If you dont have this, change this to your default
Blazko
Posts: 14
Joined: 24 Nov 2018, 05:00

Re: Open NEWEST file in a directory

15 Dec 2018, 08:34

Hi, took some time because i had other things to do. But now works fine. I have indeed changed to the Windows Photo Viewer now. thanks!
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Open NEWEST file in a directory

16 Dec 2018, 04:51

I know this topic is fairly solved.... I just thought i'd add a simpler function for anyone who comes looking.

Code: Select all


MsgBox % GetNewestFile("C:")	;most recently created file
MsgBox % GetNewestFile("C:", "M")	;most recently modified file



GetNewestFile(path, CreatedOrModified:="c"){
	Loop, %path%\*
	{
		FileGetTime, Time, %A_LoopFileFullPath%, % CreatedOrModified
		If (Time > Time_Orig){
			Time_Orig := Time
			file := A_LoopFileFullPath
		}
	}
Return file
}

live ? long & prosper : regards
Permagrin
Posts: 22
Joined: 27 Oct 2015, 15:05

Re: Open NEWEST file in a directory

29 Mar 2021, 13:39

I know this was solved years ago but here is my version.

Code: Select all

runwait,  %COMSPEC% /c dir /b /a-d /o-d "C:\YourFolder" > %A_scriptdir%\filelist.txt ,,hide ; Get Dir of folder and sort by newest file
filereadline , NewestFile, %A_scriptdir%\filelist.txt , 1 ; First line of this will be the newest file.
For posterity.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 248 guests