I have the source folder structure as shown below
c:\TestResults
|-- Log
| |-- xyz.pdf
| `-- Reports
| `-- rp.pdf
|-- Keywords
| |-- key.txt
| | `-- pb.ea
| `-- reports
|-- Test
| |-- 11.pdf
| |-- 12
| `-- Log
| |-- h1.pdf
| `-- Reports
| `-- h2.pdf
`-- Dev
|-- st
|-- ea
`-- Log
`-- Reports
`-- h4.pdf
I need to copy all the "Log" folders while maintaining the folder structure. The destination path is "c:\Work\Logs\TestResults". The resultant structure should be as shown below.
c:\Work\Logs\TestResults
|-- Log
| |-- xyz.pdf
| `-- Reports
| `-- rp.pdf
|-- Test
| `-- Log
| |-- h1.pdf
| `-- Reports
| `-- h2.pdf
`-- Dev
`-- Log
`-- Reports
`-- h4.pdf
Is there an easy way of achieving this using a powershell script? Thanks!
Edit: Here's the code that I have written so far. It flattens the folder structure but doesn't maintain the hierarchy. I am new to powershell scripting. Please help.
$baseDir = "c:\TestResults"
$outputDir = "c:\Work\Logs"
$outputLogsDir = $outputDir + "\TestResults"
$nameToFind = "Log"
$paths = Get-ChildItem $baseDir -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.EndsWith($nameToFind)}
if(!(test-path $outputLogsDir))
{
New-Item -ItemType Directory -Force -Path $outputLogsDir
}
foreach($path in $paths)
{
$sourcePath = $path.FullName + "\*"
Get-ChildItem -Path $sourcePath | Copy-Item -Destination $outputLogsDir -Recurse -Container
}