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
[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.
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.
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
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.
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.
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.
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?
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.
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.
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!