删除指定目录下的文件及空文件夹

echo off
echo 正在执行数据库备份清理及转储,执行完毕自动退出,请勿强制关机!!!
rem 删除I:\BatTest\test目录下的文件及空文件夹
set delFolder=I:\BatTest\test


forfiles /s /p %delFolder% /m *.* /d -90 /c "cmd /c  echo @file | del @file"

echo %a%

for /f "tokens=*" %%a in ('dir /b /ad /s %delFolder% ^|sort /r') do rd "%%a" 2>nul

pause

rem 本次操作完毕,请验证所有执行结果!

 

删除本地所有空文件夹的bat代码  

 

 @echo off
for /f "tokens=*" %%a in ('dir /b /ad /s "目标路径"^|sort /r') do rd "%%a" 2>nul
pause
goto :eof
@echo off
for /f "delims=" %%i in ('dir /ad /b /s') do call :rd_dir_empty "%%i"
goto :eof

rem ======= 删除空目录 =========
:rd_dir_empty
rd %1 2>nul||goto :eof
set dir_route=%1
for /f "delims=" %%i in (%dir_route%) do (
    set dir_route="%%~dpi"
    for /f "delims=" %%j in ('dir /ad /b "%%~dpi"')do rd "%%~dpi%%j" 2>nul||goto :eof
)
:: 把路径最后的\去掉,以便set route_deepest="%%~dpi"能取到上一层路径
if "%dir_route:~-2,1%"=="\" set dir_route="%dir_route:~1,-2%"
if /i not "%cd%"==%dir_route% call :rd_dir_empty %dir_route%
goto :eof
 
posted @ 2014-06-04 22:50  星火卓越  阅读(1003)  评论(0编辑  收藏  举报