
Removing Empty Directories
Thanks Michael. This is excellent.
If anyone has the time, I have some questions...
Here is a really stripped version of my script. Where in this I would call
the DeleteEmptyFolders subroutine?
Since I'm already walking the tree, can I merge the two subroutines together
to cleanup as I go, rather than run through a second time?
-If possible, I would like to have these explained because I'm not really
*getting it*-
How does this work, calling the DeleteEmptyFolders subroutine within the
same subroutine?
What does bDeleteThisFolder (in the last IF statement) reference and how
does it get there/what does it do?
'========begin=============
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strPath = UCase(objArgs(0))
strYears = Eval(objArgs(1) * 365)
TargetDate = Eval(Now - strYears)
strArchive = UCase(strPath & "\Archive")
ShowAllFiles objFSO.GetFolder(strPath)
Sub ShowAllFiles(oFolder)
For Each oFile In oFolder.Files
Set sFile = objFSO.GetFile(oFile)
If sFile.DateLastModified < TargetDate Then
sFile.Move strArchive
End If
Next
For Each oSubFolder In oFolder.SubFolders
ShowAllFiles oSubFolder
Next
End Sub
'========end==============
Thanks all,
Chris
set fso = createobject("scripting.filesystemobject")
(assumes a global scope fso variable)
DeleteEmptyFolders "c:\some path",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
folder.delete
exit sub
end if
end sub
--
Michael Harris
Microsoft.MVP.Scripting
--
Please do not email questions - post them to the newsgroup instead.
--