elEmptyDir
@ echo off&pushd "%TEMP%"&setlocal EnableDelayedExpansion
@if not exist "%~1\" exit /b 0
REM 设置要清理的路径
set "targetPath=%~1"
REM 遍历全部目录
for /f "delims=" %%D in ('dir "%targetPath%" /s/b/ad ^| sort /r') do (
set "dirPath=%%D"
REM 查抄目录是否为空
set isEmpty=1
dir /a/b "!dirPath!" | findstr .* >nul && set isEmpty=0
REM 查抄是否包罗 desktop.ini 文件
if exist "!dirPath!\desktop.ini" set isEmpty=0
REM 查抄目录属性
for %%A in ("!dirPath!") do (
set "attr=%%~aA"
echo !attr! | find "R" >nul && set isReadOnly=1 || set isReadOnly=0
echo !attr! | find "S" >nul && set isSystem=1 || set isSystem=0
echo !attr! | find "L" >nul && set isReparsePoint=1 || set isReparsePoint=0
)
REM 如果目录为空且不包罗特定属性或文件,则删除
if "!isEmpty!"=="1" if "!isReadOnly!"=="0" if "!isSystem!"=="0" if "!isReparsePoint!"=="0" (
echo Deleting empty directory: "!dirPath!"
rd "!dirPath!" >nul 2>&1
)
)
@ endlocal & popd
@exit /b 0