Identify empty sub-folders

C

Curious

In a given folder, there are many sub-folders. Is there an easy way to
find out those sub-folders that don't contain any file (empty sub-
folders)? Is there such a utility in .NET?
 
H

Henning Krause [MVP - Exchange]

Hello,

recurively iterate through the directories using Directory.GetDirectories().

In each folder call Directory.GetFiles().

Best regards,
Henning Krause
 
G

Guest

This does it:

using System.IO;

foreach (string dir in Directory.GetDirectories(<rootDir>, @"*",
SearchOption.AllDirectories))
{
if (Directory.GetFiles(dir).Length == 0)
{
// Folder empty
Console.WriteLine (dir);
}
}


Hope this helps.
 
C

Curious

There's a problem - I used UNC path (\\crpapp01\\mastershare") as root
folder and got an error: Cannot find "C:\crpapp01\\mastershare"

I used UNC path while it seems to interprete it as local folder. Any
work-around?

Details-----------------------------------------------------------------------
foreach (string dir in Directory.GetDirectories("\\crpapp01\
\mastershare", @"*", SearchOption.AllDirectories))
{
if (Directory.GetFiles(dir).Length == 0)
{
// Folder empty
//Console.WriteLine (dir);
sw.WriteLine(dir + "\n");
}
}
 
H

Henning Krause [MVP - Exchange]

Hello,

in C#, you must escape the backslash.

either use
Directory.GetDirectories(@"\\crpapp01\mastershare", @"*",
SearchOption.AllDirectories))

or
Directory.GetDirectories("\\\\crpapp01\\mastershare", @"*",
SearchOption.AllDirectories))

Best regards,
Henning Krause
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top