Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. Option Explicit
  2.  
  3. ' Declare Variables
  4. Dim wshArgs, wshShell
  5. Dim FilePath, Retention, FileFilter
  6. Set wshArgs = wscript.arguments
  7. Set wshShell = wscript.createobject("wscript.shell")
  8.  
  9. If ChkArgs = True then
  10. FilePath = wshArgs.named.item("dir")
  11. Retention = wshArgs.named.item("days")
  12. DelFiles FilePath, Retention, FileFilter
  13. wscript.echo "Done."
  14. wshShell.LogEvent 4, "Delete Operation Completed (CLEANUP.VBS)."
  15. Else
  16. ShowUsage 1
  17. wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS). Required Arguments Not Supplied"
  18. wscript.echo "DELETE OPERATION FAILED!"
  19. wscript.echo "The required Arguments have not been entered."
  20. End If
  21.  
  22. Set wshArgs = Nothing
  23. Set wshShell = Nothing
  24. FilePath = ""
  25. Retention = ""
  26.  
  27. Function ChkArgs
  28.  
  29. Dim PathArg, DaysArg, ExtArg, HelpArg
  30.  
  31. PathArg = wshArgs.Named.exists("dir")
  32. DaysArg = wshArgs.named.exists("days")
  33. ExtArg = wshArgs.named.exists("ext")
  34. HelpArg = wshArgs.named.exists("?")
  35.  
  36. If HelpArg = True then
  37. ShowUsage 0
  38. wscript.quit
  39. End If
  40.  
  41. If PathArg = True and DaysArg = True then
  42. ChkArgs = True
  43. Else
  44. ChkArgs = False
  45. End If
  46.  
  47. If ExtArg = True then
  48. FileFilter = True
  49. Else
  50. FileFilter = False
  51. End If
  52.  
  53. End Function
  54.  
  55. '
  56.  
  57. Sub ShowUsage(Mode)
  58.  
  59. wscript.echo "cleanup.vbs Usage Instructions:" & _
  60. vbcrlf & _
  61. vbcrlf & _
  62. " cleanup.vbs /dir:[path] /days:7 /ext:C:\Windows\Temp" & _
  63. vbcrlf & _
  64. vbcrlf & _
  65. "path = this is the full path to the directory to be cleaned" & _
  66. vbcrlf & _
  67. vbcrlf & _
  68. "retention period = this is the length of time in days that files are to be kept" & _
  69. vbcrlf & _
  70. vbcrlf & _
  71. "file extension = optional argument. Only files with specified extension will be deleted (enter WITHOUT a preceding dot)" & _
  72. vbcrlf & _
  73. vbcrlf & _
  74. "Any files that are outside the specified retention period will be deleted."
  75.  
  76. ' 0 = Help Only Mode. In this mode exit script after displaying usage instructions
  77. ' and do not attempt a delete operation
  78. If Mode = 0 then
  79. wscript.quit
  80. Else
  81. End If
  82. End Sub
  83.  
  84. Sub DelFiles(Dir, Days, DelFilter)
  85.  
  86. Dim DelDate, FSO, Folder, Files, File, FileDate, FilterExt, Ext, lPos
  87. DelDate = Now - Days
  88. FilterExt = wshArgs.named.item("ext")
  89. Set FSO = createobject("scripting.filesystemobject")
  90.  
  91. On Error Resume Next
  92. Set Folder = FSO.GetFolder(Dir)
  93.  
  94. If Err.Number = 76 then
  95. wscript.echo "Delete operation FAILED! - Path Not Found."
  96. wshShell.LogEvent 2, "DELETE OPERATION FAILED (CLEANUP.VBS) - Path Not Found."
  97. wscript.quit
  98. End If
  99. On Error Goto 0
  100.  
  101. Set Files = Folder.Files
  102.  
  103. For each File in Files
  104. 'DateCreated
  105. 'DateLastAccessed
  106. 'DateLastModified
  107. FileDate = File.DateCreated
  108. lPos = InStrRev(File.Name, ".")
  109. If lPos = 0 Then
  110. Ext=""
  111. Else
  112. Ext = Mid(File.Name, lPos + 1)
  113. End If
  114.  
  115. On Error Resume Next
  116.  
  117. If DelFilter = True then
  118. If FileDate <= DelDate and Ext = FilterExt then
  119. File.Delete
  120. ' wscript.echo "Debug: Deleting " & Dir & "\" & File.Name
  121. End If
  122. Else
  123. If FileDate <= DelDate then
  124. ' wscript.echo "Debug: Deleting " & Dir & "\" & File.Name
  125. File.Delete
  126. End If
  127. End If
  128.  
  129. If Err.Number = 70 Then
  130. wscript.echo "Deletion of " & Dir & "\" & File.Name & _
  131. " Failed - ACCESS DENIED!"
  132. wshShell.LogEvent 2, "Deletion of " & Dir & "\" & _
  133. File.Name & " Failed - ACCESS DENIED! (CLEANUP.VBS)"
  134. End If
  135.  
  136. On Error Goto 0
  137. Next
  138.  
  139. ' Clear File and Folder Objects
  140. Set Files = Nothing
  141. Set Folder = Nothing
  142. Set FSO = Nothing
  143.  
  144. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement