# need a simple batch file



## theduke71 (May 8, 2010)

Hi guys
Can anyone help me out with a really simple batch file 
I just need to trawl through a root directory and delete all subsidiary folders called "metadata" and their contents.
Would be greatly appreciated.

Cheers
Rob


----------



## Ent (Apr 11, 2009)

@echo off
CD _Starting Directory_
for /R /D %%M IN (*) do (
if /I %%~nM equ metadata rmdir /s /q %%M)


----------



## theduke71 (May 8, 2010)

Im not sure if I have done something wrong or didnt explain my problem very well.
I need to start from a folder.
Lets say C:\temp which will be where I test
Within that folder there are other folders of different names. And within those folder are other folders that each contain the folder metadata.
I need to delete all the metadata folders and their contents.
What have I done wrong?

Thanks 
Rob


----------



## Ent (Apr 11, 2009)

That batch file will simply delete every folder, and its contents, whose name is metadata and is within the starting folder. If you wanted to clear "metadata" folders from C:\temp then you'd change the CD line to cd C:\temp, or delete that line and run the batch from C:\temp. By way of explanation

@echo off -- stops the computer flooding your screen and slowing itself down by telling you every file it investigates

CD _Starting Directory -- sets the folder you want to clear of metadata._

for /R /D %%M IN (*) do ( -- /R means that it examines sub folders. /D means that it looks at folders rather than files. the for loop goes through every folder and subfolder, placing it's name in the variable %%M

if /I %%~nM equ metadata rmdir /s /q %%M) -- Compares the file name of M (without the path) to metadata, and removes the folder if it matches. /s and /q get rid of all files inside and avoid asking for confirmation.

By the way, what OS are you using. I've tested this on Vista and XP.


----------



## Squashman (Apr 4, 2003)

Hey Duke, It looks like Ent has it correct as far as I can tell.

His batch file should walk the directory tree from the current directory and deletes all folders named metadata. Is that not what you want?

The only other thing I would do to change it would be to put quotes around the Loop Variable when deleting the directory in case your folder structure has spaces in the folder names.

I also prefer to use pushd and popd to change the working directory.


```
@echo off

set _Directory=C:\Temp
pushd "%_Directory%"
for /R /D %%M IN (*) do (
     if /I %%~nM equ metadata rmdir /s /q "%%M"
)
popd
```


----------



## theduke71 (May 8, 2010)

Thanks for all your help
I am using windows 7 in answer to an earlier question

I have worked out that the problem is that the Metadata folder is hidden
When i deselect hidden the batch works 

Any ideas


----------



## Squashman (Apr 4, 2003)

The For Loop is not listing the hidden folders. I got a couple of ideas but have to test it out when I get a chance. At work now.

Can probably force the folder name at the end of the current Loop variable and then do an IF exist. If it exists then delete it.


----------



## theduke71 (May 8, 2010)

I assume you need to include the attrib command in there somewhere to clear the hidden folder attribute and then delete.
I haven't quite worked out how to put it all together. Im very much still a newbie

Cheers
Rob


----------



## Squashman (Apr 4, 2003)

Nope. You don't have to.


----------



## TheOutcaste (Aug 8, 2007)

This should do the trick:

```
@Echo Off
PushD "C:\Temp"
For /F "Tokens=* Delims=" %%M IN ('Dir /AD /B /S metadata^|Sort /R') Do RD /Q /S "%%M"
PopD
```


----------



## theduke71 (May 8, 2010)

Perfect!
That worked a charm
Thanks so much


----------

