Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if any file in an item list exist using msbuild?

Tags:

msbuild

I would like to run a task if any file in an item list is missing. How do I do that?

My current script has a list of "source" files @(MyComFiles) that I translate another list of "destination" files @(MyInteropLibs), using the following task:

<CombinePath BasePath="$(MyPath)\interop" 
             Paths="@(MyComFiles->'%(filename).%(extension)')">
    <Output TaskParameter="CombinedPaths" 
            ItemName="MyInteropLibs" />
</CombinePath>

I want to check if any of the files in @(MyInteropLibs) is missing and run a task that will create them.

like image 435
Magnus Lindhe Avatar asked Apr 29 '09 12:04

Magnus Lindhe


2 Answers

If you only need to create the missing files, and not get a list of the files that were missing you can you the touch task, which will create if the files don't exist.

<Touch Files="@(MyInteropLibs)" AlwaysCreate="True" />

If you only want to create the missing files, and avoid changing timestamps of the existing files, then batching can help

<Touch Files="%(MyInteropLibs.FullPath)" AlwaysCreate="True" 
       Condition=" ! Exists(%(MyInteropLibs.FullPath)) "/>

If you want a list of the files created then

<Touch Files="%(MyInteropLibs.FullPath)" AlwaysCreate="True" 
       Condition=" ! Exists(%(MyInteropLibs.FullPath)) ">
    <Output TaskParameter="TouchedFiles" ItemName="CreatedFiles"/>
</Touch>
<Message Text="Created files = @(CreatedFiles)"/>
like image 145
Scott Weinstein Avatar answered Sep 28 '22 08:09

Scott Weinstein


I am not very experienced with MSBuild so there may be better solutions than this but you could write a FilesExist task that takes the file list and passes each file to File.Exists returning true if they do exist and false otherwise and thenn react based on the result

Sorry I can't provide code to help out, my knowlege of MSBuild sytax is not strong

like image 43
Crippledsmurf Avatar answered Sep 28 '22 08:09

Crippledsmurf