Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net - remove empty directories

Posted on 2011-03-03
6
Medium Priority
?
956 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
[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
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 35033327
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
ID: 35033347
this is what im getting..


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

Expert Comment

by:PagodNaUtak
ID: 35033416
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

1
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:XK8ER
ID: 35033424
the new code its not removing any directories..
0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 2000 total points
ID: 35033572
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
ID: 35033667
thanks a lot.. it works so good..
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

660 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