Deleting empty directories 
Author Message
 Deleting empty directories

Hello,

I'm new to Windows Script Host.

I need to delete a fair amount of left over empty directories among current
ones on an NT 4 Server. Doing it "manually" using the Windows Explorer would
take to much time and effort.

Could someone show me how to identify empty directories using the
FileSystemObject.DeleteFolder method or any other way.

Regards

Philip
--
---------------------------------------------------------------------
Kuhn & Seal SA - Avenue de Villardin 2
P.o. Box 233 - 1009 Pully - Switzerland
Tel:+41.(0)21.721.24.24
Fax:+41.(0)21.721.24.29



Fri, 25 Jul 2003 16:42:36 GMT  
 Deleting empty directories
Sub DeleteEmptyFolder(filespec)
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  If fso.GetFolder(filespec).Files.Count = 0 Then fso.DeleteFolder(filespec)
End Sub

=-=-=
Steve
-=-=-


Quote:
> Hello,

> I'm new to Windows Script Host.

> I need to delete a fair amount of left over empty directories among
current
> ones on an NT 4 Server. Doing it "manually" using the Windows Explorer
would
> take to much time and effort.

> Could someone show me how to identify empty directories using the
> FileSystemObject.DeleteFolder method or any other way.

> Regards

> Philip
> --
> ---------------------------------------------------------------------
> Kuhn & Seal SA - Avenue de Villardin 2
> P.o. Box 233 - 1009 Pully - Switzerland
> Tel:+41.(0)21.721.24.24
> Fax:+41.(0)21.721.24.29



Fri, 25 Jul 2003 21:29:25 GMT  
 Deleting empty directories
Don't forget to check the SubFolders.Count as well...

If *both* the Files.Count and SubFolders.Count are 0 then the folder is empty.

You really need to walk the directory tree recursively to the bottom and then work from the bottom
up.  That is, a folder "tree" that contains only empty subfolders won't be "pruned" if you only
check the folder at the top.  With recursion and checking "bottom to top", the lowest level folders
that are empty are deleted.  That *may* then make the parent folder empty and eligible for deletion,
and so on back to the top of the tree.

Here's an example (taken from a real script I use to clean the Windows temp folder).

The bDeleteFolder argument controls only whether the folder that is the starting point is deleted if
it's empty.  Note that the value passed in on the initial call is *not* passed through to recursive
calls.  The recursive calls have True hardcoded in the call.

'== the "C:\Windows\Temp" folder IS NOT to be deleted
'== even if it becomes empty..
'
DeleteEmptyFolders "C:\Windows\Temp",false

'== the "C:\Some Other Folder\Temp" folder IS to be deleted
'== if it becomes empty..
'
DeleteEmptyFolders "C:\Some Other Folder\Temp",true

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

Quote:

> Sub DeleteEmptyFolder(filespec)
>   Dim fso
>   Set fso = CreateObject("Scripting.FileSystemObject")
>   If fso.GetFolder(filespec).Files.Count = 0 Then fso.DeleteFolder(filespec)
> End Sub

> =-=-=
> Steve
> -=-=-



> > Hello,

> > I'm new to Windows Script Host.

> > I need to delete a fair amount of left over empty directories among
> current
> > ones on an NT 4 Server. Doing it "manually" using the Windows Explorer
> would
> > take to much time and effort.

> > Could someone show me how to identify empty directories using the
> > FileSystemObject.DeleteFolder method or any other way.

> > Regards

> > Philip
> > --
> > ---------------------------------------------------------------------
> > Kuhn & Seal SA - Avenue de Villardin 2
> > P.o. Box 233 - 1009 Pully - Switzerland
> > Tel:+41.(0)21.721.24.24
> > Fax:+41.(0)21.721.24.29



Fri, 25 Jul 2003 23:38:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. delete empty from droplist?

2. Delete Empty Folders

3. Scheduled task to empty Outlook deleted items

4. How delete empty folder with vbscript

5. Quick Question about deleting empty folders

6. Delete Empty Folders

7. Scheduled task to empty Outlook deleted items

8. Emptying Outlook Deleted Items Folder.

9. Removing Empty Directories

10. Removing Empty Directories

11. Delete Directory in Session_OnEnd in GLOBAL.ASA

12. Script to delete files older than a specific date in a directory

 

 
Powered by phpBB® Forum Software