• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Remove directory

I have a directory C:\Test\Test2\Test3\Test4

I need to programmatically delete it but only if Neither folder contains any files.

Foe example if there is one file test.txt in Test2, besides folder Test3, then I want only Teast4 and Test3 folders deleteed and I want C:\Test\Test2\test.txt left intact. But if neither directory contains any files, i want them all deleted. Is there a way to traverse through directories, checking for files and delting empty directories?


I tried
 
Dim objFSO, strFolder, strFile
strFolder= wscript.Arguments(0) 

set objFSO = createobject("Scripting.FileSystemObject")

IF objFSO.FolderExists(strFolder) Then

	If ((objFSO.GetFolder(strFolder).SubFolders.Count = 0) AND 	(objFSO.GetFolder(strFolder).Files.Count = 0)) Then
		objFSO.DeleteFolder strFolder, true
	End If
End If

Open in new window


and passed it C:\Test\Test2\Test3\Test4 as an argument
and it only removes Test4 folder, leaving C:\Test\Test2\Test3\ even though there are not any files
0
YZlat
Asked:
YZlat
  • 2
1 Solution
 
connectexCommented:
Your code doesn't attempt to delete anything but the last folder. You could use the split function with "\" to split the path. Then within a loop using the number of items that the previous split returned do the same checks and delete those folders as well. Just remember you must work your way back up the file tree.
0
 
connectexCommented:
Actually I think my first post was a bit confusing as it's actually probably hard doing it that way. It's really better to loop backwards through the folder path via InStrRev function. Here's the code I come up with.
' Variables
Dim objFSO, strFolder, strFile, bFlag, intPos

' Setup objects
set objFSO = createobject("Scripting.FileSystemObject")

' Get arguments
strFolder = WScript.Arguments(0)

' Loop through folders and delete any empty folders from bottom upwards
bFlag = True
While bFlag = True 
  If objFSO.FolderExists(strFolder) Then
    If ((objFSO.GetFolder(strFolder).SubFolders.Count = 0) And (objFSO.GetFolder(strFolder).Files.Count = 0)) Then
      WScript.Echo "Deleting " & strFolder
      objFSO.DeleteFolder strFolder, True
    Else
      WScript.Echo "Folder " & strFolder & " is not empty."
      bFlag = False
    End If
    intPos = InStrRev(strFolder, "\", -1) 		
    If intPos > 0 And Mid(strFolder, intPos-1, 1) <> ":" Then
      strFolder = Left(strFolder, intPos-1)
    Else
      bFlag = False
    End If
  Else
    WScript.Echo "ERROR - Folder " & strFolder & " doesn't exists."
    bFlag = False
  End If
Wend

Open in new window

0
 
Bill PrewCommented:
Here's some logic I have used in the past. It will remove all empty subfolders under the starting folder.  As low level empty folders are removed, then if parent folders become empty, they are removed.

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
RemoveEmptyFiles objFSO.GetFolder("c:\test")

Sub RemoveEmptyFiles(objFolder)
    ' First remove all empty subfolders of this one
    For Each objSubFolder In objFolder.Subfolders
        RemoveEmptyFiles objSubFolder
    Next

    ' If folder is now empty, remove it
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        objFolder.Delete
    End If
End Sub

Open in new window

~bp
0
 
YZlatAuthor Commented:
Thank you!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now