computing
  • 0

Create Batch File To Check Empty Folder

  • 0

I need to create a batch file to check through a list of folders and see if it is empty. If it is empty, write the folder name to a text file.

I found a command to check if folder is empty.

To check if folder is empty.
dir “C:\*.*” /b /a | find /v “NoTlIkElY” >nul && echo NOT empty || echo empty

However, I do not know how to create a loop to check through all the folders and output to a text file.

Please help.

Thanks

Share

1 Answer

  1. Stole some of Mechanix’s code ( https://computing.net/answers/pr… ) to check on directory emptiness.

    This code is tested:

    You may need to finetune for special files (hidden, system), but it looks to be working OK:

    @echo on

    set outfile=C:\emptydirs.log
    set where2look=C:\

    del %outfile%

    for /F “tokens=*” %%a in (‘dir %where2look% /B /AD /S’) do (
    dir “%%a” /b > nul 2> nul
    if errorlevel 1 (
    echo Directory “%%a” is empty>> %outfile%
    )
    )

    • 0