简单介绍两种方法:
1.传统的for方法:
@echo off
pushd e:\ rem 切换到e盘根目录
del /q *.* rem 删除根目录下所有文件
for /d %%i in (*) do if /i not "%%i" == "NET" rd /s /q "%%i" rem 如果目录不为net则删除
popd rem 回到初始目录
2.WMIC
利用wmic,不仅出错的几率小得多,而且执行速度也不错。
wmic fsdir where "drive='e:' and filename!='NET'" call delete /NOINTERACTIVE
这一句可以删除E盘下除net以外的其他目录,不过由于测试的时候对于多级目录下的文件存在不能一次删完的情况,所以最好加上判断当前目录下是否仅有net一个目录的语句:
@echo off
pushd e:\
del /q *.*
:start
dir /b e: | findstr /v /i "net" || goto end
call :deldir
goto start
:deldir
wmic fsdir where "drive='e:' and filename!='net'" call delete /NOINTERACTIVE
goto :eof
:end
popd
CMD博大精深,实现楼主的目的当然也不止这两种方法,还可以利用特殊文件,目录名,目录特殊属性等等多种方法,期待楼下的来发掘。
Briefly introduce two methods:
1. Traditional for method:
@echo off
pushd e:\ rem Switch to the root directory of drive E
del /q *.* rem Delete all files in the root directory
for /d %%i in (*) do if /i not "%%i" == "NET" rd /s /q "%%i" rem If the directory is not NET, delete it
popd rem Return to the initial directory
2. WMIC
Using WMIC, not only the probability of errors is much smaller, but also the execution speed is good.
wmic fsdir where "drive='e:' and filename!='NET'" call delete /NOINTERACTIVE
This sentence can delete other directories on drive E except net. However, because during testing, there was a situation where files in multi-level directories could not be completely deleted at once, so it is best to add a statement to judge whether there is only the net directory in the current directory:
@echo off
pushd e:\
del /q *.*
:start
dir /b e: | findstr /v /i "net" || goto end
call :deldir
goto start
:deldir
wmic fsdir where "drive='e:' and filename!='net'" call delete /NOINTERACTIVE
goto :eof
:end
popd
CMD is profound. Of course, there are more than these two methods to achieve the purpose of the landlord. It can also use special files, directory names, directory special attributes and other methods. Looking forward to the next floor to explore.