How to select all read-only files using msbuild?

I am trying to write an MsBuild script to archive some files. I need to select all read-only files recursively from a folder in ItemGroup to add to zip.

I use the Zip task for community tasks, but struggle with the selection of files based on their attributes.

Is there anything around to do this out of the box, or do I need to write a custom task?

Thanks for the help.

+4
source share
3 answers

This seems to be done using a dirty command line.

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

This gives absolute file paths.

To get relative paths, try something like this:

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

You can use Property Properties (added in msbuild 4) to find out if a file is read-only:

 <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> 
+3
source

Have you considered building a community site ?

He has a zip task and an attribute change task - they should get most of them from you.

0
source

Source: https://habr.com/ru/post/1303926/


All Articles