'===DeleteTempFiles.vbs=== ' Source: http://groups.google.com/group/microsoft.public.scripting.wsh/browse_frm/thread/8fb7b6a0bd05d296/eb34633c642bcdb7#eb34633c642bcdb7 'Michael Harris 'Microsoft.MVP.Scripting Const TemporaryFolder = 2 'for GetSpecialFolder set fso = createobject("scripting.filesystemobject") 'init an empty array (ubound will be -1)... ' 'we use an array and store the file objects. 'this avoids any problems with altering the 'contents of the Files collections while they 'are being iterated. ' arFiles = array() count = -1 'get the path to the temp folder ' tempdir = fso.GetSpecialFolder(TemporaryFolder) 'load the (global scope) arFiles 'SelectFiles calls itself recursively 'for SubFolders ' SelectFiles tempdir msgbox count+1 & " files found" 'now do the actual deletes. the error trap 'is in case any are in-use... ' dcount = 0 for each file in arFiles on error resume next file.delete true if err.number = 0 then dcount = dcount + 1 err.clear on error goto 0 next 'now go back and delete empty folders 'below the temp folder DeleteEmptyFolders tempdir,false 'comment out for "silent" operation, 'or add support for a "/s" command-line switch. ' msgbox count+1 & " files found, " & dcount & " deleted." sub SelectFiles(sPath) 'select files to delete and add to array... ' set folder = fso.getfolder(sPath) set files = folder.files for each file in files count = count + 1 redim preserve arFiles(count) set arFiles(count) = file next for each fldr in folder.subfolders SelectFiles fldr.path next end sub sub DeleteEmptyFolders(sPath,bDeleteThisFolder) set folder = fso.getfolder(sPath) 'recurse first... ' for each fldr in folder.subfolders DeleteEmptyFolders fldr.path,true next 'if no files or folders then delete... ' 'bDeleteThisFolder is false for 'the root of the subtree, and true for 'sub-folders (unless you want to delete 'the entire subtree if it is empty). ' if (folder.files.count = 0) and _ (folder.subfolders.count) = 0 and _ bDeleteThisFolder then folder.delete exit sub end if end sub ' '===end-script===
From novice to tech pro — start learning today.