Search code examples
windowsdirectoryis-empty

Finding all empty directories (folders) in Windows


How to find all empty directories in Windows? For Unix there exists find. So there is an out of the box solution. What is the best solution using Windows?


Solution

  • I found several solutions so far:

    1. Use Powershell to find empty directories
    2. Install Cygwin or Gnu Findtools and follow the unix approach.
    3. Use Python or some other script language, e.g. Perl

    This Powershell snippet below will search through C:\whatever and return the empty subdirectories

    $a = Get-ChildItem C:\whatever -recurse | Where-Object {$_.PSIsContainer -eq $True}
    $a | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
    

    WARNING: The above will ALSO return all directories that contain subdirectories (but no files)!

    This python code below will list all empty subdirectories

    import os;
    folder = r"C:\whatever";
    
    for path, dirs, files in os.walk(folder):
        if (dirs == files): print path