Back to Insights

MSBuild well known metadata

There are a bunch of super handy metadata in MSBuild for referencing pieces of folder and file names when using Item Collections, and I found a complete list from the guys at C# Online.NET.

Say for example, you are selecting a bunch of files as follows:

<CreateItem Include="C:\MyFolder\**\*">
  <Output ItemName="AllFiles" TaskParameter="Include"/>
</CreateItem>

Metadata

In this case, we are searching a folder called C:\MyFolder, and if we have a sub-folder called MySubDir containing a file called MyFile.txt, then the metadata items are:

You can refernce any of these metadata in your MSBuild script using the syntax %(AllFiles.Blah), substituting the 'Blah' for the metadata you need.

Wildcards

The wildcards available are: * - Match 0 or many characters. ** - Recursively search all sub-folders. ? - Match a single character.

Deleting sub-folders, but not files

So, as an example, today I needed to delete all of the sub-folders in a folder, but not the files. Here's the MSBuild — making sure to Exclude the files using the Exclude attribute:

<!-- Remove the sub-folders of MyFolder -->
<CreateItem Include="C:\MyFolder\**\*" Exclude="C:\MyFolder\*">
  <Output ItemName="AllSubDirs" TaskParameter="Include"/>
</CreateItem>
<Message Text="Removing sub-folders..." />
<RemoveDir Directories="%(AllSubDirs.RootDir)%(AllSubDirs.Directory)" />

This post can also be found on my personal blog.

Share