Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move and rename file with double extension using MSBuild

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?

like image 219
Alexan Avatar asked Feb 04 '23 01:02

Alexan


2 Answers

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.

like image 113
Alexan Avatar answered Mar 07 '23 11:03

Alexan


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

like image 36
Leo Liu-MSFT Avatar answered Mar 07 '23 10:03

Leo Liu-MSFT