Removing Empty Directories 
Author Message
 Removing Empty Directories

I'm getting stumped. I'm working on a VBScript that will move outdated files to an archive directory. I would like to clean up directories that are emptied in the process. I have everything done except the clean up portion.
The problems I'm running into is either permission denied errors, presumably because I have the directory locked, or else I'm removing a directory tree that still has subfolders in the collection that are awaiting their turn in the loop, which causes a *folder not found* error.

I would like to be able to remove the empty directories back up the tree (after looping down through) and I'm not getting anywhere.
I haven't been able to find any examples for systematically deleting folders.

Any suggestions would be appreciated.
Thanks,
Chris



Sun, 05 Oct 2003 05:03:02 GMT  
 Removing Empty Directories
recursion grasshopper ;)

This is one of the few places that computer science {*filter*}we sufferred
through in college pays off...

lame psuedocode...just doing it on the fly here to get you started down the
path, you're gonna have to actually write it yourself...and this does
nothing to check if they are actually empty, I'll leave that as an exercise
for the reader...

call delsubdirs("c:\app\archive")

sub delsubdirs(dir)
    get sub dirs here...
    dirs = getsubdirs(dir)
    then
    for each subdir in dirs
        if hassubdirs(subdir) then
            delsubdirs subdir 'this will cause the recursion, if there are
sub dirs and keep calling back to itself till there are none.
        else
            delete directory
        end if
    next
end sub

I'm getting stumped. I'm working on a VBScript that will move outdated files
to an archive directory. I would like to clean up directories that are
emptied in the process. I have everything done except the clean up portion.
The problems I'm running into is either permission denied errors, presumably
because I have the directory locked, or else I'm removing a directory tree
that still has subfolders in the collection that are awaiting their turn in
the loop, which causes a *folder not found* error.

I would like to be able to remove the empty directories back up the tree
(after looping down through) and I'm not getting anywhere.
I haven't been able to find any examples for systematically deleting
folders.

Any suggestions would be appreciated.
Thanks,
Chris



Sun, 05 Oct 2003 13:01:00 GMT  
 Removing Empty Directories
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.
--

I'm getting stumped. I'm working on a VBScript that will move outdated files to an archive
directory. I would like to clean up directories that are emptied in the process. I have everything
done except the clean up portion.
The problems I'm running into is either permission denied errors, presumably because I have the
directory locked, or else I'm removing a directory tree that still has subfolders in the collection
that are awaiting their turn in the loop, which causes a *folder not found* error.

I would like to be able to remove the empty directories back up the tree (after looping down
through) and I'm not getting anywhere.
I haven't been able to find any examples for systematically deleting folders.

Any suggestions would be appreciated.
Thanks,
Chris



Mon, 06 Oct 2003 07:52:26 GMT  
 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.
--



Mon, 06 Oct 2003 11:07:59 GMT  
 Removing Empty Directories
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

 '===
 ' At this point you're about to move back up a level
 ' in the folder tree.  So before you leave, you could
 ' test the oFolder.Files/SubFolders.Count.  If both are
 ' 0 the you can call

 ' ==> oFolder.Delete

 ' Unless you use somehting like the bDeleteThisFolder
 ' in my example (which comes froam an actual script
 ' that I use every day) you'll delete the entire tree if
 ' all the folders end up being empty...
 '===

End Sub

--
Michael Harris
Microsoft.MVP.Scripting
--

Please do not email questions - post them to the newsgroup instead.
--

Quote:

> 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.
> --



Mon, 06 Oct 2003 12:50:02 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Removing Empty Directories

2. remove empty white frame from EPS2.0

3. Deleting empty directories

4. Removing ActiveX Controls from Downloaded Program Fies Directory

5. Add/Remove Directory User Permissions

6. Remove Directory?

7. Remove Directory using WSH

8. remove method for options not removing selected option.

9. Query Active Directory for User's Home Directory Path

10. Using AppGetStatus on IIS web directory or virtual directory object

11. need vb script to change users home directory in on fell swoop (active directory)

12. CDec returns empty variant

 

 
Powered by phpBB® Forum Software