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 subdirectories, if a single subdirectory is found, empty or not, that is read as the parent directory is not empty.
To ignore sub-directories and only report an absence of files, use the alternative below, this excludes directories even if they have a period in the directory name:
@Echo off Setlocal Set _folder="C:\Demo" dir /A:-D /B %_folder% >NUL 2>&1 && ( ECHO Source Folder is NOT empty. goto :ok ) Echo The folder is empty :ok
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