Code: Select all
ON ERROR RESUME NEXT
' Filename: cleanup.vbs
' Created: 04/11/05
' Author: Troy Hockman
' Discription: Delete files older than certain number of days.
' Declare variables
Dim ArgObj, strPath, iDays, smsg, strFilter, strLogName, ii, strStarted, strEnded, strFileType, strAction, strCompare, strCompareString
Dim objShell
' Get commandline arguments
Set ArgObj = WScript.Arguments
strAction = ucase(ArgObj(0))
strFileType = ucase(ArgObj(1))
strPath = ArgObj(2)
iDays = ArgObj(3)
strFilter = ArgObj(4)
strLogName = ArgObj(5)
strsilent = ArgObj(6)
smsg = ""
' Set start time
strStarted = Date() & "-" & Time()
' Check for valid input
inputvalidation()
' Check for valid path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
If err.number <> 0 Then
wscript.echo "Folder " & strPath & " Does Not Exist!"
wscript.quit
Else
End If
err.clear
' Get file types
filetypes()
ii = 0
For Each objItem In objFolder.Files
If objItem.DateLastModified < date() - iDays then
If strFilter = "*" Then
Select Case strFileType
Case "*"
remove()
Case Else
If objItem.Type = strFileType Then remove() End If
End Select
Else
strCompare = ucase(Left(strFilter,1))
strCompareString = Right(strFilter,Len(strFilter) - 2)
Select Case strCompare
Case "B"
If Left(ObjItem.Name,Len(strCompareString)) = strCompareString Then
Select Case strFileType
Case "*"
remove()
Case Else
If objItem.Type = strFileType Then remove() End If
End Select
Else
End If
Case "C"
If instr(objItem.Name,strCompareString) Then
Select Case strFileType
Case "*"
remove()
Case Else
If objItem.Type = strFileType Then remove() End If
End Select
Else
End If
Case Else
help()
End Select
End If
End If
Next
strEnded = Date() & "-" & Time()
smsg = smsg & vbcrlf & vbcrlf & "---------------------------------" & vbcrlf & vbcrlf & _
"Started: " & strStarted & vbcrlf & "Total Files: " & ii & vbcrlf & "Action: " & _
strAction & vbcrlf & "Ended: " & strEnded
Set objTextStream = objFSO.CreateTextFile(strLogName, True, False)
Call objTextStream.WriteLine(smsg)
Call objTextStream.Close()
If strSilent= "view" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad" & " " & strLogName
End If
'Cleanup
Set ArgObj = Nothing
Set objTextStream = Nothing
Set objFSO = Nothing
Set objShell = Nothing
wscript.quit
Sub inputvalidation()
Select Case strAction
Case "LIST"
Case "DELETE"
Case Else
help()
End Select
If strFileType = "" Then help()
If strPath = "" Then help()
If iDays = "" Then help()
If strFilter = "" Then help()
If strLogName = "" Then help()
if strSilent = "" Then help()
End Sub
Sub filetypes()
' This is a list of file types that are used. Change as needed.
Select Case strFileType
Case "TXT"
strFileType = "Text Document"
Case "EXE"
strFileType = "Application"
Case "png"
strFileType = "PNG Image"
End Select
End Sub
Sub remove()
Select Case strAction
Case "LIST"
smsg = smsg & strpath & "\" & objitem.Name & vbtab & objItem.DateLastModified & vbcrlf
Case "DELETE"
smsg = smsg & strpath & "\" & objitem.Name & vbtab & objItem.DateLastModified & vbcrlf
objFSO.DeleteFile(strPath & "\" & objitem.Name)
End Select
ii = ii + 1
End Sub
Sub Help()
' This message will display the syntax. I suggest using the list and view options first to verify results.
wscript.echo "Missing Arguments! Must be ran from commandline." & vbcrlf & vbcrlf & _
"cleanup.vbs list/delete filetype location daysold filter loglocation view/noview" & vbcrlf & vbcrlf & _
"ex. cleanup.vbs list * c:\temp 5 * c:\temp\log.txt view" & vbcrlf & _
"ex. cleanup.vbs delete * c:\temp 5 b=test c:\temp\log.txt noview" & vbcrlf & _
"ex. cleanup.vbs delete txt c:\temp 120 * c:\temp\log.txt view"
wscript.quit
End Sub