About Loop files order

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

About Loop files order

Post by vsub » 01 Aug 2021, 09:06

Does the function scan a directory in some order(created\modified\\name)
How can I scan a directory and make sure I get the name of the newest file

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: About Loop files order

Post by boiler » 01 Aug 2021, 09:27

It loops through the files based on a sort of the file names. You can loop through all the files and get their time modified/created/accessed using FileGetTime, keeping the one that's most recent. For example:

Code: Select all

LatestTime := 0
loop, Files, C:\MyPath\*.*
{
	FileGetTime, CurrentTime, % A_LoopFileFullPath, M ; modification time
	if (CurrentTime > LatestTime) {
		LatestFilePath := A_LoopFileFullPath
		LatestFileName := A_LoopFileName
		LatestTime := CurrentTime
	}
}
MsgBox, % "File path:`t" LatestFilePath "`nFile name:`t" LatestFileName "`nModified:`t" LatestTime

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: About Loop files order

Post by vsub » 01 Aug 2021, 10:50

The file name contains the time it was created in this format
Only the date is changed in every file name
Yesterday I keep getting a notification that no file is copied and then I find out that this was not getting the newest file

Code: Select all

Loop,Files,X:\backups\*.json
File := A_LoopFileLongPath
But today,this was getting the newest file :?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: About Loop files order

Post by boiler » 01 Aug 2021, 11:02

vsub wrote: The file name contains the time it was created in this format
This is a key piece of information to not mention before so I didn't write the above code that answered your question but you weren't even planning to use. :? What were you expecting when you asked "How can I scan a directory and make sure I get the name of the newest file"?
vsub wrote: Yesterday I keep getting a notification that no file is copied and then I find out that this was not getting the newest file

Code: Select all

Loop,Files,X:\backups\*.json
File := A_LoopFileLongPath
But today,this was getting the newest file :?
What gave you that message? Your script? What do you mean no file was copied? Is it always supposed to copy the last file found by the two lines posted above? Then it could have been something like there was already a file of that name where you tried to copy it to, so it couldn't copy it. You might have to show the whole script and the contents of both directories when it copied it and when it didn't copy it.

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: About Loop files order

Post by braunbaer » 01 Aug 2021, 11:21

Loop files gets the filenames in the order that the operating system gives the filenames to the script. I don't think ahk makes a sorting of its own. The operating system is not guaranteed to follow a specific sort order, it depends on the windows version and on the file system used. So you can just use the script that boiler has posted in his first answer to find the latest file.

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: About Loop files order

Post by vsub » 01 Aug 2021, 11:29

Sorry that i didn't mention the file name but my question at first was at what order AHK is scanning the files
if it is by file name,then for some reason,this simple script I posted(and I use)was giving me different result yesterday

This is the whole script(maybe there is some unnecessary code or maybe there is something wrong

Code: Select all

Backup:
IfNotExist,X:\Backups
{
SetTimer,Backup,-3600000
Return
}
Loop,X:\Backups\*.json
{
Count := A_Index
If Count > 9
{
Loop,X:\Backups\*.json
{
If A_Index = 6
Break
FileDelete,% A_LoopFileLongPath
}
Break
}
}
Loop,X:\Backups\*.json
File := A_LoopFileLongPath

Loop,D:\Data\Backups\*.json
Count := A_Index
If Count > 5
{
Loop,D:\Data\Backups\*.json
{
If A_Index = 3
Break
FileDelete,% A_LoopFileLongPath
}
}
FileCopy,% File,D:\Data\Backups
SetTimer,Backup,-3600000
Another program is creating a file every hour(if it's running)but there is no limit to how many files the program will create so I am using the script do limit it to the folders(input and destination)to 5 files

I want the script to limit the files to 5 in both folders and only copy the newest file

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: About Loop files order

Post by boiler » 01 Aug 2021, 12:55

From what I see, you are deleting the older backups, and then you copy the latest file to the backup folder. I don't see what keeps it from trying to copy the same file that has already been copied (and its backup would of course still be there since it's the latest and you're deleting only the older versions), so that would explain why there isn't a file copied every time (you did not tell the FileCopy command to overwrite existing files).

The version below is one I indented for my (and anyone else's) reference because trying to follow the non-indented version is a real pain.

Code: Select all

Backup:
IfNotExist,X:\Backups
{
	SetTimer,Backup,-3600000
	Return
}
Loop,X:\Backups\*.json
{
	Count := A_Index
	If Count > 9
	{
		Loop,X:\Backups\*.json
		{
			If A_Index = 6
				Break
			FileDelete,% A_LoopFileLongPath
		}
		Break
	}
}
Loop,X:\Backups\*.json
	File := A_LoopFileLongPath

Loop,D:\Data\Backups\*.json
	Count := A_Index
If Count > 5
{
	Loop,D:\Data\Backups\*.json
	{
		If A_Index = 3
			Break
		FileDelete,% A_LoopFileLongPath
	}
}
FileCopy,% File,D:\Data\Backups
SetTimer,Backup,-3600000

Post Reply

Return to “Ask for Help (v1)”