代码之家  ›  专栏  ›  技术社区  ›  Scott Langham

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

  •  5
  • Scott Langham  · 技术社区  · 15 年前

    我正在尝试编写一个msbuild脚本来压缩一些文件。我需要递归地将所有只读文件从一个文件夹中选择到一个项组中,以添加到zip中。

    我正在使用社区任务zip任务,但在根据文件的属性选择文件时遇到了困难。

    是否有现成的解决方案,或者是否需要编写自定义任务?

    谢谢你的帮助。

    3 回复  |  直到 13 年前
        1
  •  4
  •   Aaron Carlson    13 年前

    你可以使用 Property Functions (添加到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>
    
        2
  •  0
  •   Oded    15 年前

    你看过社区建设任务吗 site ?

    它有一个zip任务和一个属性更改任务——它们应该能让你完成大部分任务。

        3
  •  0
  •   Scott Langham    15 年前

    这似乎是在命令行使用有点脏的情况下完成的。

    <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"/>
    

    这提供了文件的绝对路径。

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

    <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"/>