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?
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.
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.*
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With