How To: Exclude Multiple Files From a Wildcard

We got the following question on our internal conversion discussion alias today:

Is there a way to exclude two or more items from an ItemGroup using the “exclude” attribute without using a wildcard?

It turns out the way to do this wasn’t clear from our existing MSDN Documentation. Brett, our documentation writer, has a fix in the queue for the next MSDN refresh, but in the mean time here is the answer from Dan:


          <Compile Include=”*.cs” Exclude=”a.cs;b.cs”/>


[ Author: Neil Enns ]

Comments (18)

  1. <a href=’‘></a>”> <a href=’‘>auto insurance</a>. <a href=" ">Insurance car</a>: car site insurance, The autos insurance company, compare car insurance. [url][/url] [link=]insurance quote[/link] from site .

  2. Roberto says:

    Just a note, maybe can be usefull to someone.

    I was looking for a way to include a complete folder including all subfolders and excluding several defined subfolders.

    (Zipping a proyect folder with all its posible subfolders excluding "bin" and "obj")

    knowing the wildcard ** and info from this post my line its supose to be:


    <ZipFiles Include="***.*" Exclude="**obj;**bin" />


    But thats didnt work, all the folders were included, the exclude attribute didnt work. Even with one extra * in the folders (**obj*) this didnt work.

    Instead i tried the next:


    <ZipFiles Include="***.*" Exclude="obj**;bin**" />


    And it works perfectly.

    A pair of questions:

    Is this normal behavior for **?

    Why is necesary the ** in the Include (without the ** the code dont work right) to mark the actual folder and is not necesary in the Exclude?

    And why Exclude="obj*.*" dont work?

  3. xmlviking says:

    Ok interesting that this works:












    But this does not







    The Exclude *.pdb will not exclude or ADD to the selection filter. I’m assuming that when you make the wildcard statement *.* that you can no longer exclude?  

    Is this a by design if so it kind of sucks.  


  4. Lionel says:

    You can do this (I just figured it out):






    Apparently "*" doesn’t mean "anything" in msbuild.

  5. insurance geico <a href= >geico  insurance</a> [url=]geico  insurance[/url]

  6. Scott says:

    I found a nuanced little bug in MSBuild 2.0.50727.832 today.

    My development time is balanced between Linux and Windows.  Certain habits, like directory slashes, carry between the platforms.

    Like much of the Windows stack the internal commands for dealing with paths don’t mind when / is used, so I simply use / as my directory separator even when making MSBuild scripts.

    This afternoon I discovered that







    fails to exclude the .js files from the item group, while







    behaves exactly as one would expect it to and removes all of the files ending in .js from the WebAppFiles item set.

  7. Scott says:

    Actually, the / was not the problem.  It was that RootDir was defined to have a / in it (or a , didn’t matter).  When the path is


    the exception processing did not work.  When it is


    it does.

    So, still a bug, but not the one I thought it was!

  8. Scott says:

    Ok, I take it back.  Both are problems.  A match pattern of Exclude="$(RootDir)/project/thingie/omitthis/** fails,  A match mattern of Exclude="$(RootDir)projectthingieomitthis**" works to prune the ‘omitthis’ directory tree.

    There be bugs in the handling of vs / within MSBuild ItemGroup handling.

  9. riezebosch says:

    @Lionel: I ran into the same problem trying to exclude *.vstemplate. I was able to fix this by adding the wildcard at the end: *.vstemplate*. There seems to be a bug when the extension is more than 3 characters long, and the ItemGroup task behaves different when using the wildcard.

  10. Riga says:

    Because Unzip command fails for 0-sized files and it seems there is no solution for that, I need an alternative solution. Because I can not delete these files, is there any way to exclude files with size == 0 ??



  11. Derek Deida says:

    Ahhh, the curse of the incorrect slash! I just had a similar issue where wildcard includes were working but excludes were not working. I was using "/" instead of ""…. duh….

    I'm not sure why MSBuild allows the forward slash to function at all for file paths.

  12. Carlos Rojas says:

    This way worked for me:


    <SourceDir Include="$(MSBuildThisFileDirectory)***.*" Exclude="$(MSBuildThisFileDirectory)**.svn**"/>