Welcome to MSDN Blogs Sign in | Join | Help

Why Doesn't Delete Take Wildcards?

Here's another question from our internal conversion discussion alias that came through last week:

Does the Delete task in MSBuild not use wildcards? The documentation doesn’t say anything about it but that seems to be the behavior I am seeing. I can always create an ItemList then pass that into delete, but I wanted to verify this was “right”.

The person asking the question is correct: the right way to do this is to create an ItemGroup that contains the list of items you want to delete. Generally you'll do this by specifying each file individually, although you can use a wildcard in the ItemGroup's Include attribute to pick up files as well. You then pass this ItemGroup into the delete task to perform the delete.

Why do we do it this way? We really wanted to ensure people passed around strongly-typed lists of objects through the build process, so things like metadata can flow through the entire build. If you have an individual task like delete take wildcards, people will tend to use the wildcards in the task directly instead of creating a strongly-typed group first.

Of course, sometimes you can't know ahead of time what the item group will be, and that's why we have the CreateItem task (which is, as we've found during the conversion of our internal build process, often required and never very pretty...).

[ Author: Neil Enns ]

Published Thursday, March 09, 2006 11:01 PM by msbuild
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Thursday, June 15, 2006 6:00 AM by Oleg B

# re: Why Doesn't Delete Take Wildcards?

Article could be much more useful if it contained example of how to do it properly, because people keep wasting time looking for it.
Friday, November 10, 2006 6:05 AM by Alexander M

# re: Why Doesn't Delete Take Wildcards?

Here's a sample I cooked up:

<ItemGroup>

<TempFiles Include="C:\Build\TempDir\*" />

</ItemGroup>

<Target Name="Clean">

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

</Target>

Wednesday, December 06, 2006 4:11 PM by rpreston

# re: Why Doesn't Delete Take Wildcards?

This doesn't seem to be working for me, but I don't get any errors. Does anyone happen to see why?

Here's my target.

<ItemGroup >

<FilesToDelete Include="$(ConfigBinDebugDirectory)\VCCustomSteps1.*"/>

</ItemGroup>

<Target Name="CleanVCCustomSteps1" >

<!-- Delete all VCCustomSteps1 output so the build events will be triggered -->

<Delete

Condition=" '%(ConfigurationToBuild.PlatformToBuild)'=='Win32' And '%(ConfigurationToBuild.FlavorToBuild)'=='Debug'"

Files="@(FilesToDelete)">

<Output TaskParameter="DeletedFiles" PropertyName="DeletedVCCS1Files" />

</Delete>

<Message Text="Config bin debug dir = $(ConfigBinDebugDirectory)"/>

<Message Text="Config bin release dir = $(ConfigBinReleaseDirectory)"/>

<Message Text="Deleted files: @(DeletedVCCS1Files)"/>

</Target>

Here's the output in the build log:

Target CleanVCCustomSteps1:

   Config bin debug dir = X:\Build\Ascent\NightlyBuild\BuildType\..\Sources\..\Binaries\Win32\Debug

   Config bin release dir = X:\Build\Ascent\NightlyBuild\BuildType\..\Sources\..\Binaries\Win32\Release

   Deleted files:

Tuesday, May 29, 2007 5:55 PM by David Thielen

# re: Why Doesn't Delete Take Wildcards?

I just use an Exec like this:

<Exec Command="del wix\*.wxs"/>

Works great and don't have to put in 5 lines of harder to understand nodes when one clear one will do. I do the same for copy - makes the .proj file a LOT easier to understand and easier to understand means fewer bugs.

Sunday, September 02, 2007 10:53 AM by Markus Schmidt

# re: Why Doesn't Delete Take Wildcards?

I have some experience with NAnt, and because I wanted an easy solution for my co-developers I just wanted to give MSBuild a try for some trivial build task (because it comes with .NET Framework so everybody already has it).

I stumbled across this issue, and while I could somehow understand why there is no wildcard support for Delete, I think the way ItemGroups were implemented is pretty unusable:

If I want to copy or delete files that were generated by my build task - which should not only be common but the normal case in a build script! - they won't get copied or deleted because the wildcard for the ItemGroup's Includes seems to be resolved at the start of the whole script, when the files are not yet existing, and not at the first or every time the ItemGroup is used.

At least you comment on CreateTask showed me some kind of workaround, but as you already stated it's "not very pretty", e. g. because it takes just one Include attribute where I have to put everything in separated by semicolons which of course looks awful and is simply unhandy compared to a list of Include nodes.

Honestly, I would understand such issues to some degree if you would have been the first to create a build tool like this. But since Ant and NAnt have already been around much longer than MSBuild and seem to do the job much better, I really wonder how it was possible to create such an inefficient solution...

Too bad, I hoped to see some improvements on NAnt which is far from being perfect as well... but at least behaves consistently.

Friday, September 07, 2007 1:28 PM by Sumanth Kollipara

# re: Why Doesn't Delete Take Wildcards?

You can use CreateItem instead of ItemGroups. Here is a reference to Brennan's blog.

Thursday, January 10, 2008 1:49 PM by GLM

# re: Why Doesn't Delete Take Wildcards?

Hi. I want to accomplish this:

In an after build target I do copy some ascx files to another folder. In the AfterClean i'd like to delete those too.

I cant figure out how to tell delete the files in the ItemGroup are on another path.

CreateItem doesn't let me modify the name, i tryed concat but it fails.

Any ideas?

# Automating the build with MSBuild &laquo; Coding Cockerel

Wednesday, May 21, 2008 12:16 PM by Boomer

# re: Why Doesn't Delete Take Wildcards?

Thanks for the tip about using the exec command

"I just use an Exec like this:

<Exec Command="del wix\*.wxs"/>"

However, I get a strange behavior in VS 2008.

The following works fine

<Exec Command="del $(ReleaseDirectory)\*.txt" />

But it won't take a double wildcard

<Exec Command="del $(ReleaseDirectory)\*.*" />

or a single wildcard like this

<Exec Command="del $(ReleaseDirectory)\*" />

Returning

The command "del ProjectRelease\*.*" exited with code 1.

I don't know if I am doing something wrong, but I just thought I would mention this in case anyone else sees this behavior.

Friday, August 08, 2008 3:18 PM by kruss1971

# re: Why Doesn't Delete Take Wildcards?

@Boomer

to delete directory/* or directory/*.*, you are basically deleting the directory

<RemoveDir Directories="$(OutputPath)\dir" />

value can be semi-colon delimited string of directories

if you need to add it back in, just use

<MakeDir Directories="$(OutputPath)\dir" />

now you've only used two lines, got around the exec bug, and kept yourself from using all of the steps needed for ItemGroup

# 11:60 p.m. &raquo; Blog Archive &raquo; phonet4n can be downloaded from sourceforge

Tuesday, February 17, 2009 8:48 AM by Mike

# re: Why Doesn't Delete Take Wildcards?

This post in incorrect, if you use wildcards in the include attribute of a tag in an itemgroup, the delete task will not work either!

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker