Search code examples
filemsbuildvisual-studio-2017file-renamemovefile

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?


Solution

  • 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.