如何使用msbuild选择所有只读文件?

Sco*_*ham 5 msbuild

我正在尝试编写一个MsBuild脚本来压缩一些文件.我需要从文件夹中递归选择所有只读文件到ItemGroup以添加到zip.

我正在使用社区任务Zip任务,但我正在努力根据其属性选择文件.

是否有任何东西可以开箱即用,或者我是否需要编写自定义任务?

谢谢你的帮助.

Aar*_*son 5

您可以使用属性函数(添加到 msbuild 4)来确定文件是否是只读的,如下所示:

<ItemGroup>
  <MyFiles Include="Testing\*.*" >
    <ReadOnly Condition='1 == $([MSBuild]::BitwiseAnd(1, $([System.IO.File]::GetAttributes("%(Identity)"))))'>True</ReadOnly>
  </MyFiles>
</ItemGroup> 

<Target Name="Run" Outputs="%(MyFiles.Identity)">
  <Message Text="%(MyFiles.Identity)" Condition="%(MyFiles.ReadOnly) != True"/>
  <Message Text="%(MyFiles.Identity) ReadOnly" Condition="%(MyFiles.ReadOnly) == True" />
</Target>
Run Code Online (Sandbox Code Playgroud)


Sco*_*ham 0

这似乎是通过一些肮脏的命令行使用来完成这项工作的。

<Exec Command="dir .\RelPath\ToFolder\ToSearchIn /S /AR /B > readonlyfiles.temp.txt"/>
<ReadLinesFromFile File="readonlyfiles.temp.txt">
    <Output TaskParameter="Lines" ItemName="ReadOnlyFiles"/>
</ReadLinesFromFile>
<Delete Files="readonlyfiles.temp.txt"/>
Run Code Online (Sandbox Code Playgroud)

这给出了文件的绝对路径。

要获取相对路径,请尝试以下操作:

<Exec Command="dir .\RelPath\ToFolder\ToSearchIn /S /AR /B > readonlyfiles.temp.txt"/>
<FileUpdate Files="readonlyfiles.temp.txt"
            Multiline="True"
            Regex="^.*\\RelPath\\ToFolder\\ToSearchIn"
            ReplacementText="RelPath\ToFolder\ToSearchIn"
            />
<ReadLinesFromFile File="readonlyfiles.temp.txt">
    <Output TaskParameter="Lines" ItemName="ReadOnlyZipFiles"/>
</ReadLinesFromFile>
<Delete Files="readonlyfiles.temp.txt"/>
Run Code Online (Sandbox Code Playgroud)