Tuesday, July 31, 2012

VB Script : Clean Files Older Then 7 Days

Option Explicit
dim WshShell,fso, file, source, daysBack, toDelete(1000000), toDeleteFolder(1000000), groups, group
dim i, ii, path, copySource, copyDestination, copySource1, copyDestination1, copySource2, copyDestination2,result

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")

daysBack = 7
Path = "z:\temporary"


listFiles(Path)

for ii = 0 to i-1
 fso.DeleteFile toDelete(ii), true
Next

DeleteFolders(Path)

Set fso = Nothing

'---------------------------------------------

private sub listFiles(folderSpec)
dim file, folder

 For Each file In fso.GetFolder(folderSpec).files
  If (DateValue(file.dateCreated) < DateValue(Now - daysBack)) And (DateValue(file.dateLastModified) < DateValue(Now - daysBack)) Then
   toDelete(i) = file.Path
   i = i + 1
  End If
 Next

 for each folder in fso.GetFolder(folderSpec).SubFolders
  listFiles(folder.path)
 Next

end Sub

Private Sub DeleteFolders(folderSpec)
 Dim folder

 If fso.GetFolder(folderSpec).size = 0 Then
  fso.DeleteFolder(folderSpec)
 End If
 
 On Error Resume Next
 For each folder in fso.GetFolder(folderSpec).SubFolders
  DeleteFolders(folder.path)
 Next
 On Error Goto 0
End Sub 

No comments:

Post a Comment