---
title: "利用批处理删除空文件夹"
author: "hipeace87"
date: "2009-03-25T10:39:14Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-03-25T17:13:30Z"
id: 354629
url: "https://hipeace87.iteye.com/blog/354629"
---

for /f "delims=" %%i in ('dir /ad /b /s') do call :rd\_dir\_empty "%%i"  
goto :eof

rem ======= 删除空目录 =========  
:rd\_dir\_empty  
rd %1 2&gt;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&gt;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

 

把上面文字保存到一个文本中，重命名为\*\*\*\*\*.bat，双击运行即可

对于嵌套文件夹、含空格的文件夹、末尾带点的文件夹(网上通常能搜到的批处理方法没有此项功能)均有效。  
！最好不要在c 盘使用 (c 盘中的一些空文件夹是系统创建的，另有他用)！