I am trying to write MSBuild script that will perform some action (eg. print its path) on an arbitrary files (specified as a property on a command line) in some predefined directory (F:\Files).
Given the following directory structure
F:\Files\TextFile.txt
F:\Files\Subdir1\ImageFile.bmp
F:\Files\Subdir1\SubSubdir\ImageFile2.bmp
F:\Files\Subdir1\SubSubdir\TextFile2.txt
And MSBuild Script
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="PrintNames" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetDir>F:\Files</TargetDir>
</PropertyGroup>
<ItemGroup>
<Files Include="$(TargetDir)\$(InputFiles)"/>
</ItemGroup>
<Target Name="PrintNames">
<Message Text="Files: @(Files, ', ')" />
</Target>
</Project>
running the script with InputFiles set to “**\*.bmp;**\*.txt” works fine only for bmp files. Txt files are taken from the current working directory, not from “F:\Files”
There are two problems that you have to solve:
It is easy to solve either of two problems separately, but combination of the two is actually tricky. I have one possible solution, and it works, but the downside is you have to encode ‘*’ characters in your pattern definition.
It works as follows. Property
InputFilesRelativeEscis a list of relative file patterns. Notice wildcard characters are encoded (%2A is a hex code for asterisk). Since the wildcards encoded, the group_TempGroupdoes not attempt to search for and extract list of files while youIncludethis patterns into this group. Now_TempGroupis a group which consists of two elements:**\*.bmpand**\*.txt. Now that you have a real group you can transform it. The only complication is that the normal MSBuild mechanism of running transform does not expand wildcards. You have to use olderCreateItemtask. TheCreateItemtask is actually declared deprecated by MSBuild team, but it still works.