I have a batch file that is supposed to identify the smallest directory and delete all other direcotires only leaving the smallest one.
The identification works properly, but the removal of the other, larger folders doesn't work. Below is what I have now. What needs to be changed?
@echo off
setlocal EnableDelayedExpansion
rem Get size of all folders
set smallestSize=9999999999
for /D %%a in (*) do (
set size=0
for %%b in (%%a\*.*) do set /A size+=%%~Zb
if !size! lss !smallestSize! (
set smallestSize=!size!
set smallestName=%%a
)
)
echo Deleted folder: "%smallestName%"
pause
rem Delete all folders, except the smallest one.
for /D %%a in (*) do (
if "%%a" neq "%smallestName%" rmdir /S /Q "%%a"