Hi,
I need help with creating a script for deleting the files in a folder. I am familiar with deleting files BUT I am need to delete all the files EXCEPT 5 Latest files.
How can I achieve it?
Thanks.
Delete Files except last 5 latest files Topic is solved
Delete Files except last 5 latest files
System Specs: Core-I3 16 GB RAM Windows 8.1 Pro IE 11 MS Office 2007 AHK Version: 1.1.(Latest Version)
Re: Delete Files except last 5 latest files
Code: Select all
FileList := ""
Loop, FolderPath\*.*
; create a list of those files consisting of the time the file was created und the file name separated by tab
FileList = %FileList%%A_LoopFileTimeCreated%`t%A_LoopFileName%`n
Sort, FileList, R ; sort the list by time created in reverse order
Loop, parse, FileList, `n,`r
{
if A_LoopField =
continue
; Split the list items into two parts at the tab character
StringSplit, FileItem, A_LoopField, %A_Tab%
; FileItem1 is FileTimeCreated and FileItem2 is FileName
If not ErrorLevel
{
If (A_Index > 5)
FileRecycle, FolderPath\%FileItem2%
}
}
return
Last edited by GEV on 13 Nov 2016, 03:36, edited 2 times in total.
Re: Delete Files except last 5 latest files Topic is solved
Code: Select all
Loop, Files, c:\temp\*.*
list .= A_LoopFileTimeModified ":" A_LoopFileFullPath "`n"
Sort, list, NR
list := RegExReplace(list,"^(.*?\n){5}|\d+?:")
loop, parse, list, `n
if A_LoopField
FileDelete, %A_LoopField%
Re: Delete Files except last 5 latest files
Try with a custom sort function:
I hope that helps.
Code: Select all
DeleteAllFiles_ExceptFiveLatest(A_Temp)
;-------------------------------------------------------------------------------
DeleteAllFiles_ExceptFiveLatest(Folder) {
;-------------------------------------------------------------------------------
; get a list of all the files
Loop, Files, %Folder%\*.*
FileList .= A_LoopFileFullPath "`n"
; sort the list by date
Sort, FileList, f Sort_by_Date
; delete all exept 5 latest files
Loop, Parse, FileList, `n, `r
If (A_Index > 5)
FileDelete, %A_LoopField%
}
;-------------------------------------------------------------------------------
Sort_by_Date(File1, File2) { ; custom sorting function
;-------------------------------------------------------------------------------
FileGetTime, Time1, %File1%
FileGetTime, Time2, %File2%
If (Time1 < Time2)
Return, 1
Else If (Time1 = Time2)
Return, 0
Else ; Time1 > Time2
Return, -1
}
Re: Delete Files except last 5 latest files
Thanks for All of you guys help. I will test it out and i will let you know the outcome.
Again thanks a lot.
Again thanks a lot.
System Specs: Core-I3 16 GB RAM Windows 8.1 Pro IE 11 MS Office 2007 AHK Version: 1.1.(Latest Version)
Re: Delete Files except last 5 latest files
Thanks for your response guys.
I have tested all the suggested solution and everything is working as expected.
This is first time I got the resolution this fast.
Thanks.
I have tested all the suggested solution and everything is working as expected.
This is first time I got the resolution this fast.
Thanks.
System Specs: Core-I3 16 GB RAM Windows 8.1 Pro IE 11 MS Office 2007 AHK Version: 1.1.(Latest Version)
Who is online
Users browsing this forum: ht55cd3 and 340 guests