我试图在文件列表上“循环”(批处理),并使用不同的目录名替换string.replace根目录名。
这个项目使用3.5,因此无法充分利用一些新的4.0功能。
我正在使用 MSBuild.ExtensionPack.Framework.TextString 任务(来自 http://msbuildextensionpack.codeplex.com/releases/57599/download/229487 )
基本上,我想重写一个文件名
c:\SomeFolder\myfile.txt
to
c:\AnotherFolder\myfile.txt
我写了一个模拟来证明这个问题。
以下是我得到的结果:
"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"
您会注意到批次中的每个项目都会重复“c:\ WindowsFake \ write.exe”(右侧)。 :其中
我想要得到的东西:
"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"
我(有点)理解为什么我得到重复值。 但(当然)无法弄清楚如何解决这个问题。 不要挂在“windows”“fakewindows”上,这些只是我创建的模拟示例,因为大多数人在他们的计算机上都有一个c:\ windows目录,我需要一些“抓住”一些文件。
同样,我使用的是3.5 MSBuild,而不是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=
这是完整的.msbuild代码。 (我在一个名为'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>
答案 0 :(得分:1)
您是否有任何理由不仅仅进行简单的项目转换?它的代码要少得多。
<ItemGroup>
<MyIncludedFiles
Include="$(WindowsForRealDirectory)\**\Microsoft.NET\*.exe"
Exclude="@(MyExcludedFiles)"
/>
<MyIncludedFiles
Include="$(WindowsForRealDirectory)\*.exe"
Exclude="@(MyExcludedFiles)"
/>
</ItemGroup>
...then
<ItemGroup>
<MyTransformedFiles
Include="@(MyIncludedFiles->'$(WindowsFakeDirectory)\%(RecursiveDir)%(Filename)%(Extension)')
/>
</ItemGroup>
请注意Microsoft.NET之前的额外**,以便RecursiveDir具有值。