Page 1 of 1

Deleting All folders but not files using batch

Posted: 08 May 2009 04:02
by ayoubahmed
Hello,
I have a folder called B2D located in E drive (E:\B2D)

This folder at any point in time could have many many folders and exactly two files.

The two files names are :
Changer.cfg
Folder.cfg

The system generates folders in that E:\B2D. The folders always start like with an IMG, then they would have a random number attached to it.

I would like to know if there is some type of batch script to delete all the folders with the IMG Prefix and keep the two files.
I do not want to delete the parent folder
I do not want to copy the two files somewhere else temporarily and copy them back.

Can you help me ?
Thank you.

Posted: 08 May 2009 11:17
by avery_larry
From the command line directly:


for /d %a in (e:\b2d\*) do rd /s /q "%a"



In a batch file, you would have to use %%a instead of %a

Posted: 11 May 2009 02:02
by ayoubahmed
Thanks a lot.. it worked..

what does the %a mean in the for loop ?

Posted: 11 May 2009 07:54
by RElliott63
%A = individual folder names you would see with the command "Dir E:\b2d\*"

The For loop loops through each folder name and performs the "DO" which in this case is RD (Remove Directory) with the Name retrieved (%A) as the target of removal.

-Rick