Select Most Recent File in a Folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
3ggg
Posts: 31
Joined: 11 Nov 2019, 19:52

Select Most Recent File in a Folder

25 Nov 2019, 21:07

I'm trying to rename a file a specific file ( which I know how to do ) but having trouble figuring out how to select the file path of the most recent file in a given folder. How do I do this?
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Select Most Recent File in a Folder

25 Nov 2019, 23:32

By @SKAN https://autohotkey.com/board/topic/59408-get-the-newest-file-in-a-folder/

Code: Select all

Loop %A_WinDir%\*.*               
 If ( A_LoopFileTimeModified >= Time )
    Time := A_LoopFileTimeModified, File := A_LoopFileLongPath

MsgBox, Lastest file in %A_WinDir% is `n`n%File%
3ggg
Posts: 31
Joined: 11 Nov 2019, 19:52

Re: Select Most Recent File in a Folder

26 Nov 2019, 02:07

@AHKStudent

Do you know what I'm doing wrong here? Here I keep on getting the most recent file is C:\windows\bootstat.dat.

Code: Select all

^o::
A_WinDR := "C:\Users\myname\Desktop\foldername\subfoldername"
Loop %A_WinDir%\*.*               
 If ( A_LoopFileTimeModified >= Time )
    Time := A_LoopFileTimeModified, File := A_LoopFileLongPath

MsgBox, Lastest file in %A_WinDir% is `n`n%File%
Return
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Select Most Recent File in a Folder

26 Nov 2019, 02:13

from the docu:
Built-in Variables

A number of useful variables are built into the program and can be referenced by any script. With the exception of Clipboard, ErrorLevel, and command line parameters, these variables are read-only; that is, their contents cannot be directly altered by the script.
Hubert
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Select Most Recent File in a Folder

26 Nov 2019, 02:13

3ggg wrote:
26 Nov 2019, 02:07
@AHKStudent

Do you know what I'm doing wrong here? Here I keep on getting the most recent file is C:\windows\bootstat.dat.

Code: Select all

^o::
A_WinDR := "C:\Users\myname\Desktop\foldername\subfoldername"
Loop %A_WinDir%\*.*               
 If ( A_LoopFileTimeModified >= Time )
    Time := A_LoopFileTimeModified, File := A_LoopFileLongPath

MsgBox, Lastest file in %A_WinDir% is `n`n%File%
Return
maybe its a hidden file in that directory that is the most recent.

Try copying and pasting something new to that directory and run it again.
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Select Most Recent File in a Folder

26 Nov 2019, 02:17

Code: Select all

Loop %A_WinDR%\*.*
Hubert
3ggg
Posts: 31
Joined: 11 Nov 2019, 19:52

Re: Select Most Recent File in a Folder

26 Nov 2019, 02:49

@AHKStudent

tried with multiple different folders, moving things around, still got that boot.stat thing every time. I'm sure I'm getting something wrong
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Select Most Recent File in a Folder  Topic is solved

26 Nov 2019, 03:36

@3ggg

There are at least two problems:

* A typo. You have A_WinDR (it seems you mean A_WinDir)
* You can't use A_WinDir (or any another built-in variable) as an output variable.

And by the way, the name like WinDir is just misleading here, to my opinion. I changed it to Foo.

Code: Select all

^o::
  Foo := A_Desktop . "\Test" ; C:\Users\John\Desktop\Test
  Loop %Foo%\*.*               
   If ( A_LoopFileTimeModified >= Time )
     Time := A_LoopFileTimeModified, File := A_LoopFileLongPath

  MsgBox, Lastest file in %Foo% is `n`n%File%
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: inseption86, mikeyww and 456 guests