Simple Batch Code to Detect Empty Sub-folder
Posted: 30 Sep 2019 20:57
I have a very simple batch file that deletes all of the folders in C:\TIER1\TIER2 with consistent results, and then informs the user that “The folders and their files have been deleted.” \TIER2 can contain several sub-folders that can accrue over time, or it should be able to be emptied by the user, via this batch file.
What I’m lacking is a means of detecting when C:\TIER1\TIER2 is EMPTY (i.e. when the process above has been previously implemented, resulting in \TIER2 containing no sub-folders) so that the user will be informed that “There were no files or folders to be deleted.” BTW-The path C:\TIER1\TIER2 is constant.
I’ve attempted to adapt various scripts that I’ve found on the web, but I can't seem to get any to work (I’m not proficient in any programming language, so some simple as possible code in a batch file is what I’m looking for). Any help would be greatly appreciated. Thanks in advance . . .
Right now I have this code:
RD /S /Q C:\ TIER1\TIER2 >NUL
MD C:\ TIER1\TIER2 >NUL
REM Yes, I know there’s probably a better way than deleting the
REM folder and then replacing it, but this isn’t what I need help with.
GOTO FIN
:FIN
ECHO The folders and their files have been deleted.
PAUSE
GOTO LEAVE
What I’m hoping to add (ahead of the code above) is something like this:
IF “C:\TIER1\TIER2” is empty GOTO EMPTY
With the following appropriately placed at the end:
:EMPTY
ECHO There were no files or folders to be deleted.
PAUSE
:LEAVE
EXIT
What I’m lacking is a means of detecting when C:\TIER1\TIER2 is EMPTY (i.e. when the process above has been previously implemented, resulting in \TIER2 containing no sub-folders) so that the user will be informed that “There were no files or folders to be deleted.” BTW-The path C:\TIER1\TIER2 is constant.
I’ve attempted to adapt various scripts that I’ve found on the web, but I can't seem to get any to work (I’m not proficient in any programming language, so some simple as possible code in a batch file is what I’m looking for). Any help would be greatly appreciated. Thanks in advance . . .
Right now I have this code:
RD /S /Q C:\ TIER1\TIER2 >NUL
MD C:\ TIER1\TIER2 >NUL
REM Yes, I know there’s probably a better way than deleting the
REM folder and then replacing it, but this isn’t what I need help with.
GOTO FIN
:FIN
ECHO The folders and their files have been deleted.
PAUSE
GOTO LEAVE
What I’m hoping to add (ahead of the code above) is something like this:
IF “C:\TIER1\TIER2” is empty GOTO EMPTY
With the following appropriately placed at the end:
:EMPTY
ECHO There were no files or folders to be deleted.
PAUSE
:LEAVE
EXIT