Windows – How to find all empty directories using batch

batchwindows

How do you find all empty directories using batch script?

I have a folder called "shows" on my desktop. Inside it has many sub directories. Some directories are empty and other have some files in them.

How can I loop though all the sub folder and print all folders which are empty?

Below is what I have tried. Issue is that it prints out all folders… even if a sub folder is not empty.

@echo off

SET "MYPATH=C:\Users\dave\Desktop\shows"

 for /d %%a in ("%mypath%\*") do (
    dir /b /s "%MYPATH%"
    pause
)
pause

Best Answer

Here is one way.

From the command line:

for /r "C:\Users\dave\Desktop\shows" /d %F in (.) do @dir /b "%F" | findstr "^" >nul || echo %~fF

Using a batch script:

@echo off
setlocal
set "myPath=C:\Users\dave\Desktop\shows"
for /r "%myPath%" /d %%F in (.) do dir /b "%%F" | findstr "^" >nul || echo %%~fF
Related Question