WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Files and Directories

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Delete File Folder if Older Than ...

 Keywords: Delete remove older than filedelete 

Remove File \ Folder, if older than XX days, months or years

Deletes entire directory or single file, depending on whats passed, based on a specified amount of time.

You can also specify whether you want to delete, based on the Creation, Modifed or Last Accessed date.

#DefineFunction RemoveIfOlderThan(path, howold, dateflag, timefield)
   ;Date-flags
	;-----------
	;days = 1
	;months = 2
	;years = 3

	;Time-field
	;-----------
	;Created = 1
	;Modified= 2
	;Last Accessed= 3

	today = TimeYmdHms()
	dirmoddate = FileTimeGetEx(path,timefield)
	diff = TimeDiff(today, dirmoddate)
	year =  ItemExtract(1,diff,":")
	month =   ItemExtract(2,diff,":")
	day =  ItemExtract(3,diff,":") 
	answer = @NO
	Switch dateflag
	Case 1
		if year>0 || month>0 || day>HowOld
		   date = "Day(s)"
			answer = AskYesNo('Older than %howold% %date%',StrCat('Are you sure you want to delete ','"',path,'"'))
		endif
		break
	Case 2
		if year>0 || month>HowOld || day>0
		   date = "Month(s)"
  			answer = AskYesNo('Older than %howold% %date%',StrCat('Are you sure you want to delete ','"',path,'"'))
		endif
		break
	Case 3
		if year>HowOld || month>0 || day>0
			date = "Year(s)"
			answer = AskYesNo('Older than %howold% %date%',StrCat('Are you sure you want to delete ','"',path,'"'))
		endif
		break
	EndSwitch
	If answer == @YES
		;Deletes testsource directory and all of it's contents.
		AddExtender("wwsop34i.DLL")
		afiledelete(dir,0)
	Endif
#EndFunction



;Checks if directory creation date is older than 7 days.
;if so, It deletes it and all of its contents
Path = "C:\BTEMP"
HowOld = 7
RemoveIfOlderThan(path, howold, 1, 1);remove if older than 7 days
Message("Directory Removal Test","Done")


;Checks if file modification date is older than 4 months.
;if so, It deletes it.
Path = "C:\BTEMP\TEST.TXT"
HowOld = 4
dateflag = 2 ;Months
timefield = 2; Modified
RemoveIfOlderThan(path, howold, dateflag, timefield);remove file if older than 4 months
Message("File Removal Test","Done")

Article ID:   W15311
File Created: 2005:03:24:09:10:20
Last Updated: 2005:03:24:09:10:20