Delete Empty Folders 
Author Message
 Delete Empty Folders

I have several thousand folders that need to be deleted, But only if they
are empty.


Tue, 04 Oct 2005 05:34:11 GMT  
 Delete Empty Folders

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



Tue, 04 Oct 2005 06:19:34 GMT  
 Delete Empty Folders


: I have several thousand folders that need to be deleted, But only if they
: are empty.

Try this, needs full testing because of what it's doing

I've included a safety net   -   if vbNo = ..... then wscript.quit

Option Explicit
dim fso, foldersToDelete(100000), i, ii

Set fso = CreateObject("Scripting.FileSystemObject")

do
  for ii = 0 to i-1
    if vbNo = msgbox ("delete " & foldersToDelete(ii) & " ?",vbYesNo) then wscript.quit
    fso.deleteFolder (foldersToDelete(ii))
  next

  i = 0
  ListEmptyFolders("C:\My Documents\")
loop while i > 0

set fso = nothing
msgbox "complete"
'-----

sub ListEmptyFolders(folderSpec)
Dim folder

if fso.GetFolder(folderSpec).SubFolders.count = 0 _
  and fso.GetFolder(folderSpec).Files.count = 0 then
  foldersToDelete(i) = folderSpec
  i = i + 1
end if

For Each folder In fso.GetFolder(folderSpec).Subfolders
  ListEmptyFolders(folder.path)
Next
end sub



Tue, 04 Oct 2005 06:35:35 GMT  
 Delete Empty Folders
Thanks



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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.471 / Virus Database: 269 - Release Date: 4/10/2003


Tue, 04 Oct 2005 19:22:27 GMT  
 Delete Empty Folders
Thanks - Once again, you dug me out of it.

Niel

Quote:



> : I have several thousand folders that need to be deleted, But only if
they
> : are empty.

> Try this, needs full testing because of what it's doing

> I've included a safety net   -   if vbNo = ..... then wscript.quit

> Option Explicit
> dim fso, foldersToDelete(100000), i, ii

> Set fso = CreateObject("Scripting.FileSystemObject")

> do
>   for ii = 0 to i-1
>     if vbNo = msgbox ("delete " & foldersToDelete(ii) & " ?",vbYesNo) then
wscript.quit
>     fso.deleteFolder (foldersToDelete(ii))
>   next

>   i = 0
>   ListEmptyFolders("C:\My Documents\")
> loop while i > 0

> set fso = nothing
> msgbox "complete"
> '-----

> sub ListEmptyFolders(folderSpec)
> Dim folder

> if fso.GetFolder(folderSpec).SubFolders.count = 0 _
>   and fso.GetFolder(folderSpec).Files.count = 0 then
>   foldersToDelete(i) = folderSpec
>   i = i + 1
> end if

> For Each folder In fso.GetFolder(folderSpec).Subfolders
>   ListEmptyFolders(folder.path)
> Next
> end sub

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.471 / Virus Database: 269 - Release Date: 4/10/2003


Tue, 04 Oct 2005 19:23:20 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Delete Empty Folders

2. How delete empty folder with vbscript

3. Quick Question about deleting empty folders

4. Emptying Outlook Deleted Items Folder.

5. delete empty from droplist?

6. Scheduled task to empty Outlook deleted items

7. Scheduled task to empty Outlook deleted items

8. Deleting empty directories

9. Determining if folder is empty....

10. Empty folder OK, 2 more questions

11. Creating / Deleting Folders and files

12. Delete a folder using VBS

 

 
Powered by phpBB® Forum Software