Question:
I would like to copy all files of a certain type from a certain sub-directory with their relative path from that sub-directory to another directory with the relative path intact. e.g.:
Source sub-dir:
1 2 |
c:\temp\sourcedirectory |
Source files:
1 2 3 |
c:\temp\sourcedirectory\tonymontana\fileOne.txt c:\temp\sourcedirectory\poker\fileTwo.txt |
Target dir:
1 2 |
c:\temp\targetdirectory |
Desired result:
1 2 3 |
c:\temp\targetdirectory\tonymontana\fileOne.txt c:\temp\targetdirectory\poker\fileTwo.txt |
So far I’ve come up with:
1 2 3 4 5 |
Set-Location $srcRoot Get-ChildItem -Path $srcRoot -Filter $filePattern -Recurse | Resolve-Path -Relative | Copy-Item -Destination {Join-Path $buildroot $_.FullName} |
However, this “everything is an object” à la PowerShell is beating me down (at least that’s what I suspect). I.e. the files gets copied, but without their relative path.
Anyone who could enlighten me a bit?
Answer:
Don’t bother with PowerShell cmdlets for this, simply use robocopy
:
1 2 |
robocopy C:\temp\sourcedirectory C:\temp\targetdirectory *.txt /s |