Deleting Files and Folders
Filed under: Configuring, Endpoint Management and Virtualization
A common issue that is encountered on a Windows-based system is the deletion of files. The following script deletes the contents of two temporary system folders; Files and folders in the folders, that error on delete, are bypassed.
Folders
- %systemdrive%\temp
- %windir%\temp
%systemdrive% and %windir% are system variables.
The value for arrFolderPaths can be edited to delete files and folders of different folders.
'*******************************************************************************
'* File: deleteFiles.vbs
'* Purpose: This script deletes all files and subfolders of a defined
'* array of folders.
'* Usage: deleteFiles.vbs
'* Version: 1.0 (02/24/2009)
'* Technology: VBScript
'* Requirements: Windows 2000 or higher (tested on Windows XP)
'* Windows Script Host 5.6 or higher (tested with WSH 5.6)
'* History:
'* 02/24/2009 Added header information using Microsoft's
'* Windows 2000 Scripting Guide Framework.
'* 02/24/2009 Script created.
'*******************************************************************************
Option Explicit
Const ForWriting = 2
On Error Resume Next ' Add only for production
Dim arrFolderPaths, strFolderPath, strFolderPaths, objLogFile
Set objLogFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(Replace(WScript.ScriptFullName, ".vbs", ".log"), ForWriting, True)
objLogFile.WriteLine "Started: " & Now()
arrFolderPaths = Array("%windir%\temp", "%userprofile%\local settings\temp", "%userprofile%\local settings\temporary internet files")
For Each strFolderPath In arrFolderPaths
strFolderPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings(strFolderPath)
If CreateObject("Scripting.FileSystemObject").FolderExists(strFolderPath) = True Then
ProcessFolder CreateObject("Scripting.FileSystemObject").GetFolder(strFolderPath)
End If
Next
objLogFile.Write "Finished: " & Now()
Function ProcessFolder(ByVal objFolder)
On Error Resume Next
Dim objFile, objSubFolder, intFilesCount, intSubFoldersCount
objLogFile.WriteLine """" & objFolder.Path & """"
Err.Clear
intFilesCount = objFolder.Files.Count
If Err.Number Then
objLogFile.WriteLine vbTab & vbTab & "Could not read file count (" & Err.Number & ": " & Err.Description & ")"
Else
If intFilesCount Then objLogFile.WriteLine vbTab & vbTab & intFilesCount & " files"
End If
Err.Clear
intSubFoldersCount = objFolder.SubFolders.Count
If Err.Number Then
objLogFile.WriteLine vbTab & vbTab & "Could not read subfolder count (" & Err.Number & ": " & Err.Description & ")"
Else
If intSubFoldersCount Then objLogFile.WriteLine vbTab & vbTab & intSubFoldersCount & " subfolders"
End If
If intFilesCount = 0 And intSubFoldersCount = 0 Then objLogFile.WriteLine vbTab & vbTab & "No files or subfolders found"
If intFilesCount Then
For Each objFile In objFolder.Files
On Error Resume Next
objLogFile.Write vbTab & vbTab & objFile.Name
objFile.Delete True
If Err.Number Then
objLogFile.WriteLine " file has not been deleted (" & Err.Number & ": " & Err.Description & ")"
Else
objLogFile.WriteLine " file has been deleted"
End If
On Error GoTo 0
Set objFile = Nothing
Next
End If
If intSubFoldersCount Then
For Each objSubFolder In objFolder.SubFolders
ProcessFolder objSubFolder
On Error Resume Next
objSubFolder.Delete True
If Err.Number Then
objLogFile.WriteLine vbTab & vbTab & "This folder has not been deleted (" & Err.Number & ": " & Err.Description & ")"
Else
objLogFile.WriteLine vbTab & vbTab & "This folder has been deleted"
End If
On Error GoTo 0
Set objSubFolder = Nothing
Next
End If
Set objFolder = Nothing
End Function

cool script
I published a similar script to delete temp files and folders, but this is cool and also the log looks great!
You can also include deleting all files from PREFTECH folder [C:\WINDOWS\Prefetch] in your script.
Thank you for posting this script. I appreciate it.
Eshwar
Thanks,
Eshwar
You're welcome. I actually created this script for use on the Altiris Deployment Server.
This script is more of a proof-of-concept for scripters (old and new). There is no need to create objects and destroy them if you access them directly.
Good suggestion on the Prefetch folder, but try to get away from hard-coding paths. In your example, use %windir% instead of "C:\Windows". While it's not always possible, training yourself to use it will alleviate issues in areas where you place your script on a different system, such as Citrix which has a Windows folder of M:\Windows.
More scripts and programs to come. ;)
MyITGuy | Automating your life to make my life easier.
Powershell
Have you a Powershell version
No PowerShell version at this time; But it should't take you long to convert this one.
MyITGuy | Automating your life to make my life easier.
Would you like to reply?
Login or Register to post your comment.