Delete Files except last 5 latest files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Geek9
Posts: 27
Joined: 26 Apr 2016, 17:04

Delete Files except last 5 latest files

13 Nov 2016, 02:35

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.
System Specs: Core-I3 16 GB RAM Windows 8.1 Pro IE 11 MS Office 2007 AHK Version: 1.1.(Latest Version)
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Delete Files except last 5 latest files

13 Nov 2016, 03:11

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.
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Delete Files except last 5 latest files  Topic is solved

13 Nov 2016, 03:21

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%
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Delete Files except last 5 latest files

13 Nov 2016, 03:33

Try with a custom sort function:

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
}
I hope that helps.
User avatar
Geek9
Posts: 27
Joined: 26 Apr 2016, 17:04

Re: Delete Files except last 5 latest files

13 Nov 2016, 03:42

Thanks for All of you guys help. I will test it out and i will let you know the outcome.

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)
User avatar
Geek9
Posts: 27
Joined: 26 Apr 2016, 17:04

Re: Delete Files except last 5 latest files

13 Nov 2016, 03:53

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. :o :o :o

Thanks.
System Specs: Core-I3 16 GB RAM Windows 8.1 Pro IE 11 MS Office 2007 AHK Version: 1.1.(Latest Version)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3 and 340 guests