Solved

Remove directory

Posted on 2011-02-18
4
323 Views
Last Modified: 2012-05-11
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
Comment
Question by:YZlat
  • 2
4 Comments
 
LVL 13

Expert Comment

by:connectex
ID: 34930464
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
 
LVL 13

Accepted Solution

by:
connectex earned 500 total points
ID: 34930910
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 34931045
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
 
LVL 35

Author Closing Comment

by:YZlat
ID: 34943761
Thank you!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question