What I want to do is copy a directory that exists in multiple parent directories, to a destination while preserving the \Parent\child structure. The source directories are versioned software releases, and the directory that I want contains hot-fixes for each of those versions.
Source directory structure
\Releases\app_26.6.1.123\Patch\
\Releases\app_26.6.2.456\Patch\
etc etc
The last two numbers change, but the format is always #.## or #.###, with the first # incrementing from 1 to 7.
And what I'm trying to do is something like this:
copy \Release\app_26.6.[1-7].*\Patch\ to C:\Temp\eureka\app_26.6.[1-7].*\Patch\
I think that I'll have to use a for loop iteration, but my brain is hurting trying to think of a way to do this in powershell; and that's why I'm here.
This post is pretty close to what I want to do, but wrong platform: Copying multiple directories into multiple destination directories
Thank you
CodePudding user response:
I'm assuming you have multiple destinations per source patch? Try something like this where you wildcard up the version number in the name:
Foreach ($src in (Get-Item "\Releases\app_*\Patch")) {
# Split off the last version number on the parent folder
$baseVersion = ($src.parent.name -split '\.')[0..2] -join '.'
# Get each destination folder using * wildcard
$destinations = Get-Item "c:/Temp/eureka/$baseVersion*/"
# Copy folders
Foreach ($dst in $destinations) {
Copy-Item $src.FullName $dst.FullName -Recurse
}
}
This should create the following source and destination structure:
# src
\Releases\app_26.6.1.123\Patch\1
\Releases\app_26.6.2.456\Patch\2
# dst
c:/Temp/eureka/app_26.6.1.100/Patch/1
c:/Temp/eureka/app_26.6.1.200/Patch/1
c:/Temp/eureka/app_26.6.2.300/Patch/2
c:/Temp/eureka/app_26.6.2.400/Patch/2
If you don't want a wildcard but specifically need [1-7]
, you can use regex:
$destinations = Get-Item "c:/Temp/eureka/*/" |
Where Name -match "$baseversion.[1-7]"