Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script to Delete Empty Folders

1,573 views
Skip to first unread message

carlr...@gmail.com

unread,
Jan 10, 2008, 9:42:40 AM1/10/08
to
I have a script that starts with a specific "path" and is supposed to
examine every folder beneath the path and delete any folders that are
empty. It checks to make sure there are no more subfolders and no
more files within the folder before it attempts to delete the folder.
For some reason it is not deleting the folders. I have added a few
msgbox functions to help keep track of the progress as the script runs
and, as far as I can tell, it is finding empty folders correctly but
it just isnt deleting them. Below is the script. Any advice would be
appreciated.

Dim objFSO
Dim objFolder
set fso = createobject("scripting.filesystemobject")
set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")

path = "e:\temp holding folder"

FindEmptyFolders path

Sub FindEmptyFolders(nPath)
' Verify if there is subfolder(s) and file(s) in it and delete it.
msgbox nPath
' msgbox ("Folders")
' msgbox ("Files", objFSO.GetFolder(nPath).Files.Count)
msgbox (objFSO.GetFolder(nPath).Files.Count)
msgbox (objFSO.GetFolder(nPath).SubFolders.Count)
If (objFSO.GetFolder(nPath).SubFolders.Count = 0) AND
(objFSO.GetFolder(nPath).Files.Count = 0) Then
objFSO.DeleteFolder(nPath)
End If
' If there is subfolder(s) under current folder in nPath, call
' recursively this sub until there is no other subfolder(s)
For Each objFolder In objFSO.GetFolder(nPath).Subfolders
on error resume next
FindEmptyFolders(objFolder.Path)
Next
End sub

McKirahan

unread,
Jan 10, 2008, 9:59:51 AM1/10/08
to
"carlr...@NOgmailSPAM.com" <carlr...@gmail.com> wrote in message
news:61f013b0-cc98-49c9...@d21g2000prf.googlegroups.com...

> I have a script that starts with a specific "path" and is supposed to
> examine every folder beneath the path and delete any folders that are
> empty. It checks to make sure there are no more subfolders and no
> more files within the folder before it attempts to delete the folder.
> For some reason it is not deleting the folders. I have added a few
> msgbox functions to help keep track of the progress as the script runs
> and, as far as I can tell, it is finding empty folders correctly but
> it just isnt deleting them. Below is the script. Any advice would be
> appreciated.

[snip]

> set fso = createobject("scripting.filesystemobject")

is not necessary; (you'r usng objFSO).

> path = "e:\temp holding folder"

With spaces in the path, enclose it in quotes.

If won't handle deleting a folder that contains an
empty subfolder as the subfolder would be deleted
after the test for the folder.


ekkehard.horner

unread,
Jan 10, 2008, 10:18:57 AM1/10/08
to
carlr...@NOgmailSPAM.com schrieb:

> I have a script that starts with a specific "path" and is supposed to
> examine every folder beneath the path and delete any folders that are
> empty. It checks to make sure there are no more subfolders and no
> more files within the folder before it attempts to delete the folder.
> For some reason it is not deleting the folders. I have added a few
> msgbox functions to help keep track of the progress as the script runs
> and, as far as I can tell, it is finding empty folders correctly but
> it just isnt deleting them. Below is the script. Any advice would be
> appreciated.
>
> Dim objFSO
> Dim objFolder
> set fso = createobject("scripting.filesystemobject")
> set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
>
> path = "e:\temp holding folder"

BTW, this is correct (without extra ") because the string path isn't
pathed - passed - to a command line; functions like GetFolder() handle
their params intelligenty

>
> FindEmptyFolders path
>
> Sub FindEmptyFolders(nPath)
> ' Verify if there is subfolder(s) and file(s) in it and delete it.
> msgbox nPath
> ' msgbox ("Folders")
> ' msgbox ("Files", objFSO.GetFolder(nPath).Files.Count)
> msgbox (objFSO.GetFolder(nPath).Files.Count)
> msgbox (objFSO.GetFolder(nPath).SubFolders.Count)
> If (objFSO.GetFolder(nPath).SubFolders.Count = 0) AND
> (objFSO.GetFolder(nPath).Files.Count = 0) Then
> objFSO.DeleteFolder(nPath)
> End If
> ' If there is subfolder(s) under current folder in nPath, call
> ' recursively this sub until there is no other subfolder(s)
> For Each objFolder In objFSO.GetFolder(nPath).Subfolders
> on error resume next
> FindEmptyFolders(objFolder.Path)
> Next
> End sub

You try to delete the Folders *before* you process their Subfolders.
Perhaps looking at this

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sDir : sDir = oFS.GetAbsolutePathName( ".\zaptmp\zap" )

DeleteTemp oFS.GetFolder( sDir ), 15

Sub DeleteTemp( oFld, nDays )
Dim oItem
For Each oItem In oFld.Files
WScript.Echo oItem.DateCreated, oItem.Path
If DateDiff( "d", oItem.DateCreated, Date ) > nDays Then
WScript.Echo "will delete", oItem.Path
oItem.Delete True
End If
Next
For Each oItem In oFld.SubFolders
DeleteTemp oItem, nDays
Next
If 0 = oFld.Files.Count And 0 = oFld.SubFolders.Count Then
WScript.Echo "will delete", oFld.Path
oFld.Delete True
End If
End Sub

will help.

McKirahan

unread,
Jan 10, 2008, 10:37:51 AM1/10/08
to
"McKirahan" <Ne...@McKirahan.com> wrote in message
news:P5qdnboCtecYrxva...@comcast.com...

> "carlr...@NOgmailSPAM.com" <carlr...@gmail.com> wrote in message
> news:61f013b0-cc98-49c9...@d21g2000prf.googlegroups.com...
> > I have a script that starts with a specific "path" and is supposed to
> > examine every folder beneath the path and delete any folders that are
> > empty. It checks to make sure there are no more subfolders and no
> > more files within the folder before it attempts to delete the folder.
> > For some reason it is not deleting the folders. I have added a few
> > msgbox functions to help keep track of the progress as the script runs
> > and, as far as I can tell, it is finding empty folders correctly but
> > it just isnt deleting them. Below is the script. Any advice would be
> > appreciated.
>
> [snip]
>
> > set fso = createobject("scripting.filesystemobject")
> is not necessary; (you'r usng objFSO).
>
> > path = "e:\temp holding folder"
> With spaces in the path, enclose it in quotes.

Never mind.

> If won't handle deleting a folder that contains an
> empty subfolder as the subfolder would be deleted
> after the test for the folder.


Will this help? Watch for word-wrap.

Since higher level folders may contain empty subfolders
(which will be deleted) it will continue until no more folders
are deleted.

WScript.Echo "." & nPath -- identifies each folder tested.
WScript.Echo "!" & nPath -- identifies each folder deleted.


Option Explicit
Const path = "e:\temp holding folder"
Dim booDEL
Dim strFOL
Dim objFSO
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Do
booDEL = False
Call FindEmptyFolders(path)
If Not booDEL Then Exit Do
Loop

Sub FindEmptyFolders(nPath)
WScript.Echo "? " & nPath
If objFSO.GetFolder(nPath).SubFolders.Count = 0 _
And objFSO.GetFolder(nPath).Files.Count = 0 Then
objFSO.DeleteFolder(nPath)
WScript.Echo "! " & nPath
booDEL = True
Else
For Each strFOL In objFSO.GetFolder(nPath).Subfolders
FindEmptyFolders(strFOL.Path)
Next
End If
End Sub


carlr...@gmail.com

unread,
Jan 10, 2008, 10:40:10 AM1/10/08
to
> > set fso = createobject("scripting.filesystemobject")
>
> is not necessary; (you'r usng objFSO).

Yeah, that was left over from a larger script and I just didn't bother
to delete it.

> > path = "e:\temp holding folder"
>
> With spaces in the path, enclose it in quotes.

So, your saying that the path should be ""e:\temp holding folder""
essentially I should be using double quotes?

> If won't handle deleting a folder that contains an
> empty subfolder as the subfolder would be deleted
> after the test for the folder.

Yeah, I knew that but wasn't concerned as it should just catch and
delete the root folder the next time it runs.

McKirahan

unread,
Jan 10, 2008, 10:51:06 AM1/10/08
to
"carlr...@NOgmailSPAM.com" <carlr...@gmail.com> wrote in message
news:fbd18416-198a-4c0e...@s19g2000prg.googlegroups.com...

> > > set fso = createobject("scripting.filesystemobject")
> >
> > is not necessary; (you'r usng objFSO).
>
> Yeah, that was left over from a larger script and I just didn't bother
> to delete it.
>
> > > path = "e:\temp holding folder"
> >
> > With spaces in the path, enclose it in quotes.
>
> So, your saying that the path should be ""e:\temp holding folder""
> essentially I should be using double quotes?

No; I was wrong.

> > If won't handle deleting a folder that contains an
> > empty subfolder as the subfolder would be deleted
> > after the test for the folder.
>
> Yeah, I knew that but wasn't concerned as it should just catch and
> delete the root folder the next time it runs.

Testing your code revealed that it failed if it deleted a folder
then tried to find the deleted folder's subfolders.


carlr...@gmail.com

unread,
Jan 10, 2008, 11:17:39 AM1/10/08
to
In trying to isolate the problem, I tried to just simply delete one
folder based on the "path" variable, and I am now getting a VBScript
runtime error saying Permission denied. Here is the code:

Dim objFSO
Dim objFolder


set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")

path = "e:\temp holding folder\andrea colegrove\favorites"

objFSO.DeleteFolder(Path)


Any ideas why I would be getting this? It is all run locally and,
since I am a local administator on the machine, it should have no
problem deleting these files.

McKirahan

unread,
Jan 10, 2008, 11:29:12 AM1/10/08
to
"carlr...@NOgmailSPAM.com" <carlr...@gmail.com> wrote in message
news:86ed920f-a54a-4a45...@q39g2000hsf.googlegroups.com...

I got that few times too; but it worked after repeated attempts.
I can't replicate the problem at this time.

Did you see the solution I posted earlier?

carlr...@gmail.com

unread,
Jan 10, 2008, 11:47:02 AM1/10/08
to
> I got that few times too; but it worked after repeated attempts.
> I can't replicate the problem at this time.

It fails every time for me.

> Did you see the solution I posted earlier?

Yes, but it still relies on the objFSO.DeleteFolder(nPath) function
to ultimately delete the folder and, based on my inability to even get
that to work, I don't think it will work. I will give it a go anyway
and let you know.

Thanks.

McKirahan

unread,
Jan 10, 2008, 12:51:12 PM1/10/08
to
"carlr...@NOgmailSPAM.com" <carlr...@gmail.com> wrote in message
news:6380c9a4-56ce-48d9...@v29g2000hsf.googlegroups.com...


Here's how I tested it successfully on a diskette.

1) I created the script file "FindEmptyFolders.vbs":

Option Explicit
'*
'* Declare Constants
'*
'Const path = "e:\temp holding folder"
Const path = "a:\temp dir"
'*
'* Declare Variables
'*
Dim booDEL
Dim strFOL
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
'*
'* FindEmptyFolders()
'*


Do
booDEL = False
Call FindEmptyFolders(path)
If Not booDEL Then Exit Do

WScript.Echo ":"
Loop

Sub FindEmptyFolders(nPath)
WScript.Echo ". " & nPath


If objFSO.GetFolder(nPath).SubFolders.Count = 0 _
And objFSO.GetFolder(nPath).Files.Count = 0 Then

WScript.Echo "! " & nPath
objFSO.DeleteFolder(nPath)


booDEL = True
Else
For Each strFOL In objFSO.GetFolder(nPath).Subfolders
FindEmptyFolders(strFOL.Path)
Next
End If
End Sub

2) I created the batch file "FindEmptyFolders.bat":

@echo off
echo FindEmptyFolders.bat
echo.
if exist "a:\temp dir\nul" deltree "a:\temp dir" /Y
md "a:\temp dir"
md "a:\temp dir\1"
md "a:\temp dir\1\1.1"
md "a:\temp dir\1\1.2"
echo > "a:\temp dir\1\1.2\1.txt"
md "a:\temp dir\2"
md "a:\temp dir\2\2.1"
md "a:\temp dir\3"
echo > "a:\temp dir\3\3.txt"
echo.
dir "a:\temp dir" /a/o/s | find /v "<DIR>" | find ":"


3) Then I opened up a command prompt and ran the batch file:

C:\>a:

A:\>FindEmptyFolders.bat
FindEmptyFolders.bat

Delete directory "a:\temp dir" and all its subdirectories? [yn] y
Deleting a:\temp dir...

Directory of A:\temp dir
Directory of A:\temp dir\1
Directory of A:\temp dir\1\1.1
Directory of A:\temp dir\1\1.2
1 TXT 13 01-10-08 11:42a 1.txt
Directory of A:\temp dir\2
Directory of A:\temp dir\2\2.1
Directory of A:\temp dir\3
3 TXT 13 01-10-08 11:42a 3.txt
Total files listed:
A:\>


4) Then (still at the command prompt) I ran the script file:

A:\>cscript //nologo FindEmptyFolders.vbs
. a:\temp dir
. A:\temp dir\1
. A:\temp dir\1\1.1
! A:\temp dir\1\1.1
. A:\temp dir\1\1.2
. A:\temp dir\2
. A:\temp dir\2\2.1
! A:\temp dir\2\2.1
. A:\temp dir\3
:
. a:\temp dir
. A:\temp dir\1
. A:\temp dir\1\1.2
. A:\temp dir\2
! A:\temp dir\2
. A:\temp dir\3
:
. a:\temp dir
. A:\temp dir\1
. A:\temp dir\1\1.2
. A:\temp dir\3

A:\>


5) Which revealed that three folders were deleted in 3 passes.


carlr...@gmail.com

unread,
Jan 10, 2008, 1:54:00 PM1/10/08
to
Unbelievable. I found the problem. The script is performing
flawlessly (both yours and mine). The problem is caused by a read-
only attribute on the folders we are trying to delete. These files
are typically migrated Windows XP profiles and, as such, they are
tagged with a R attribute. Whats even more amazing is that I cant
even change this attribute using Explorer. According to the below MS
article, the R attribute is usually ignored by Windows programs (such
as Explorer) but it seems that WSH does not ignore it and, thus, I was
getting permission denied errors.

http://support.microsoft.com/kb/326549/

I am going to add a line in my script to flag all of the files back to
normal using the attrib command so that it runs successfully.

Thanks so much for your help!

0 new messages