views:

1459

answers:

2

I'm trying to write a MSBuild Task that deletes the Obj directory and PDBs from my bin folder on my production build scripts and can't seem to get it to work right.

Does anyone have an example where they do this or similar, or a link to a simple example of removing files and a directory with MSBuild?

+2  A: 

If you're looking to delete an entire directory you require the RemoveDir task:

<RemoveDir Directories="Path/To/Obj" />

And if you're wanting to delete the PDB files from bin you'll want the Delete task:

<Delete Files="Path/To/Bin/MyApp.pdb" />

Note that you cannot use wildcards in the Delete task, so if you have multiple pdb files you're have to provide an ItemGroup as an argument.

Gavin Miller
Will the remove dir work with files in it?
Chris Marisic
I'm not sure... The MSBuild tasks are simply wrappers around the commands. And since rmdir in windows doesn't delete the directory unless it is empty, I'm leaning towards No.
Gavin Miller
The *RemoveDir* task reference page (see above) says all files and subdirectories will be deleted.
Yawar
I wish I could mark both of your answers because the link to the RemoveDir task showed me what was really missing was I that I didn't dereference my OutputPath property for finding the right directory. Sayeds response with the ItemGroup syntax was what I needed to do to grab all of the pdb's to delete them. Thank you both!
Chris Marisic
+3  A: 

You can delete the files in those directories first and then the dir itself with

<Target Name="SomeTarget">

<ItemGroup>
    <FilesToDelete Include="Path\To\Obj\**\*"/>
</ItemGroup>

<Delete Files="@(FilesToDelete)" />

<RemoveDir Directories="Path\To\Obj\" />
</Target>
Sayed Ibrahim Hashimi
This helped me thanks alot! Sorry I couldn't mark both of your answers at the solution, I added some more in my comment above.
Chris Marisic