I have a directory structure that looks like this:
C:\folderA\folderB\folderC\client1\f1\files C:\folderA\folderB\folderC\client1\f2\files C:\folderA\folderB\folderC\client2\f1\files C:\folderA\folderB\folderC\client2\f2\files C:\folderA\folderB\folderC\client3\f1\files C:\folderA\folderB\folderC\client4\f2\files
I want to copy the content of the f1
folders in C:\tmp\ to get this
C:\tmp\client1\f1\files C:\tmp\client2\f1\files C:\tmp\client3\f1\files
I tried this:
Copy-Item -recur -path: "*/f1/" -destination: C:\tmp\
But it copies the contents without copying the structure correctly.
The Copy-Item cmdlet has the Container parameter set to $false . This causes the contents of the source folder to be copied but doesn't preserve the folder structure. Notice that files with the same name are overwritten in the destination folder.
Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters.
In PowerShell version 3.0 and newer this is simply done this way:
Get-ChildItem -Path $sourceDir | Copy-Item -Destination $targetDir -Recurse -Container
Reference: Get-ChildItem
PowerShell:
$sourceDir = 'c:\folderA\folderB\folderC\client1\f1' $targetDir = ' c:\tmp\' Get-ChildItem $sourceDir -filter "*" -recurse | ` foreach{ $targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length); New-Item -ItemType File -Path $targetFile -Force; Copy-Item $_.FullName -destination $targetFile }
Note:
-Force
to actually create the folder structure.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