How To: Find out if a directory is empty

CMD Batch script to show if a folder is empty.

@Echo off
Setlocal
Set _folder="C:\Demo"
For /F %%A in ('dir /b /a %_folder%') Do (
    Echo The folder is NOT empty
    goto :ok
)
Echo The folder is empty
:ok

The script above will scan all files in the folder, which may be slow if there are many thousands of files, but it does not recursively scan through all the subfolders, if a single subfolder is found, empty or not, that is read as the parent folder is not empty.

To determine if a folder exists at all:

Set _folder="C:\Demo"
if not exist %_folder% (Echo The folder does not exist)

“The difference between false memories and true ones is the same as for jewels: it is always the false ones that look the most real, the most brilliant” ~ Salvador DalĂ­

Related:

DELTREE - Delete a folder and all subfolders/files.
Delete only empty folders and log results.

RD - Remove (or Delete) a Directory.
IF command


 
Copyright © 1999-2022 SS64.com
Some rights reserved