E:\test>dir /b /s|sort /r E:\test\c E:\test\b\b E:\test\b E:\test\a\a.txt E:\test\a E:\test>rd .\a & rd .\b & rd .\c 目录不是空的。 目录不是空的。 E:\test>dir /b /s|sort /r E:\test\b\b E:\test\b E:\test\a\a.txt E:\test\a
利用 rd 命令的特点,达到删除空目录的作用,但是谨慎使用,空目录不代表无意义。
@echo off rem ---------------------------------------------------------------------------- rem Script Name: Empty_remover rem Author Name: Tty725 rem Creation Date: 2019-11-05 rem Last Modified: 2019-11-05 rem Copyright (c)2019 rem Purpose: Delete an empty folder rem ---------------------------------------------------------------------------- if "%1"=="" (set folder=%cd%) else (set folder=%1) if not EXIST %folder% (echo directory does not exist:%folder% & goto bexit) dir %folder% /ad /b /s | sort /r > %folder%\dir_adbs.pre for /f %%a in (%folder%\dir_adbs.pre) do rd %%a dir %folder% /ad /b /s | sort /r > %folder%\dir_adbs.post :bexit exit