I am trying to "loop" (batching) over a list of files, and string.replace the root directory name with a different directory name.
This project is using 3.5, thus cannot take full advantage of some new 4.0 features.
I am using the MSBuild.ExtensionPack.Framework.TextString task (from http://msbuildextensionpack.codeplex.com/releases/57599/download/229487 )
Bascially, I want to rewrite a filename
c:\SomeFolder\myfile.txt
to
c:\AnotherFolder\myfile.txt
I've written a mock to demonstrate the issue.
Here are the results I am getting:
"C:\Windows\twunk_16.exe" "c:\WindowsFake\write.exe"
"C:\Windows\twunk_32.exe" "c:\WindowsFake\write.exe"
"C:\Windows\winhlp32.exe" "c:\WindowsFake\write.exe"
"C:\Windows\write.exe" "c:\WindowsFake\write.exe"
You'll notice that "c:\WindowsFake\write.exe" (right side) is repeated for each item in the batch. :<
What I am trying to get:
"C:\Windows\twunk_16.exe" "c:\WindowsFake\twunk_16.exe"
"C:\Windows\twunk_32.exe" "c:\WindowsFake\twunk_32.exe"
"C:\Windows\winhlp32.exe" "c:\WindowsFake\winhlp32.exe"
"C:\Windows\write.exe" "c:\WindowsFake\write.exe"
I (kinda) understand why I am getting the repeat value. But (of course) cannot figure out how to address the issue. Don't get hung up on the "windows" "fakewindows", these are just mock examples I created, because most people have a c:\windows directory on their computer, and I needed some "grab" some files.
Again, I am using 3.5 MSBuild, not 4.0.
set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5
call %msBuildDir%\msbuild.exe Master_MSBuild.xml /p:Configuration=Release /l:FileLogger,Microsoft.Build.Engine;logfile=Master_MSBuild_LOG.log
set msBuildDir=
Here is the complete .msbuild code. (Which I have in a file called 'Master_MSBuild.xml'.)
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper">
<!-- -->
<!-- -->
<UsingTask AssemblyFile=".\MSBuild.ExtensionPack.dll" TaskName="MSBuild.ExtensionPack.Framework.TextString" />
<!-- -->
<!-- -->
<PropertyGroup>
<WindowsForRealDirectory>$(windir)</WindowsForRealDirectory>
<WindowsFakeDirectory>c:\WindowsFake</WindowsFakeDirectory>
</PropertyGroup>
<!-- -->
<!-- -->
<Target Name="DetermineAllFilesTarget">
<!-- -->
<ItemGroup>
<MyExcludedFiles Include="$(WindowsForRealDirectory)\**\notepad.exe" />
<MyExcludedFiles Include="$(WindowsForRealDirectory)\**\b*.exe" />
<MyExcludedFiles Include="$(WindowsForRealDirectory)\**\e*.exe" />
<MyExcludedFiles Include="$(WindowsForRealDirectory)\**\f*.exe" />
</ItemGroup>
<!-- -->
<ItemGroup>
<MyIncludedFiles Include="$(WindowsForRealDirectory)\Microsoft.NET\*.exe" Exclude="@(MyExcludedFiles)" />
<MyIncludedFiles Include="$(WindowsForRealDirectory)\*.exe" Exclude="@(MyExcludedFiles)" />
</ItemGroup>
<!-- -->
<!-- This create item may be redundant. -->
<CreateItem Include="@(MyIncludedFiles)">
<Output TaskParameter="Include" ItemName="FoundFolders" />
</CreateItem>
<!-- -->
<!-- -->
<Message Text="rootdir: @(FoundFolders->'%(rootdir)')" />
<Message Text="fullpath: @(FoundFolders->'%(fullpath)')" />
<Message Text="rootdir + directory + filename + extension: @(FoundFolders->'%(rootdir)%(directory)%(filename)%(extension)')" />
<Message Text="identity: @(FoundFolders->'%(identity)')" />
<Message Text="filename: @(FoundFolders->'%(filename)')" />
<Message Text="directory: @(FoundFolders->'%(directory)')" />
<Message Text="relativedir: @(FoundFolders->'%(relativedir)')" />
<Message Text="extension: @(FoundFolders->'%(extension)')" />
<!-- -->
</Target>
<!-- -->
<!-- -->
<!-- -->
<Target Name="ReplaceAndShowAllFileNames" Inputs="@(FoundFolders)" Outputs="@(FoundFolders.Identity')">
<!-- -->
<Message Text=" " />
<Message Text="FoundFolders.FileName = %(FoundFolders.FileName)" />
<Message Text="FoundFolders.FullPath = %(FoundFolders.FullPath)" />
<Message Text=" rootdir + directory + filename + extension = '%(FoundFolders.rootdir) *** %(FoundFolders.directory) *** %(FoundFolders.filename) *** %(FoundFolders.extension)' " />
<Message Text="FoundFolders.rootdir = %(FoundFolders.rootdir)" />
<Message Text="FoundFolders.directory = %(FoundFolders.directory)" />
<Message Text="FoundFolders.relativedir = %(FoundFolders.relativedir)" />
<Message Text="FoundFolders.extension = %(FoundFolders.extension)" />
<Message Text="FoundFolders.recursivedir = %(FoundFolders.recursivedir)" />
<Message Text=" " />
<Message Text=" " />
<Message Text=" " />
<!-- -->
<Message Text=" (WindowsFakeDirectory)= $(WindowsFakeDirectory)" />
<!-- -->
<!-- Replace the contents of a string -->
<MSBuild.ExtensionPack.Framework.TextString TaskAction="Replace" OldString="%(FoundFolders.relativedir)%(FoundFolders.Filename)%(FoundFolders.Extension)" OldValue="$(WindowsForRealDirectory)" NewValue="$(WindowsFakeDirectory)">
<Output PropertyName="RewrittenFileName" TaskParameter="NewString" />
</MSBuild.ExtensionPack.Framework.TextString>
<!-- -->
<CreateProperty Value="$(CheckedOutFileName)">
<Output PropertyName="CheckedOutFileNameTwo" TaskParameter="Value" />
</CreateProperty>
<!-- -->
<Message Text=" " />
<Message Text="The Result of the String.Replace: $(RewrittenFileName)" />
<!-- -->
<Message Text=" " />
<Message Text="Command: "The original and the rewritten file name: " "%(FoundFolders.relativedir)%(FoundFolders.Filename)%(FoundFolders.Extension)" "$(RewrittenFileName)"" />
<!-- -->
</Target>
<!-- -->
<!-- -->
<!-- -->
<Target Name="AllTargetsWrapper">
<!-- -->
<CallTarget Targets="DetermineAllFilesTarget" />
<!-- -->
<CallTarget Targets="ReplaceAndShowAllFileNames" />
<!-- -->
</Target>
<!-- -->
</Project>