Juzzbott
Juzzbott

Reputation: 1769

MSBuild cannot recursively copy files with Copy task

I'm aware that this type question has been asked before (Recursively copy all files with MSBuild and MSBuild recursive copy) however I can't seem to get it to work for my project file.

I'm using Visual Studio 2015, so I don't know if anything has changes from all the other information out there. I've also tried following the second example on this page, all with the same outcome: https://msdn.microsoft.com/en-us/library/3e54c37h.aspx

I'm trying to recursively copy files from my source directory to my inetpub directory. I've got the following setup inside my "BuildLocal" target.

<ItemGroup>
  <CopyFiles Include="**\*.cshtml" />
</ItemGroup>
<Copy SourceFiles="@(CopyFiles)" DestinationFiles="@(CopyFiles->'$(DeploymentFolder)\%(RecursiveDir)%(Filename)%(Extension)')" ContinueOnError="true" SkipUnchangedFiles="true" />

The DeploymentFolder variable is setup in my PropertyGroup like so: <DeploymentFolder>C:\inetpub\websites\TestSite</DeploymentFolder>

The error message I'm getting is:

warning MSB3021: Unable to copy file "**\*.cshtml" to "C:\inetpub\websites\TestSite\**\*.cshtml". Illegal characters in path.

Changing the CopyFile Include to be include="$(ProjectDir)**\*.cshtml" has no effect either...

Any help on this issue would awesome :)

Upvotes: 3

Views: 1472

Answers (1)

Rainer Sigwald
Rainer Sigwald

Reputation: 835

I suspect that there was a file under the project directory with a full path longer than 260 characters (MAX_PATH). In that case, you would hit an MSBuild bug that causes the wildcard to be included as a literal string rather than as the list of files. This is often seen with node_modules folders.

As of Visual Studio 2017 (technically with its Preview 5 prerelease), adding an Exclude attribute for the folder that contains long paths will get the rest of the files instead of the literal **\*.cshtml.

To work around this, you can use an Exec task to invoke robocopy or similar.

Upvotes: 2

Related Questions