Deleting All folders but not files using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ayoubahmed
Posts: 2
Joined: 08 May 2009 03:56

Deleting All folders but not files using batch

#1 Post by ayoubahmed » 08 May 2009 04:02

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 08 May 2009 11:17

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

ayoubahmed
Posts: 2
Joined: 08 May 2009 03:56

#3 Post by ayoubahmed » 11 May 2009 02:02

Thanks a lot.. it worked..

what does the %a mean in the for loop ?

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#4 Post by RElliott63 » 11 May 2009 07:54

%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

Post Reply