JSI Tip 8300. How can I delete empty sub-folders in a directory tree?

NOTE: See A better way to delete empty sub-folders in a directory tree.

If have scripted DelMT.bat to delete empty sub-folders in a directory tree?

The syntax for using DelMT.bat is:

DelMT Directory

Where Directory is the top level directory use wish to purge of empty sub-folders.

NOTE: DelMT.bat displays the sub-folders that it deletes on the CMD.EXE console window.

NOTE: For non-English versions of Windows, replace File and Dir in the Findstr command.

DelMT.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax DelMT Folder&goto :EOF
if not exist %1 @echo Syntax DelMT Folder - %1 not found.&goto :EOF
set folder=%1
set delSW=N
if exist "%TEMP%\DelMT.TM1" del /q "%TEMP%\DelMT.TM1"
if exist "%TEMP%\DelMT.TM2" del /q "%TEMP%\DelMT.TM2"
set /a cnt=10000000
for /f "Tokens=*" %%f in ('dir %folder% /AD /S /b') do (
 call :IsMT1 "%%f"
if not exist "%TEMP%\DelMT.TM1" endlocal&goto :EOF
sort /R "%TEMP%\DelMT.TM1" /O "%TEMP%\DelMT.TM2"
del /q "%TEMP%\DelMT.TM1"
for /f "Tokens=1*" %%f in ('type "%TEMP%\DelMT.TM2"') do (
 call :IsMT2 %%g
del /q "%TEMP%\DelMT.TM2"
goto :EOF
set /a cnt=%cnt% + 1
@echo %cnt% %1>>"%TEMP%\DelMT.TM1"
goto :EOF
pushd %1
set /a numb=0
for /f "Tokens=*" %%a in ('dir /a^|findstr /L /c:" 0 File" /c:" 2 Dir"') do (
 set /a numb=!numb! + 1
if %numb% NEQ 2 goto :EOF
rd /q %1 
@echo Deleted %1

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.