Quote:
> I have several thousand folders that need to be deleted, But only if they
> are empty.
Hi
The DeleteEmptyFolders sub below will remove all empty folders (also the
complete folder/subfolder/subfolder/... structure will be removed if no files
exists in it).
Note the second parameter to the sub (bDeleteThisFolder). If it is set to True,
the script will remove the root folder in the input path as well if it is empty
(the folder "c:\temp" itself in the example below) .
DeleteEmptyFolders ("c:\temp", False)
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
on error resume next
folder.delete
exit sub
end if
end sub
--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter