sudhakaranR87
sudhakaranR87

Reputation: 210

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?

Upvotes: 2

Views: 5176

Answers (3)

sunish surendran.k
sunish surendran.k

Reputation: 71

pushd "d:\abc\cd" && rd /s /q "d:\abc" 2>nul will delete all the files inside the folder cd.

Upvotes: 1

Ilya
Ilya

Reputation: 4689

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"

Upvotes: 0

foxidrive
foxidrive

Reputation: 41234

Test this - it should work.

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

Upvotes: 1

Related Questions