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
Solved

vb.net - remove empty directories

Posted on 2011-03-03
6
757 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
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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

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

Accepted Solution

by:
PagodNaUtak earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…

839 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