Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- ' Declare Variables
- Dim wshArgs, wshShell
- Dim FilePath, Retention, FileFilter
- Set wshArgs = wscript.arguments
- Set wshShell = wscript.createobject("wscript.shell")
- If ChkArgs = True then
- FilePath = wshArgs.named.item("dir")
- Retention = wshArgs.named.item("days")
- DelFiles FilePath, Retention, FileFilter
- wscript.echo "Done."
- wshShell.LogEvent 4, "Delete Operation Completed (CLEANUP.VBS)."
- Else
- ShowUsage 1
- wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS). Required Arguments Not Supplied"
- wscript.echo "DELETE OPERATION FAILED!"
- wscript.echo "The required Arguments have not been entered."
- End If
- Set wshArgs = Nothing
- Set wshShell = Nothing
- FilePath = ""
- Retention = ""
- Function ChkArgs
- Dim PathArg, DaysArg, ExtArg, HelpArg
- PathArg = wshArgs.Named.exists("dir")
- DaysArg = wshArgs.named.exists("days")
- ExtArg = wshArgs.named.exists("ext")
- HelpArg = wshArgs.named.exists("?")
- If HelpArg = True then
- ShowUsage 0
- wscript.quit
- End If
- If PathArg = True and DaysArg = True then
- ChkArgs = True
- Else
- ChkArgs = False
- End If
- If ExtArg = True then
- FileFilter = True
- Else
- FileFilter = False
- End If
- End Function
- '
- Sub ShowUsage(Mode)
- wscript.echo "cleanup.vbs Usage Instructions:" & _
- vbcrlf & _
- vbcrlf & _
- " cleanup.vbs /dir:[path] /days:7 /ext:C:\Windows\Temp" & _
- vbcrlf & _
- vbcrlf & _
- "path = this is the full path to the directory to be cleaned" & _
- vbcrlf & _
- vbcrlf & _
- "retention period = this is the length of time in days that files are to be kept" & _
- vbcrlf & _
- vbcrlf & _
- "file extension = optional argument. Only files with specified extension will be deleted (enter WITHOUT a preceding dot)" & _
- vbcrlf & _
- vbcrlf & _
- "Any files that are outside the specified retention period will be deleted."
- ' 0 = Help Only Mode. In this mode exit script after displaying usage instructions
- ' and do not attempt a delete operation
- If Mode = 0 then
- wscript.quit
- Else
- End If
- End Sub
- Sub DelFiles(Dir, Days, DelFilter)
- Dim DelDate, FSO, Folder, Files, File, FileDate, FilterExt, Ext, lPos
- DelDate = Now - Days
- FilterExt = wshArgs.named.item("ext")
- Set FSO = createobject("scripting.filesystemobject")
- On Error Resume Next
- Set Folder = FSO.GetFolder(Dir)
- If Err.Number = 76 then
- wscript.echo "Delete operation FAILED! - Path Not Found."
- wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS) - Path Not Found."
- wscript.quit
- End If
- On Error Goto 0
- Set Files = Folder.Files
- For each File in Files
- 'DateCreated
- 'DateLastAccessed
- 'DateLastModified
- FileDate = File.DateCreated
- lPos = InStrRev(File.Name, ".")
- If lPos = 0 Then
- Ext=""
- Else
- Ext = Mid(File.Name, lPos + 1)
- End If
- On Error Resume Next
- If DelFilter = True then
- If FileDate <= DelDate and Ext = FilterExt then
- File.Delete
- ' wscript.echo "Debug: Deleting " & Dir & "\" & File.Name
- End If
- Else
- If FileDate <= DelDate then
- ' wscript.echo "Debug: Deleting " & Dir & "\" & File.Name
- File.Delete
- End If
- End If
- If Err.Number = 70 Then
- wscript.echo "Deletion of " & Dir & "\" & File.Name & _
- " Failed - ACCESS DENIED!"
- wshShell.LogEvent 2, "Deletion of " & Dir & "\" & _
- File.Name & " Failed - ACCESS DENIED! (CLEANUP.VBS)"
- End If
- On Error Goto 0
- Next
- ' Clear File and Folder Objects
- Set Files = Nothing
- Set Folder = Nothing
- Set FSO = Nothing
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement