Option Explicit
On Error Resume Next
'Deletes old files based on age. You must specify a target 
'directory and max age. This script looks for these items
'on the command line - The directory is the first argument, 
'and the max age is the second argument. If no arguments are
'supplied, the environment is checked for KILL_FILES_IN and 
'MAX_FILE_AGE. If no environment variables are found, the user 
'is asked.
'Written by Eric Phelps http://www.ericphelps.com


Main
Wscript.Quit 0

Sub Main()
Dim wsh 'As WScript.Shell
Dim fil 'As Scripting.File
Dim fils 'As Scripting.Files
Dim fol 'As Scripting.Folder
Dim fols 'As Scripting.Folders
Dim fs 'As Scripting.FileSystemObject
Dim strStartingDirectory 'As String
Dim strDirectories() 'As String
Dim lngCounter 'As Long
Dim dblMaxAge 'As Double
Const READONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4
	On Error Resume Next
	'Create needed objects
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set wsh = CreateObject("WScript.Shell")
	'Initialize input variables
	strStartingDirectory = ""
	dblMaxAge = 0
	'Get data from command line
	If Wscript.Arguments.Count > 0 Then
		strStartingDirectory = Wscript.Arguments(0)
		If Wscript.Arguments.Count = 2 Then
			dblMaxAge = CDbl(Wscript.Arguments(1))
		End If
	End If
	'Get data from the environment
	If strStartingDirectory = "" Then
		strStartingDirectory = wsh.Environment.Item("KILL_FILES_IN")
	End If
	If dblMaxAge = 0 Then
		If IsNumeric(wsh.Environment.Item("MAX_FILE_AGE")) Then
			dblMaxAge = CDbl(wsh.Environment.Item("MAX_FILE_AGE"))
		End If
	End If	
	'Ask user for data
	If strStartingDirectory = "" Then
		strStartingDirectory = InputBox("Enter path to start deleting at:", "Delete Old Files", FileNameInThisDir(""))
		MsgBox "You could have supplied the path information as the environment variable KILL_FILES_IN or as the first argument to this script"
	End If
	If strStartingDirectory = "" Then
		WScript.Quit 1
	End If
	If dblMaxAge = 0 Then
		dblMaxAge = CDbl(InputBox("Enter the minimum time since file creation (in days) of files to delete", "Delete Old Files", "180"))
		MsgBox "You could have supplied the file age information in the environment variable MAX_FILE_AGE or as the second argument to this script"
	End If
	If dblMaxAge = 0 Then
		WScript.Quit 1
	End If
	'Test input data
	Redim strDirectories(0)
	strDirectories(0) = fs.GetAbsolutePathName(strStartingDirectory)
	If strDirectories(0) = "" Then Wscript.Quit 1
	If dblMaxAge < 1 Then Wscript.Quit 1
	lngCounter = 0
	'Start deleting!
	Do Until lngCounter > Ubound(strDirectories,1)
		'Next folder to process
		Set fol = fs.GetFolder(strDirectories(lngCounter))
		'Get each file in turn
		Err.Clear
		Set fils = fol.Files
		If Err.Number <> 0 Then Exit Sub
		For Each fil In fils
			If (CDbl(Now) - CDbl(fil.DateCreated)) > dblMaxAge Then
			If ((fil.Attributes And READONLY) = 0) Then
			If ((fil.Attributes And SYSTEM) = 0) Then
			If ((fil.Attributes And HIDDEN) = 0) Then
			If Lcase(fil.Path) <> Lcase(Wscript.ScriptFullName) Then
				fil.Delete
			End If
			End If
			End If
			End If
			End If
		Next
		'Check for any sub folders and add them to the folder array
		Set fols = fol.SubFolders
		For each fol in fols
			If Lcase(fol.Name) <> "recycled" Then
				Redim Preserve strDirectories(Ubound(strDirectories,1) + 1)
				strDirectories(Ubound(strDirectories,1)) = fol.Path
			End If
		Next
		'Check for (and delete) empty directory
		Set fol = fs.GetFolder(strDirectories(lngCounter))
		If ((fol.Files.Count = 0) And (fol.SubFolders.Count = 0)) Then
			fol.Delete
		End If
		lngCounter = lngCounter + 1
	Loop
End Sub

Function FileNameInThisDir(strFileName) 'As String
'Returns the complete path and file name to a file in
'the script directory. For example, "trans.log" might
'return "C:\Program Files\Scripts\Database\trans.log"
'if the script was in the "C:\Program Files\Scripts\Database"
'directory.
Dim fs 'As Scripting.FileSystemObject
	Set fs = CreateObject("Scripting.FileSystemObject")
	 FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName))
	''''''''''Clean up
	Set fs = Nothing
End Function


