I have a script that tries to build ItemGroupfrom all files in a specific directory, excluding files with specific names (regardless of extension).
The list of files that will be excluded initially contains file extensions, and I use Community Tasks RegexReplaceto replace the extensions with an asterisk. Then I use this list in the attribute Exclude. For some reason, files are not properly excluded, although the list seems to be correct.
To try to find the reason, I created a test script (below) that has two tasks: first it initializes two properties with a list of file templates in two different ways . The second task prints both the properties and the files resulting from using both of these properties in the attribute Exclude.
Property values look the same, but the resulting groups are different. How is this possible?
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="Init;Test" ToolsVersion="3.5">
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<Target Name="Init">
<ItemGroup>
<OriginalFilenames Include="TestDir\SampleProj.exe"/>
<OriginalFilenames Include="TestDir\SampleLib1.dll"/>
</ItemGroup>
<RegexReplace Input="@(OriginalFilenames)" Expression="\.\w+$" Replacement=".*">
<Output TaskParameter="Output" ItemName="PatternedFilenames"/>
</RegexReplace>
<PropertyGroup>
<ExcludeFilesA>TestDir\SampleProj.*;TestDir\SampleLib1.*</ExcludeFilesA>
<ExcludeFilesB>@(PatternedFilenames)</ExcludeFilesB>
</PropertyGroup>
</Target>
<Target Name="Test">
<Message Text='ExcludeFilesA: $(ExcludeFilesA)' />
<Message Text='ExcludeFilesB: $(ExcludeFilesB)' />
<ItemGroup>
<AllFiles Include="TestDir\**"/>
<RemainingFilesA Include="TestDir\**" Exclude="$(ExcludeFilesA)"/>
<RemainingFilesB Include="TestDir\**" Exclude="$(ExcludeFilesB)"/>
</ItemGroup>
<Message Text="
**AllFiles**
@(AllFiles, '
')" />
<Message Text="
**PatternedFilenames**
@(PatternedFilenames, '
')" />
<Message Text="
**RemainingFilesA**
@(RemainingFilesA, '
')" />
<Message Text="
**RemainingFilesB**
@(RemainingFilesB, '
')" />
</Target>
</Project>
Output (reformatted for clarity):
ExcludeFilesA: TestDir\SampleProj.*;TestDir\SampleLib1.*
ExcludeFilesB: TestDir\SampleProj.*;TestDir\SampleLib1.*
AllFiles:
TestDir\SampleLib1.dll
TestDir\SampleLib1.pdb
TestDir\SampleLib2.dll
TestDir\SampleLib2.pdb
TestDir\SampleProj.exe
TestDir\SampleProj.pdb
PatternedFilenames:
TestDir\SampleProj.*
TestDir\SampleLib1.*
RemainingFilesA:
TestDir\SampleLib2.dll
TestDir\SampleLib2.pdb
RemainingFilesB:
TestDir\SampleLib1.dll
TestDir\SampleLib1.pdb
TestDir\SampleLib2.dll
TestDir\SampleLib2.pdb
TestDir\SampleProj.exe
TestDir\SampleProj.pdb
Note that both ExcludeFilesAand ExcludeFilesBlook the same, but the resulting group RemainingFilesAand RemainingFilesBdifferent.
Ultimately, I want to get a list RemainingFilesAusing a template generated in the same way ExcludeFilesB. Can you suggest a way, or do I need to completely rethink my approach?