MSBuild - how to copy files that may or may not exist?

后端 未结 3 1222
鱼传尺愫
鱼传尺愫 2021-02-03 23:02

I have a situation where I need to copy a few specific files in a MSBuild script, but they may or may not exist. If they don\'t exist it\'s fine, I don\'t need them then. But th

相关标签:
3条回答
  • 2021-02-03 23:08

    Use the Exists condition on Copy task.

    <CreateItem Include="*.xml">
      <Output ItemName="ItemsThatNeedToBeCopied" TaskParameter="Include"/>
    </CreateItem>
    
    <Copy SourceFiles="@(ItemsThatNeedToBeCopied)"
          DestinationFolder="$(OutputDir)"
          Condition="Exists('%(RootDir)%(Directory)%(Filename)%(Extension)')"/>
    
    0 讨论(0)
  • 2021-02-03 23:12

    The easiest would be to use the ContinueOnError flag http://msdn.microsoft.com/en-us/library/7z253716.aspx

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
        <ItemGroup>
            <MySourceFiles Include="a.cs;b.cs;c.cs"/>
        </ItemGroup>
    
        <Target Name="CopyFiles">
            <Copy
                SourceFiles="@(MySourceFiles)"
                DestinationFolder="c:\MyProject\Destination"
                ContinueOnError="true"
            />
        </Target>
    
    </Project>
    

    But if something else is wrong you will not notice it. So the condition exist from madgnome's answer would be better.

    0 讨论(0)
  • 2021-02-03 23:31

    It looks like you can mark MySourceFiles as SkipUnchangedFiles="true" and it won't copy the files if they already exist.

    0 讨论(0)
提交回复
热议问题