title: MSBUILD Batching (Looping) Over Files and String Replacing the FIleName(s)
body: <p>I am trying to "loop" (batching) over a list of files, and string.replace the root directory name with a different directory name.</p>
  
  <p>This project is using 3.5, thus cannot take full advantage of some new 4.0 features.</p>
  
  <p>I am using the 
  MSBuild.ExtensionPack.Framework.TextString 
  task (from 
  <a href="http://msbuildextensionpack.codeplex.com/releases/57599/download/229487" rel="nofollow">http://msbuildextensionpack.codeplex.com/releases/57599/download/229487</a>
  )</p>
  
  <p>Bascially, I want to rewrite a filename</p>
  
  <pre><code>c:\SomeFolder\myfile.txt
  to
  c:\AnotherFolder\myfile.txt
  </code></pre>
  
  <p>I've written a mock to demonstrate the issue.</p>
  
  <p>Here are the results I am getting:</p>
  
  <pre><code>"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"
  </code></pre>
  
  <p>You'll notice that "c:\WindowsFake\write.exe" (right side) is repeated for each item in the batch.
  :&lt;</p>
  
  <p>What I am trying to get:</p>
  
  <pre><code>"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"
  </code></pre>
  
  <p>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.</p>
  
  <p>Again, I am using 3.5 MSBuild, not 4.0.</p>
  
  <pre><code>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=
  </code></pre>
  
  <p>Here is the complete .msbuild code. (Which I have in a file called 'Master_MSBuild.xml'.)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper"&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;UsingTask AssemblyFile=".\MSBuild.ExtensionPack.dll" TaskName="MSBuild.ExtensionPack.Framework.TextString" /&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;PropertyGroup&gt;
      &lt;WindowsForRealDirectory&gt;$(windir)&lt;/WindowsForRealDirectory&gt;
      &lt;WindowsFakeDirectory&gt;c:\WindowsFake&lt;/WindowsFakeDirectory&gt;
    &lt;/PropertyGroup&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;Target Name="DetermineAllFilesTarget"&gt;
      &lt;!--  --&gt;
      &lt;ItemGroup&gt;
        &lt;MyExcludedFiles Include="$(WindowsForRealDirectory)\**\notepad.exe" /&gt;
        &lt;MyExcludedFiles Include="$(WindowsForRealDirectory)\**\b*.exe" /&gt;      
        &lt;MyExcludedFiles Include="$(WindowsForRealDirectory)\**\e*.exe" /&gt;          
        &lt;MyExcludedFiles Include="$(WindowsForRealDirectory)\**\f*.exe" /&gt;    
      &lt;/ItemGroup&gt;
      &lt;!--  --&gt;
      &lt;ItemGroup&gt;
        &lt;MyIncludedFiles Include="$(WindowsForRealDirectory)\Microsoft.NET\*.exe" Exclude="@(MyExcludedFiles)" /&gt;
        &lt;MyIncludedFiles Include="$(WindowsForRealDirectory)\*.exe" Exclude="@(MyExcludedFiles)" /&gt;
      &lt;/ItemGroup&gt;
      &lt;!--  --&gt;
      &lt;!-- This create item may be redundant. --&gt;
      &lt;CreateItem Include="@(MyIncludedFiles)"&gt;
        &lt;Output TaskParameter="Include" ItemName="FoundFolders" /&gt;
      &lt;/CreateItem&gt;
      &lt;!--  --&gt;
      &lt;!--  --&gt;
      &lt;Message Text="rootdir: @(FoundFolders-&amp;gt;'%(rootdir)')" /&gt;
      &lt;Message Text="fullpath: @(FoundFolders-&amp;gt;'%(fullpath)')" /&gt;
      &lt;Message Text="rootdir + directory + filename + extension: @(FoundFolders-&amp;gt;'%(rootdir)%(directory)%(filename)%(extension)')" /&gt;
      &lt;Message Text="identity: @(FoundFolders-&amp;gt;'%(identity)')" /&gt;
      &lt;Message Text="filename: @(FoundFolders-&amp;gt;'%(filename)')" /&gt;
      &lt;Message Text="directory: @(FoundFolders-&amp;gt;'%(directory)')" /&gt;
      &lt;Message Text="relativedir: @(FoundFolders-&amp;gt;'%(relativedir)')" /&gt;
      &lt;Message Text="extension: @(FoundFolders-&amp;gt;'%(extension)')" /&gt;
      &lt;!--  --&gt;
    &lt;/Target&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;Target Name="ReplaceAndShowAllFileNames" Inputs="@(FoundFolders)" Outputs="@(FoundFolders.Identity')"&gt;
      &lt;!--  --&gt;
      &lt;Message Text="   " /&gt;
      &lt;Message Text="FoundFolders.FileName = %(FoundFolders.FileName)" /&gt;
      &lt;Message Text="FoundFolders.FullPath = %(FoundFolders.FullPath)" /&gt;
      &lt;Message Text="   rootdir + directory + filename + extension = '%(FoundFolders.rootdir)  ***  %(FoundFolders.directory)  ***  %(FoundFolders.filename)  ***  %(FoundFolders.extension)'  " /&gt;
      &lt;Message Text="FoundFolders.rootdir = %(FoundFolders.rootdir)" /&gt;
      &lt;Message Text="FoundFolders.directory = %(FoundFolders.directory)" /&gt;
      &lt;Message Text="FoundFolders.relativedir = %(FoundFolders.relativedir)" /&gt;
      &lt;Message Text="FoundFolders.extension = %(FoundFolders.extension)" /&gt;
      &lt;Message Text="FoundFolders.recursivedir = %(FoundFolders.recursivedir)" /&gt;
      &lt;Message Text="   " /&gt;
      &lt;Message Text="   " /&gt;
      &lt;Message Text="   " /&gt;
      &lt;!--  --&gt;
      &lt;Message Text="   (WindowsFakeDirectory)= $(WindowsFakeDirectory)" /&gt;
      &lt;!--  --&gt;
      &lt;!-- Replace the contents of a string --&gt;
      &lt;MSBuild.ExtensionPack.Framework.TextString TaskAction="Replace" OldString="%(FoundFolders.relativedir)%(FoundFolders.Filename)%(FoundFolders.Extension)" OldValue="$(WindowsForRealDirectory)" NewValue="$(WindowsFakeDirectory)"&gt;
        &lt;Output PropertyName="RewrittenFileName" TaskParameter="NewString" /&gt;
      &lt;/MSBuild.ExtensionPack.Framework.TextString&gt;
      &lt;!--  --&gt;
      &lt;CreateProperty Value="$(CheckedOutFileName)"&gt;
        &lt;Output PropertyName="CheckedOutFileNameTwo" TaskParameter="Value" /&gt;
      &lt;/CreateProperty&gt;
      &lt;!--  --&gt;
      &lt;Message Text="   " /&gt;  
      &lt;Message Text="The Result of the String.Replace: $(RewrittenFileName)" /&gt;
      &lt;!--  --&gt;
      &lt;Message Text="   " /&gt;      
      &lt;Message Text="Command: &amp;quot;The original and the rewritten file name:  &amp;quot; &amp;quot;%(FoundFolders.relativedir)%(FoundFolders.Filename)%(FoundFolders.Extension)&amp;quot;        &amp;quot;$(RewrittenFileName)&amp;quot;" /&gt;
      &lt;!--  --&gt;
    &lt;/Target&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;!--  --&gt;
    &lt;Target Name="AllTargetsWrapper"&gt;
      &lt;!--  --&gt;
      &lt;CallTarget Targets="DetermineAllFilesTarget" /&gt;
      &lt;!--  --&gt;
      &lt;CallTarget Targets="ReplaceAndShowAllFileNames" /&gt;
      &lt;!--  --&gt;
    &lt;/Target&gt;
    &lt;!--  --&gt;
  &lt;/Project&gt;
  </code></pre>