Delete files AND folders older then 7 days

Discussion, questions and support.
Post Reply
User avatar
Ennovy
Posts: 70
Joined: Mar 18th, ’14, 08:46

Post by Ennovy » Jul 6th, ’15, 11:52

Hi Marko
In the AHK forum I found a script that enables me to delete files older then 7 days (not tested yet)

Code: Select all

StringLeft, Now_Date, A_Now, 8
;
Loop, %TEMP%\*
{
StringLeft, File_date, A_LoopFileTimeCreated, 8
If (Now_Date - File_date >= 7)
 FileDelete, %A_LOOPFILEFULLPATH%
 ; FileRecycle, %A_LOOPFILEFULLPATH%
}
I want to check my temp folder for files AND folders that are older then 7 days.
Can you please give me a hint on how to do that?

Thanks in advance for your support
Peter
User avatar
Marko
Posts: 1719
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Jul 6th, ’15, 16:09

Hi, not tested but it should be something like this (based on your method):

Code: Select all

StringLeft, Now_Date, A_Now, 8
Loop, %TEMP%\*, 1, 1
{
	StringLeft, File_date, A_LoopFileTimeCreated, 8
	If (Now_Date - File_date >= 7)
	{
		If InStr(A_LoopFileAttrib, "D")  ;is folder?
			FileDelete, %A_LoopFileFullPath%
		Else
			FileRemoveDir, %A_LoopFileFullPath%, 1
	}
}
return
This will also delete folders that were created more that 7 day ago. I'm not sure you really want this as there might be some newer files in such folders. Maybe it would be better to have two loops, first to delete all old files and then all empty folders.

For more information you should look at:
http://ahkscript.org/docs/commands/LoopFile.htm (see options Include Folders and Recurse)
http://ahkscript.org/docs/commands/FileRemoveDir.htm
User avatar
Ennovy
Posts: 70
Joined: Mar 18th, ’14, 08:46

Post by Ennovy » Jul 6th, ’15, 16:28

Thank you Marko for your information :!:

I want to cleanup my Temp folder once a week and I presume that files older then one week are no longer needed and can be safely erased.
Good idea to make a seperate loop for the removal of empty folders.

FastKeys and AHK are such great tools :D

Have a nice day ;)
Post Reply