views:

5693

answers:

6

Hi all,

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 another folder using msbuild.

{ProjectName}
      |----->Source
      |----->Tools
              |----->Viewer
                       |-----{about 5 sub dirs}

What I need to be able to do is copy all the files and sub folders from the tools folder into the debug folder for the application. This is the code that I have so far.

 <ItemGroup>
<Viewer Include="..\$(ApplicationDirectory)\Tools\viewer\**\*.*" />
 </ItemGroup>

<Target Name="BeforeBuild">
     <Copy SourceFiles="@(Viewer)" DestinationFolder="@(Viewer->'$(OutputPath)\\Tools')" />
  </Target>

The build script runs but doesn't copy any of the files or folders.

Thanks

A: 

Did you try to specify concrete destination directory instead of "DestinationFolder="@(Viewer->'$(OutputPath)\Tools')"" ? I'm not very proficient with advanced MSBuild syntax, but "@(Viewer->'$(OutputPath)\Tools')" looks weird to me. Script looks good, so the problem might be in values of $(ApplicationDirectory) and $(OutputPath)

EDIT:

Here is a blog post that might be useful:

How To: Recursively Copy Files Using the Task

aku
A: 

I've never seen the @ sign used to reference a variable. I thought it was always the $.

ranomore
@ is used to signify that you want to work with a list/collection of items.
muloh
+8  A: 

I think the problem might be in how you're creating your ItemGroup and calling the Copy task. See if this makes sense:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
    <PropertyGroup>
     <YourDestinationDirectory>..\SomeDestinationDirectory</YourDestinationDirectory>
     <YourSourceDirectory>..\SomeSourceDirectory</YourSourceDirectory>
    </PropertyGroup>

    <Target Name="BeforeBuild">
     <CreateItem Include="$(YourSourceDirectory)\**\*.*">
      <Output TaskParameter="Include" ItemName="YourFilesToCopy" />
     </CreateItem>

     <Copy SourceFiles="@(YourFilesToCopy)"
       DestinationFiles="@(YourFilesToCopy->'$(YourDestinationDirectory)\%(RecursiveDir)%(Filename)%(Extension)')" />
    </Target>
</Project>
muloh
+1  A: 

Thanks all,

Turns out that I missed the %(RecursiveDir) cal, that will teach me for not reading information properly.

My code now looks like this.

<ItemGroup>
    <Viewer Include="$(MSBuildProjectDirectory)\Tools\**\*.*" Exclude="$(MSBuildProjectDirectory)\Tools\.svn" />
    <Database Include="..\MapsAndPlans_Working.mdb" />
  </ItemGroup>
  <Target Name="BeforeBuild">
  </Target>

  <Target Name="AfterBuild">
  <MakeDir Directories = "$(OutputPath)\Documents\Thumbnails" Condition="!Exists('$(OutputPath)\Documents\Thumbnails')" />
     <Copy SourceFiles="@(Database)" DestinationFolder="$(OutputPath)" Condition="!Exists('$(OutputPath)\MapsAndPlans_Working.mdb')" />
     <PropertyGroup Condition="!Exists('$(OutputPath)\\Tools')">
      <Copy SourceFiles="@(Viewer)" DestinationFolder="$(OutputPath)\\Tools\\%(RecursiveDir)" />
     </PropertyGroup>
  </Target>

It worked the first time but now I'm getting an error that says:

Error 1 The attribute "SourceFiles" in element is unrecognized"

It seems to be on this line of xml:

<Copy SourceFiles="@(Viewer)" DestinationFolder="$(OutputPath)\\Tools\\%(RecursiveDir)" />

Can anyone see what would cause this?

Thanks

Nathan W
Just a guess, but have you tried eliminated the double backslashes ('\\') from your DestinationFolder?
muloh
+6  A: 

I'm kinda new to MSBuild but I find the EXEC Task handy for situation like these. I came across the same challenge in my project and this worked for me and was much simpler. Someone please let me know if it's not a good practice.

<Target Name="CopyToDeployFolder" DependsOnTargets="CompileWebSite">
    <Exec Command="xcopy.exe  $(OutputDirectory) $(DeploymentDirectory) /e" WorkingDirectory="C:\Windows\" />
</Target>
zXen
+1  A: 

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

http://sdctasks.codeplex.com/

Johan