Option Explicit
On Error Resume Next
'Deletes small files in and below a specified directory. 
'You must specify a target directory and file size. This 
'script looks for these items on the command line - The 
'directory is the first argument, and the file size is the 
'second argument. If no arguments are supplied, the 
'environment is checked for KILL_FILES_IN and MIN_FILE_SIZE. 
'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 dblMaxSize 'As Double
Const READONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4
	'Create needed objects
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set wsh = CreateObject("WScript.Shell")
	'Initialize input variables
	strStartingDirectory = ""
	dblMaxSize = 0
	'Get data from command line
	If Wscript.Arguments.Count > 0 Then
		strStartingDirectory = Wscript.Arguments(0)
		If Wscript.Arguments.Count = 2 Then
			dblMaxSize = 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 dblMaxSize = 0 Then
		If IsNumeric(wsh.Environment.Item("MIN_FILE_SIZE")) Then
			dblMaxSize = CDbl(wsh.Environment.Item("MIN_FILE_SIZE"))
		End If
	End If	
	'Ask user for data
	If strStartingDirectory = "" Then
		strStartingDirectory = InputBox("Enter path to start deleting at:", "Delete Small 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 dblMaxSize = 0 Then
		dblMaxSize = CDbl(InputBox("Enter the minimum size of files to leave alone (files smaller than this size will be deleted)", "Delete small Files", "1024"))
		MsgBox "You could have supplied the size information in the environment variable MIN_FILE_SIZE or as the second argument to this script"
	End If
	'Test input data
	Redim strDirectories(0)
	strDirectories(0) = fs.GetAbsolutePathName(strStartingDirectory)
	If strDirectories(0) = "" Then Wscript.Quit 1
	If dblMaxSize < 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
		Set fils = fol.Files
		If Err.Number <> 0 Then Exit Sub
		For Each fil In fils
			If fil.Size < dblMaxSize 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
		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

