Without editing the csproj file, run MSBuild to copy all the files marked as Content to a folder, keeping the folder structure?

Question

Ultimately, we would like to use the command line to copy all files that csproj marked as Content , while maintaining the directory structure and without editing the source csprog file.

We have seen How to make MSBuild copy all files marked as Content to a folder while maintaining the folder structure? This does not work for us because it includes editing the csproj . We want to avoid this.

Msbuild?

We thought to use msbuild through the command line, but we do not know how to ask it to do only the equivalent of this:

 <Target Name="CopyContentFiles"> <Copy SourceFiles="@(Content)" DestinationFiles="@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')"/> </Target> 

In other words, we would like to use the command line to determine the target (not just specify it) from the command line. For example. in pseudocode we want:

 msbuild MyApp.csproj /t:"Copy SourceFiles="@(Content)" DestinationFiles=..." 

XCopy?

We also thought about using xcopy , although we did not decide how to ask it to copy only files that have csproj tags as Content .

+2
cmd msbuild csproj
source share
1 answer

I am using PowerShell.

If you have a Get-VSProjectItems.ps1 script:

 param( [Parameter(Mandatory=$true)] $project ) $ErrorActionPreference = 'stop'; $project = (resolve-path $project).Path; $projectDir = Split-Path $project -Parent; $ns = @{ msb = 'http://schemas.microsoft.com/developer/msbuild/2003'; } Select-Xml -Path:$project -XPath:'//msb:Content' -Namespace:$ns | Select-Object -ExpandProperty:Node | % { New-Object psobject -Property:@{ RelPath = $_.Include; Directory = (Split-Path -Parent $_.Include); FullName = Join-Path $projectDir $_.Include; } } 

Then copying the content files becomes as simple as passing the output to a script block, which creates the structure of the target folder and copies the file (nb: the snippet below assumes that you want to recreate the directory tree in the current working directory):

 .\Get-VSProjectItems.ps1 ..\SomeProject\SomeProject.csproj | % { [void](mkdir $_.Directory -ErrorAction:SilentlyContinue); copy-item $_.fullname $_.relpath; } 
+1
source share

All Articles