Do you wonder how many empty folder in your computer? Well, I do.
It is happen to me when I see the folder name and excited to open it, but then the folder is actually an empty folder. Many times until I found that my computer have alot of empty folder and I need to clean it from my computer.So instead of double click one by one folder and delete it, I decided developed simple application to help me do the job. Here the sample code of my application.
First of all, i need to choose either GUI or Console., Since I like GUI more than console, so I choose GUI.
What I needs in the GUI are:-
- The location of the folder/drive
- Label to show how many empty folder exist.
- 2 Buttons one to find empty folders, and the other one to delete the folders.
Here come my GUI application.
C# code.
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CountEmptyFolder();
}
public void CountEmptyFolder()
{
string location = textBox1.Text;
LCount.Text = "-";
button2.Enabled = false;
string folderName = Path.GetDirectoryName(location);
string[] directoryList = Directory.GetDirectories(textBox1.Text);
ArrayList emptyFolder = new ArrayList();
foreach (string a in directoryList)
{
if (Directory.Exists(a))
{
if (!Directory.EnumerateFiles(a, "*", System.IO.SearchOption.AllDirectories).Any())
{
emptyFolder.Add(a);
}
}
}
LCount.Text = emptyFolder.Count.ToString();
if (emptyFolder.Count > 0)
{
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
string location = textBox1.Text;
string folderName = Path.GetDirectoryName(location);
string[] directoryList = Directory.GetDirectories(textBox1.Text);
ArrayList emptyFolder = new ArrayList();
foreach (string a in directoryList)
{
if (Directory.Exists(a))
{
if (!Directory.EnumerateFiles(a, "*", System.IO.SearchOption.AllDirectories).Any())
{
emptyFolder.Add(a);
}
}
}
//delete empty folder
foreach (string a in emptyFolder)
{
if (Directory.Exists(a))
{
//Directory.Delete(a);
Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(a, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
}
}
MessageBox.Show("Successful delete folders.");
CountEmptyFolder();
}
The Results
Back to listing