Q:

CopyToPublishDirectory in .csproj

I'm using CopyToPublishDirectory in my .csproj to copy over files/folders when publishing my dotnet app:
<None Update="Views\**\*; wwwroot\**\*">
  <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>

Is there a way to copy over a folder and change its name in the process?  For example, I'd like to copy over a subset of my node_modules folder, so I could create a new folder called node_modules_dev with my subset of npm dependencies, and copy it over via CopyToPublishDirectory as node_modules.  I'd imagine the syntax would work something like this:
<None Update="node_modules_dev/**/*" Rename="node_modules">
  <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>

Thanks--

A:

The trick here is to:

Ensure that the items are not yet included by default so there is no leftover metadata from previous glob pattern expansions. This can be done by adding the path to the DefaultItemExcludes property so the web sdk will ignore the files.
Use the %(RecursiveDir) metadata that is available for items expanded via glob patterns and represents the value of any expanded path. This will be defined for the Include="…" syntax only hence 1.

This will overwrite the default target path to a new directory using the Link metadata:
<PropertyGroup>
  <DefaultItemExcludes>$(DefaultItemExcludes);node_modules_dev\**</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
  <Content Include="node_modules_dev\**\*" Link="node_nodules\%(RecursiveDir)%(FileName)%(Extension)" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

