Mitchell Grimes
Mitchell Grimes

Reputation: 3

Make temporary backup files in the same folder structure

Below is a full path to some files that have a specific file pattern I'm looking for. The files are not all located in the same location, however, I would like to change the ".gen" to ".tmp" for a short period of time while still maintaining ".gen" in its entirety.

D:\Example\CommonData\Settings\ConnectionSettings.config.gen 
D:\Example\CommonData\Settings\FileExtensions.config.gen
D:\Example\WebApps\Website\Web.config.gen
D:\Example\Test\SCM\Config.Files\Some.Other.thing.config.gen 

What I would like to do:

D:\Example\CommonData\Settings\ConnectionSettings.config.gen
D:\Example\CommonData\Settings\ConnectionSettings.config.tmp 
D:\Example\CommonData\Settings\FileExtensions.config.gen
D:\Example\CommonData\Settings\FileExtensions.config.tmp
D:\Example\WebApps\Website\Web.config.gen
D:\Example\WebApps\Website\Web.config.tmp
D:\Example\Test\SCM\Config.Files\Some.Other.thing.config.gen
D:\Example\Test\SCM\Config.Files\Some.Other.thing.config.tmp  

I've tried so far:

Get-ChildItem *.config.gen | ForEach {
  $NewName = $_.Name -replace ".config.gen$", ".config.tmp"
  Copy-Item $_ $NewName
}

Is what I'm trying to do more easier to do with xcopy or robocopy versus Copy-Item? If so, how would I go about using something other than Copy-Item.

Upvotes: 0

Views: 110

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

Since you want the destination items renamed Get-ChildItem/Copy-Item is the better approach here. Something like this should work:

Get-ChildItem *.config.gen -Recurse |
  Copy-Item -Destination { Join-Path $_.Directory ($_.BaseName + '.tmp') }

Upvotes: 1

Related Questions