I am trying to dump the .yaml files inside of 4 folders (files are nested in a subfolder inside) to another directory, only copying the .yaml files into each of the 4 copied folders, excluding the subfolders. The subfolder in each directory has the same name. Ive got a script that does the first part of the copy, but it doesnt exclude the subfolders, and nests the files in their respective target folder still. Not sure what im doing wrong or what I need to fix with my script at the bottom.
The current file structure:
- QA
- Target **Connections** Folder
- contains .yaml files
- Several Other Folders
- RESEARCH
- Target **Connections** Folder
- contains .yaml files
- Several Other Folders
- PROD
- Target **Connections** Folder
- Several Other Folders
- DEV
- Target **Connections** Folder
- Several Other Folders
The output to the copied directory should be as follows:
- QA
- .yaml files from original "qa/Connections" subfolder
- RESEARCH
- .yaml files from original "research/Connections" subfolder
- PROD
- .yaml files from original "prod/Connections" subfolder
- DEV
- .yaml files from original "dev/Connections" subfolder```
I have the following script, but it only copies the 4 sub folders under the base directory, and all of the contents contained, I cant figure out how to drill those down to exclude everything except for the Connections folder, and place only the files into those 4 main environment folders.
$sourceDir = "C:\Users\hhh\appdev\powershell-scripts\TEST-REPO\hhh\data\environments"
$targetDir = "C:\Users\hhh\appdev\targetfolder"
Get-ChildItem $sourceDir -File -Recurse | ForEach-Object {
$dest = Join-Path -Path $targetDir -ChildPath $_.DirectoryName.SubString($sourceDir.Length)
if ($dest -match 'research|qa|production|global') {
$null = New-Item -Path $dest -ItemType Directory -Force
$_ | Copy-Item -Destination $dest -Force
}
}
CodePudding user response:
The Get-ChildItem
-Path
allows for wildcards. This can be combined with -Filter to only return *.yml
files.
$sourceDir = "C:\Users\hhh\appdev\powershell-scripts\TEST-REPO\hhh\data\environments"
$targetDir = "C:\Users\hhh\appdev\targetfolder"
Get-ChildItem -Path "$sourceDir\*\Connections" -Filter '*.yml' -Recurse | ForEach-Object {
# this assumes there are no subdirectories in the 'Connections' directory
# that contain any .yml files
$dest = Join-Path -Path $targetDir -ChildPath $_.Directory.Parent.Name
if (!(Test-Path -Path $dest -PathType Container)) {
New-Item -Path $dest -ItemType Directory -Force | Out-Null
}
$_ | Copy-Item -Destination $dest -Force
}
CodePudding user response:
I think this will do the trick:
$sourceDir = 'C:\Users\hhh\appdev\powershell-scripts\TEST-REPO\hhh\data\environments'
$targetDir = 'C:\Users\hhh\appdev\targetfolder'
# Create target folders
Get-ChildItem $SourceDir -Directory -Name | %{
mkdir (Join-Path $TargetDir $_) | Out-Null
}
# Copy .yaml files
Get-ChildItem $SourceDir -Filter *.yaml -Recurse -Depth 2 |
where {$_.Directory.Name -eq 'Connections'} |
Copy-Item -Destination { '{0}\{1}\{2}' -f $TargetDir , $_.Directory.Parent.Name , $_.Name }