Copy all files and folders using msbuild

后端 未结 10 1811
感动是毒
感动是毒 2020-11-30 22:22

Just wondering if someone could help me with some msbuild scripts that I am trying to write. What I would like to do is copy all the files and sub folders from a folder to

相关标签:
10条回答
  • 2020-11-30 22:52

    This is the example that worked:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
       <ItemGroup>
          <MySourceFiles Include="c:\MySourceTree\**\*.*"/>
       </ItemGroup>
    
       <Target Name="CopyFiles">
          <Copy
            SourceFiles="@(MySourceFiles)"
            DestinationFiles="@(MySourceFiles->'c:\MyDestinationTree\%(RecursiveDir)%(Filename)%(Extension)')"
           />
        </Target>
    
    </Project>
    

    source: https://msdn.microsoft.com/en-us/library/3e54c37h.aspx

    0 讨论(0)
  • 2020-11-30 22:54

    The best way to recursively copy files from one directory to another using MSBuild is using Copy task with SourceFiles and DestinationFiles as parameters. For example - To copy all files from build directory to back up directory will be

    <PropertyGroup>
    <BuildDirectory Condition="'$(BuildDirectory)' == ''">Build</BuildDirectory>
    <BackupDirectory Condition="'$(BackupDiretory)' == ''">Backup</BackupDirectory>
    </PropertyGroup>
    
    <ItemGroup>
    <AllFiles Include="$(MSBuildProjectDirectory)/$(BuildDirectory)/**/*.*" />
    </ItemGroup>
    
    <Target Name="Backup">
    <Exec Command="if not exist $(BackupDirectory) md $(BackupDirectory)" />
    <Copy SourceFiles="@(AllFiles)" DestinationFiles="@(AllFiles-> 
    '$(MSBuildProjectDirectory)/$(BackupDirectory)/%(RecursiveDir)/%(Filename)% 
    (Extension)')" />
    </Target>
    

    Now in above Copy command all source directories are traversed and files are copied to destination directory.

    0 讨论(0)
  • 2020-11-30 22:55

    Personally I have made use of CopyFolder which is part of the SDC Tasks Library.

    http://sdctasks.codeplex.com/

    0 讨论(0)
  • 2020-11-30 23:01

    This is copy task i used in my own project, it was working perfectly for me that copies folder with sub folders to destination successfully:

    <ItemGroup >
    <MyProjectSource Include="$(OutputRoot)/MySource/**/*.*" />
    </ItemGroup>
    
    <Target Name="AfterCopy" AfterTargets="WebPublish">
    <Copy SourceFiles="@(MyProjectSource)" 
     OverwriteReadOnlyFiles="true" DestinationFolder="$(PublishFolder)api/% (RecursiveDir)"/>
    

    In my case i copied a project's publish folder to another destination folder, i think it is similiar with your case.

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