Finding newest file and copy its contents

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ness
Posts: 6
Joined: 20 May 2023, 14:18

Finding newest file and copy its contents

Post by ness » 31 May 2023, 19:03

Let me begin by explaining how the folder structure looks like:

Code: Select all

mainfolder
    auto.log
    subfolder1
        -auto.log
    subfolder2
        -auto.log
    subfolder3
        -auto.log
The "auto.log" file in the main folder is manually created (not by the program).
I need to find the newest updated file with name "auto.log" inside any of the subfolders (which names are almost random) and copy the contents into the "auto.log" in the main folder.

The "auto.log" file keeps updating (so it's needed to copy and replace the contents into our main "auto.log" file periodically (maybe every few seconds), but at some point a new subfolder will be created with a new "auto.log" file.
So the ahk script will need to keep checking for newer files and copy the contents into the main "auto.log".

I hope I explained well and someone can help me. Thanks

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Finding newest file and copy its contents

Post by mikeyww » 31 May 2023, 20:13

Code: Select all

#Requires AutoHotkey v1.1.33
#Persistent
dir    := A_ScriptDir "\mainfolder"
target := dir "\auto.log"
Process Priority,, B
SetTimer Update, 2000
Update:
newestTime := newest := ""
Loop Files, % target, R
 If (A_LoopFileDir != dir && A_LoopFileTimeModified > newestTime)
  newestTime := A_LoopFileTimeModified, newest := A_LoopFilePath
FileGetTime currentTime, % target
If (newestTime > currentTime) {
 FileCopy % newest, % target, OVERWRITE := True
 If ErrorLevel
  MsgBox 48, Error, An error occurred while copying the file.
 SoundBeep 2500
}
Return

ness
Posts: 6
Joined: 20 May 2023, 14:18

Re: Finding newest file and copy its contents

Post by ness » 01 Jun 2023, 06:12

mikeyww wrote:
31 May 2023, 20:13

Code: Select all

#Requires AutoHotkey v1.1.33
#Persistent
dir    := A_ScriptDir "\mainfolder"
target := dir "\auto.log"
Process Priority,, B
SetTimer Update, 2000
Update:
newestTime := newest := ""
Loop Files, % target, R
 If (A_LoopFileDir != dir && A_LoopFileTimeModified > newestTime)
  newestTime := A_LoopFileTimeModified, newest := A_LoopFilePath
FileGetTime currentTime, % target
If (newestTime > currentTime) {
 FileCopy % newest, % target, OVERWRITE := True
 If ErrorLevel
  ;MsgBox 48, Error, An error occurred while copying the file.
 ;SoundBeep 2500
}
Return
Question
How do I add an absolute path for the main folder? I want to put the .ahk file in another location.

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Finding newest file and copy its contents

Post by mikeyww » 01 Jun 2023, 06:29

Line 3 defines an absolute path. You can change it to whatever you like.

ness
Posts: 6
Joined: 20 May 2023, 14:18

Re: Finding newest file and copy its contents

Post by ness » 01 Jun 2023, 06:47

mikeyww wrote:
01 Jun 2023, 06:29
Line 3 defines an absolute path. You can change it to whatever you like.
Oh. I thought it was relative path only.
Thank you.

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Finding newest file and copy its contents

Post by mikeyww » 01 Jun 2023, 07:07

A way to know:

Code: Select all

#Requires AutoHotkey v1.1.33
dir    := A_ScriptDir "\mainfolder"
MsgBox % dir

Post Reply

Return to “Ask for Help (v1)”