Solved

vb.net - remove empty directories

Posted on 2011-03-03
6
677 Views
Last Modified: 2012-05-11
hello there,
I have a path lets say "c:\names" that contains about 300 different directories..
I would like to create a button that will look through all of the sub-directories and remove the empty ones..
how can I do that?
0
Comment
Question by:XK8ER
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
Try this link

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22839113.html?sfQueryTermInfo=1+10+30+empti+folder+remov

Below is the accepted answer from TheLearnedOne:

Imports System.IO

Public Class DirectoryCleaner

    ''' <summary>
    ''' Search through sub-directories, and remove any empty ones.
    ''' </summary>
    ''' <param name="path">The folder path to the directory</param>
    Public Shared Sub RemoveEmptyFolders(ByVal path As String)
        Dim directory As New DirectoryInfo(path)
        For Each subDirectory As DirectoryInfo In directory.GetDirectories()
            If subDirectory.GetFiles().Length = 0 Then
                subDirectory.Delete()
            End If
        Next subDirectory
    End Sub

End Class

Example:
    DirectoryCleaner.RemoveEmptyFolders("c:\temp")

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
this is what im getting..


The directory is not empty.
>>subDirectory.Delete()
0
 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
Try the below code instead

'Here's the code:
    Public Sub CheckFolder(ByVal sourceFolderPath As String)
        Dim di As New IO.DirectoryInfo(sourceFolderPath)
        For Each directory As IO.DirectoryInfo In di.GetDirectories
            If directory.GetFiles().Length = 0 AndAlso directory.GetDirectories.Length = 0 Then
                directory.Delete()
            End If
        Next
    End Sub

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:XK8ER
Comment Utility
the new code its not removing any directories..
0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 500 total points
Comment Utility
Try this revise code:
I think this is what you need...
Private _listAllDirectories As New List(Of String)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DeleteEmptyFolder("C:\TEMP\Source Folder")
    End Sub


    Public Sub DeleteEmptyFolder(ByVal sourceFolderPath As String)
        CheckFolder(sourceFolderPath)
        For ctr As Integer = _listAllDirectories.Count - 1 To 0 Step -1
            Dim dir As New IO.DirectoryInfo(_listAllDirectories(ctr))
            If dir.GetFiles.Length = 0 And dir.GetDirectories.Length = 0 Then
                dir.Delete()
            End If
        Next
    End Sub

    'Here's the code:
    Public Sub CheckFolder(ByVal sourceFolderPath As String)
        Dim di As New IO.DirectoryInfo(sourceFolderPath)
        For Each directory As IO.DirectoryInfo In di.GetDirectories
            _listAllDirectories.Add(directory.FullName)
            CheckFolder(directory.FullName)
        Next
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
thanks a lot.. it works so good..
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now