Expand MSBuild Properties Containing a Wildcard in Elements

I am trying to write an MSBuild script that will take some action (for example, print its path) on arbitrary files (set as a property on the 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> 

the script works 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"

+4
source share
1 answer

You must solve two problems:

  • $ (InputFiles) is specified as a scalar property, but you want to interpret it as an array
  • $ (InputFiles) contains the wildcards that you want to expand after converting to the list of patterns in $ (InputFiles).

It is easy to solve one of the two problems separately, but the combination of the two is actually complicated. I have one possible solution and it works, but the disadvantage is that you have to encode * * characters in the template definition.

 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="PrintNames" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <TargetDir>c:\temp\MyContent</TargetDir> <InputFilesRelativeEsc>%2A%2A\%2A.bmp;%2A%2A\%2A.txt</InputFilesRelativeEsc> </PropertyGroup> <Target Name="PrintNames"> <ItemGroup> <_TempGroup Include="$(InputFilesRelativeEsc)" /> </ItemGroup> <CreateItem Include="@(_TempGroup->'$(TargetDir)\%(Identity)')"> <Output TaskParameter="Include" ItemName="_EvaluatedGroup" /> </CreateItem> <Message Text="_EvaluatedGroup: %(_EvaluatedGroup.FullPath)" /> </Target> </Project> 

This works as follows. The InputFilesRelativeEsc property is a list of relative file templates. Wildcards are recorded (% 2A is the hexadecimal code for the asterisk). Since the encoded characters are encoded, the _TempGroup does not attempt to search and retrieve the list of files, while Include these patterns in this group. Now _TempGroup is a group consisting of two elements: **\*.bmp and **\*.txt . Now that you have a real group, you can transform it. The only complication is that the usual MSBuild launcher does not extend wildcards. You should use the older CreateItem task. The CreateItem task CreateItem actually declared obsolete by the MSBuild team, but it still works.

+6
source

Source: https://habr.com/ru/post/1393634/


All Articles