How to quickly check if folder is empty (.NET)?
.NetIoDirectory.Net Problem Overview
I have to check, if directory on disk is empty. It means, that it does not contain any folders/files. I know, that there is a simple method. We get array of FileSystemInfo's and check if count of elements equals to zero. Something like that:
public static bool CheckFolderEmpty(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
var folder = new DirectoryInfo(path);
if (folder.Exists)
{
return folder.GetFileSystemInfos().Length == 0;
}
throw new DirectoryNotFoundException();
}
This approach seems OK. BUT!! It is very, very bad from a perspective of performance. GetFileSystemInfos() is a very hard method. Actually, it enumerates all filesystem objects of folder, gets all their properties, creates objects, fills typed array etc. And all this just to simply check Length. That's stupid, isn't it?
I just profiled such code and determined, that ~250 calls of such method are executed in ~500ms. This is very slow and I believe, that it is possible to do it much quicker.
Any suggestions?
.Net Solutions
Solution 1 - .Net
There is a new feature in Directory
and DirectoryInfo
in .NET 4 that allows them to return an IEnumerable
instead of an array, and start returning results before reading all the directory contents.
public bool IsDirectoryEmpty(string path)
{
IEnumerable<string> items = Directory.EnumerateFileSystemEntries(path);
using (IEnumerator<string> en = items.GetEnumerator())
{
return !en.MoveNext();
}
}
EDIT: seeing that answer again, I realize this code can be made much simpler...
public bool IsDirectoryEmpty(string path)
{
return !Directory.EnumerateFileSystemEntries(path).Any();
}
Solution 2 - .Net
Here is the extra fast solution, that I finally implemented. Here I am using WinAPI and functions FindFirstFile, FindNextFile. It allows to avoid enumeration of all items in Folder and stops right after detecting the first object in the Folder. This approach is ~6(!!) times faster, than described above. 250 calls in 36ms!
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct WIN32_FIND_DATA
{
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll")]
private static extern bool FindClose(IntPtr hFindFile);
public static bool CheckDirectoryEmpty_Fast(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
if (Directory.Exists(path))
{
if (path.EndsWith(Path.DirectorySeparatorChar.ToString()))
path += "*";
else
path += Path.DirectorySeparatorChar + "*";
WIN32_FIND_DATA findData;
var findHandle = FindFirstFile(path, out findData);
if (findHandle != INVALID_HANDLE_VALUE)
{
try
{
bool empty = true;
do
{
if (findData.cFileName != "." && findData.cFileName != "..")
empty = false;
} while (empty && FindNextFile(findHandle, out findData));
return empty;
}
finally
{
FindClose(findHandle);
}
}
throw new Exception("Failed to get directory first file",
Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
}
throw new DirectoryNotFoundException();
}
I hope it will be useful for somebody in the future.
Solution 3 - .Net
You could try Directory.Exists(path)
and Directory.GetFiles(path)
- probably less overhead (no objects - just strings etc).
Solution 4 - .Net
private static void test()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
string [] dirs = System.IO.Directory.GetDirectories("C:\\Test\\");
string[] files = System.IO.Directory.GetFiles("C:\\Test\\");
if (dirs.Length == 0 && files.Length == 0)
Console.WriteLine("Empty");
else
Console.WriteLine("Not Empty");
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}
This quick test came back in 2 milliseconds for the folder when empty and when containing subfolders & files (5 folders with 5 files in each)
Solution 5 - .Net
I use this for folders and files (don't know if it's optimal)
if(Directory.GetFileSystemEntries(path).Length == 0)
Solution 6 - .Net
If you don't mind leaving pure C# and going for WinApi calls, then you might want to consider the PathIsDirectoryEmpty() function. According to the MSDN, the function:
> Returns TRUE if pszPath is an empty directory. Returns FALSE if pszPath is not a directory, or if it contains at least one file other than "." or "..".
That seems to be a function which does exactly what you want, so it is probably well optimised for that task (although I haven't tested that).
To call it from C#, the pinvoke.net site should help you. (Unfortunately, it doesn't describe this certain function yet, but you should be able to find some functions with similar arguments and return type there and use them as the basis for your call. If you look again into the MSDN, it says that the DLL to import from is shlwapi.dll
)
Solution 7 - .Net
I don't know about the performance statistics on this one, but have you tried using the Directory.GetFiles()
static method ?
It returns a string array containing filenames (not FileInfos) and you can check the length of the array in the same way as above.
Solution 8 - .Net
I'm sure the other answers are faster, and your question asked for whether or not a folder contained files or folders... but I'd think most of the time people would consider a directory empty if it contains no files. ie It's still "empty" to me if it contains empty subdirectories... this may not fit for your usage, but may for others!
public bool DirectoryIsEmpty(string path)
{
int fileCount = Directory.GetFiles(path).Length;
if (fileCount > 0)
{
return false;
}
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
if (! DirectoryIsEmpty(dir))
{
return false;
}
}
return true;
}
Solution 9 - .Net
Easy and simple:
public static bool DirIsEmpty(string path) {
int num = Directory.GetFiles(path).Length + Directory.GetDirectories(path).Length;
return num == 0;
}
Solution 10 - .Net
You will have to go the hard drive for this information in any case, and this alone will trump any object creation and array filling.
Solution 11 - .Net
I'm not aware of a method that will succinctly tell you if a given folder contains any other folders or files, however, using:
Directory.GetFiles(path);
&
Directory.GetDirectories(path);
should help performance since both of these methods will only return an array of strings with the names of the files/directories rather than entire FileSystemInfo objects.
Solution 12 - .Net
Thanks, everybody, for replies. I tried to use Directory.GetFiles() and Directory.GetDirectories() methods. Good news! The performance improved ~twice! 229 calls in 221ms. But also I hope, that it is possible to avoid enumeration of all items in the folder. Agree, that still the unnecessary job is executing. Don't you think so?
After all investigations, I reached a conclusion, that under pure .NET further optimiation is impossible. I am going to play with WinAPI's FindFirstFile function. Hope it will help.
Solution 13 - .Net
Some time you might want to verify whether any files exist inside sub directories and ignore those empty sub directories; in this case you can used method below:
public bool isDirectoryContainFiles(string path) {
if (!Directory.Exists(path)) return false;
return Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Any();
}
Solution 14 - .Net
Based in Brad Parks code:
public static bool DirectoryIsEmpty(string path)
{
if (System.IO.Directory.GetFiles(path).Length > 0) return false;
foreach (string dir in System.IO.Directory.GetDirectories(path))
if (!DirectoryIsEmpty(dir)) return false;
return true;
}
Solution 15 - .Net
Since you are going to work with a DirectoryInfo object anyway, I'd go with an extension
public static bool IsEmpty(this DirectoryInfo directoryInfo)
{
return directoryInfo.GetFileSystemInfos().Count() == 0;
}
Solution 16 - .Net
My code is amazing it just took 00:00:00.0007143 less than milisecond with 34 file in folder
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
bool IsEmptyDirectory = (Directory.GetFiles("d:\\pdf").Length == 0);
sw.Stop();
Console.WriteLine(sw.Elapsed);
Solution 17 - .Net
Here is something that might help you doing it. I managed to do it in two iterations.
private static IEnumerable<string> GetAllNonEmptyDirectories(string path)
{
var directories =
Directory.EnumerateDirectories(path, "*.*", SearchOption.AllDirectories)
.ToList();
var directoryList =
(from directory in directories
let isEmpty = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories).Length == 0
where !isEmpty select directory)
.ToList();
return directoryList.ToList();
}
Solution 18 - .Net
Use this. It's simple.
Public Function IsDirectoryEmpty(ByVal strDirectoryPath As String) As Boolean
Dim s() As String = _
Directory.GetFiles(strDirectoryPath)
If s.Length = 0 Then
Return True
Else
Return False
End If
End Function