About Loop files order
About Loop files order
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
How can I scan a directory and make sure I get the name of the newest file
Re: About Loop files order
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
Re: About Loop files order
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
But today,this was getting the newest file
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
Re: About Loop files order
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"?
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.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 fileBut today,this was getting the newest fileCode: Select all
Loop,Files,X:\backups\*.json File := A_LoopFileLongPath
Re: About Loop files order
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.
Re: About Loop files order
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
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
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
I want the script to limit the files to 5 in both folders and only copy the newest file
Re: About Loop files order
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.
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