[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Remove directory

Posted on 2011-02-18
4
Medium Priority
?
345 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 2000 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 57

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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