Skip to content

Instantly share code, notes, and snippets.

@jrdmb
Created May 9, 2015 18:37
Show Gist options
  • Save jrdmb/717d6ac4bc8bae4f938b to your computer and use it in GitHub Desktop.
Save jrdmb/717d6ac4bc8bae4f938b to your computer and use it in GitHub Desktop.
Option Explicit
Dim oFSO, oFolder, oSubFolder, oSubFolders, sDirectoryPath, sTempDir
Dim oFileCollection, oFile, sDir
Dim iDaysOld, iFiles, iFolders
Dim wshShell
'Get user Temp directory path from environment variable
Set wshShell = CreateObject( "WScript.Shell" )
sTempDir = wshShell.ExpandEnvironmentStrings( "%TEMP%" )
'An alternate is "%LOCALAPPDATA%\Temp"
Set wshShell = Nothing
iDaysOld = 0
iFiles = 0
iFolders = 0
'This section declares the file system variables that you're going to use to access the
'directory and the files that you want to clean up.
'Setting up the iDaysOld variable tells the script the age of the files that you want to keep.
'Next comes the ultra-simple cleanup section.
' ***** CLEAR OUT OLD FILES IN LOG FOLDER *****
sDirectoryPath = sTempDir
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
on error resume next
oFile.Delete(True)
if err.number = 0 then
iFiles = iFiles + 1
end if
err.clear
End If
Next
'The section above connects with the Windows File System, and then connects to the
'directory that you've defined with the sDirectoryPath variable. This first loop
'goes through each individual file in the directory, checks the modified date and
'compares it to the age of the file that you defined. If it's older than 3 days,
'it performs a deletion operation on that file.
'This works great on files, but what about all of the subdirectories in folders like
'the Windows temp directory? This next section of the script will next file through all
'of the subdirectories, and perform the same file operations on the files in there as well.
Set oSubfolders = oFolder.SubFolders
For Each oSubFolder In oSubFolders
sDirectoryPath = oSubFolder
'msgbox sDirectoryPath
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
on error resume next
oFile.Delete(True)
if err.number = 0 then
iFiles = iFiles + 1
end if
err.clear
End If
Next
If oSubFolder.Size = 0 Then
on error resume next
oSubFolder.Delete(True)
if err.number = 0 then
iFolders = iFolders + 1
end if
err.clear
end if
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
Next
'Finally, don't forget to clear out the objects in the case where there weren't
'any subdirectories to go through.
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
msgbox "Delete Temp Files in " & sTempDir & vbcrlf & vbcrlf &"Results of Delete Temp Files Task:" & vbcrlf & vbcrlf & "# Files Deleted: " & iFiles & vbcrlf & "# Folders Deleted: " & iFolders,,"Delete Temp Files"
WScript.Quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment