tags:

views:

153

answers:

2

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.

A: 

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

Crippledsmurf
I would rather do it in msbuild. It is possible to use a function Exists() in the Conditional attribute of a task, but it only works on one file.
Manga Lee
It doesn't appear that MSBuild has the functionality built in. Neither do I see such functionality in the MSBuild Extension Pack or the MSBuild Community Tasks. As Crippledsmurf suggested, you can create your own custom MSBuild task that will do what you need. You then reference the dll in your build file and call the task as necessary.
Pedro
A: 

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)"/>
Scott Weinstein