Page 1 of 1

Delete files AND folders older then 7 days

Posted: Jul 6th, ’15, 11:52
by Ennovy
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

Re: Delete files AND folders older then 7 days

Posted: Jul 6th, ’15, 16:09
by Marko
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

Re: Delete files AND folders older then 7 days

Posted: Jul 6th, ’15, 16:28
by Ennovy
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 ;)