Page 1 of 1

Simple Batch Code to Detect Empty Sub-folder

Posted: 30 Sep 2019 20:57
by RSquirrel
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

Re: Simple Batch Code to Detect Empty Sub-folder

Posted: 01 Oct 2019 04:06
by jfl
I first tried 'if not exist subdir\* echo empty', but this does not work :-(

So the solution is to list the sub-folder contents, and check for the presence of anything using 'findstr /r .'.
The result is in findstr's %ERRORLEVEL%.
Ex:

Code: Select all

C:\JFL\Temp\test>md Empty

C:\JFL\Temp\test>md HasFile

C:\JFL\Temp\test>echo Hello >HasFile\hello.txt

C:\JFL\Temp\test>md HasDir

C:\JFL\Temp\test>md HasDir\Subdir

C:\JFL\Temp\test>dir /b Empty | findstr /r . >NUL 2>NUL || echo empty
empty

C:\JFL\Temp\test>dir /b HasDir | findstr /r . >NUL 2>NUL || echo empty

C:\JFL\Temp\test>dir /b HasFile | findstr /r . >NUL 2>NUL || echo empty

C:\JFL\Temp\test>if not exist Empty\* echo empty

C:\JFL\Temp\test>if not exist HasFile\* echo empty

C:\JFL\Temp\test>if not exist HasDir\* echo empty

C:\JFL\Temp\test>

Re: Simple Batch Code to Detect Empty Sub-folder

Posted: 01 Oct 2019 07:56
by RSquirrel
Most of your suggested solution does not look like any Batch File code I’m used to (as I stated, I'm not a programmer). However, assuming it does what it appears to do, it seems to require the user to know the names of the sub-folders to be deleted. If so, it’s not a solution, as the user has no way of knowing the names of the \Tier2 contents to be deleted.

Re: Simple Batch Code to Detect Empty Sub-folder

Posted: 02 Oct 2019 02:36
by jfl
OK, I was showing the principle, not the exact code you need.
In your case, that would be:

Code: Select all

dir /b "C:\TIER1\TIER2" | findstr /r . >NUL 2>NUL
if errorlevel 1 ( rem The C:\TIER1\TIER2 directory is empty
  echo There were no files or folders to be deleted.
) else (          rem The C:\TIER1\TIER2 directory contains something. Delete it.
  rem First remove sub-directories
  for /d %%d in ("C:\TIER1\TIER2\*") do rd /s /q "%%~d"
  rem Then remove the remaining files
  for %%f in ("C:\TIER1\TIER2\*") do del "%%~f"
  echo The folders and their files have been deleted.
)

Re: Simple Batch Code to Detect Empty Sub-folder

Posted: 02 Oct 2019 07:15
by RSquirrel
jfl,

Thank you for taking the time to respond. This looks like something I may be able to make work (and learn something in the process).

p.s. It looks like you live in a magnificent location. Take care . . .