Solved

vb.net - remove empty directories

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

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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