简体   繁体   中英

How to Delete all folders inside a folder except one in windows batch script?

I tried to delete a files and folder inside a folder except one ,but so far I didn't get a perfect answer for it?

Can any one help on this above?

My folder structure seems like this:

I have ABC Folder under E:\\ Inside that folder I have 4 files named A.txt,B.txt,C.txt & D.txt and I have 3 Folders in that they are AB , BC & CD ...I want to remove all files and folders in ABC folder except CD folder inside ABC folder..

Can any one please help on the above?

测试一下-应该可以。

pushd "d:\abc\cd" && rd /s /q "d:\abc" 2>nul

按下“ d:\\ abc \\ cd” && rd / s / q“ d:\\ abc” 2> nul将删除cd文件夹内的所有文件。

Since you know exactly yout directory tree, you can just remove all element you don't need:

del e:\abc\ab\*.*
rmdir e:\abc\ab
del e:\abc\bc\*.*
rmdir e:\abc\bc

More general solution:

for /D %%d in (e:\abc\*) do if "%%d" neq "CD" rmdir "%%d"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM