12

In VS MSBuild we move group of files from one folder to another:

<ItemGroup>
      <RenameFile Include="Main\App.*" />     
</ItemGroup>    
<Move SourceFiles="@(RenameFile)" DestinationFiles="%(RootDir)%(RenameFile.Directory)NewApp%(RenameFile.Extension)" />

It works fine, except one file: App.exe.config, because it has double extension and it renamed to NewApp.config instead NewApp.exe.config (how it should be).

How to fix it?

Alexan
  • 8,165
  • 14
  • 74
  • 101

2 Answers2

14

Starting with MSBuild 4.0 we can use String Item Functions, for example Replace:

<ItemGroup>
  <RenameFile Include="Main\App.*" />    
</ItemGroup>    
<Message Text="Move: @(RenameFile) -> @(RenameFile -> Replace('App', 'NewApp'))" Importance="High" />
<Move SourceFiles="@(RenameFile)" DestinationFiles="@(RenameFile -> Replace('App', 'NewApp'))" />

which works fine.

Alexan
  • 8,165
  • 14
  • 74
  • 101
  • 1
    Wow, I almost forgot this simple method, when I see this problem, I am always accustomed to using RegexReplace. Because I use it often. Thanks for your sharing :). This answer should be accept as answer, you can accept it after time limit. – Leo Liu Mar 23 '18 at 02:15
2

Move and rename file with double extension using MSBuild

It seems you want to move and rename file App.* to NewApp.* but keep extension using MSBuild. You can use the MsBuild Community Tasks which have a RegexReplace task.

To accomplish this, add the MSBuildTasks nuget package to your project, then add following target to do that:

  <Target Name="MoveAndRename" AfterTargets="Build">
    <ItemGroup>
      <RenameFile Include="Main\App.*" />
    </ItemGroup>
    <Message Text="Files to copy and rename: @(RenameFile)" />
    <RegexReplace Input="@(RenameFile)" Expression="App." Replacement="NewApp.">
      <Output ItemName="DestinationFullPath" TaskParameter="Output" />
    </RegexReplace>
    <Message Text="Renamed Files: @(DestinationFullPath)" />
    <Move SourceFiles="@(RenameFile)" DestinationFiles="@(DestinationFullPath)" />
  </Target>

With this target, all App.* file are moved and renamed to NewApp.*:

enter image description here

Leo Liu
  • 71,098
  • 10
  • 114
  • 135