Solved

Remove directory

Posted on 2011-02-18
4
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 55

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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