'Program: File Limiter Script 'Version: 1.0 'Author: Joshua R. Cook 'Website: http://www.joshcook.net/ 'Date: 6.15.2006 On Error Resume Next Set objFSO = CreateObject("Scripting.FileSystemObject") 'Obtain Argument: /? If WScript.Arguments.Named.Exists("?") Then WScript.Echo("| File Limiter") WScript.Echo("| Version 1.00") WScript.Echo("| Keeps the specified number of newest files in a directory.") WScript.Echo("|") WScript.Echo("| Usage:") WScript.Echo("| filelimiter.vbs /ext: /num: PATH ") WScript.Echo("| /? - displays usage") WScript.Echo("| /date: - specify which date to compare against") WScript.Echo("| c = file creation date") WScript.Echo("| a = file last access date") WScript.Echo("| m = file last modified date (default)") WScript.Echo("| /ext: - file extension (with or without the leading '.'") WScript.Echo("| /num: - number of files to keep") WScript.Echo("| /? - displays usage") WScript.Echo("| /test - lists file to be deleted, doesn't perform file deletion") WScript.Echo("| - directory in which to process files (must be last)") WScript.Echo("|") WScript.Echo("| Examples:") WScript.Echo("| filelimiter.vbs /ext:txt /num:10 c:\my directory\") WScript.Echo("| - Keeps the 10 newest *.txt files located in the c:\my directory\,") WScript.Echo("| based on the file's last modified date.") WScript.Echo("| filelimiter.vbs /date:a /ext:log /num:5 c:\logfiles\") WScript.Echo("| - Keeps the 5 newest *.log files located in the c:\logfiles\,") WScript.Echo("| based on the file's last access date.") WScript.Quit 0 End If ErrNum = 0 ErrMsg = "" 'Obtain Argument: /date If WScript.Arguments.Named.Exists("date") Then argDate = WScript.Arguments.Named("date") argDate = LCase(argDate) If argDate <> "c" And argDate <> "a" And argDate <> "m" Then ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - /date must either be 'c', 'a', 'm' or not specified" End If Else argDate = "m" End If 'Obtain Argument: /ext If WScript.Arguments.Named.Exists("ext") Then argExt = WScript.Arguments.Named("ext") If argExt = "" Then ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - /ext cannot be blank" Else If Left(argExt, 1) = "." Then argExt = Mid(argExt, 2, Len(argExt) - 1) argExt = LCase(argExt) End If Else ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - /ext is required" End If 'Obtain Argument: /num If WScript.Arguments.Named.Exists("num") Then argNum = WScript.Arguments.Named("num") If argNum = "" Or Not IsNumeric(argNum) Then ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - /num must be a number and cannot be blank" End If Else ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - /num is required" End If 'Obtain Argument: For Each tmpArgument in WScript.Arguments.Unnamed argPath = argPath & tmpArgument Next If argPath = "" Then ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - is required" End If 'Obtain Argument: /test If WScript.Arguments.Named.Exists("test") Then argTest = True Else argTest = False End If 'Make sure exists If ErrNum = 0 Then If Not objFSO.FolderExists(argPath) Then ErrNum = ErrNum + 1 ErrMsg = ErrMsg & "| - does not exist (" & argPath & ")" End If End If If ErrNum > 0 Then WScript.Echo("| File Limiter") WScript.Echo("| ") WScript.Echo("| The following error(s) were reported:") WScript.Echo("|") ErrMsg = Split(ErrMsg, "|") For Each ErrItem in ErrMsg If ErrItem <> "" Then WScript.Echo("|" & ErrItem) Next WScript.Echo("|") WScript.Echo("| Please run filelimiter.vbs /? for correct usage") WScript.Quit 1 End If 'Get file count, add all type EXT files into array for processing Set objFolder = objFSO.GetFolder(argPath) tmpFileCount = objFolder.Files.Count - 1 Dim TargetFiles() 'All files of type EXT located in PATH ReDim TargetFiles(1, tmpFileCount) tmpCounter = 0 For Each objFile in objFolder.Files If LCase(objFSO.GetExtensionName(objFile.Path & "\" & objFile.Name)) = argExt Then Select Case argDate Case "c" TargetFiles(0, tmpCounter) = objFile.DateCreated Case "a" TargetFiles(0, tmpCounter) = objFile.DateLastAccessed Case "m" TargetFiles(0, tmpCounter) = objFile.DateLastModified End Select TargetFiles(1, tmpCounter) = objFile.Path & "?" & objFile.Name tmpCounter = tmpCounter + 1 End If Next ReDim Preserve TargetFiles(1, tmpCounter) Set objFolder = Nothing 'How many files should we be deleting DeleteCount = tmpCounter - argNum If DeleteCount < 0 Then DeleteCount = tmpCounter - 1 'Sort Array based on first dimension, the date Call QuickSort(TargetFiles, 0, UBound(TargetFiles, 1), 0) 'Process files that should be deleted WScript.Echo("| File Limiter") WScript.Echo("|") If tmpCounter = 0 Then WScript.Echo("| - No Files to Delete") For tmpCounter = 0 to DeleteCount tmpTargetFileDate = TargetFiles(0, tmpCounter) tmpTargetFileData = Split(TargetFiles(1, tmpCounter), "?") WScript.Echo("| - " & tmpTargetFileData(1) & " (" & tmpTargetFileDate & ")") If Not argTest Then Set objFile = objFSO.GetFile(tmpTargetFileData(0)) objFile.Delete Set objFile = Nothing WScript.Echo("| Action: File Deleted") Else WScript.Echo("| Action: None, Test Mode Enabled") End If Next WScript.Echo("|") WScript.Echo("| Process Complete") Set objFSO = Nothing WScript.Quit 0 '-- Borrowed Code from Interweb Sub SwapRows(ary,row1,row2) '== This proc swaps two rows of an array Dim x,tempvar For x = 0 to Ubound(ary,2) tempvar = ary(row1,x) ary(row1,x) = ary(row2,x) ary(row2,x) = tempvar Next End Sub 'SwapRows Sub QuickSort(vec,loBound,hiBound,SortField) Dim pivot(),loSwap,hiSwap,temp,counter Redim pivot (Ubound(vec,2)) '== Two items to sort if hiBound - loBound = 1 then if vec(loBound,SortField) > vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound) End If '== Three or more items to sort For counter = 0 to Ubound(vec,2) pivot(counter) = vec(int((loBound + hiBound) / 2),counter) vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter) vec(loBound,counter) = pivot(counter) Next loSwap = loBound + 1 hiSwap = hiBound do '== Find the right loSwap while loSwap < hiSwap and vec(loSwap,SortField) <= pivot(SortField) loSwap = loSwap + 1 wend '== Find the right hiSwap while vec(hiSwap,SortField) > pivot(SortField) hiSwap = hiSwap - 1 wend '== Swap values if loSwap is less then hiSwap if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap) loop while loSwap < hiSwap For counter = 0 to Ubound(vec,2) vec(loBound,counter) = vec(hiSwap,counter) vec(hiSwap,counter) = pivot(counter) Next '== Recursively call function .. the beauty of Quicksort '== 2 or more items in first section if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField) '== 2 or more items in second section if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField) End Sub 'QuickSort