Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file with relative path

Tags:

powershell

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:

c:\temp\sourcedirectory

Source files:

c:\temp\sourcedirectory\tonymontana\fileOne.txt
c:\temp\sourcedirectory\poker\fileTwo.txt

Target dir:

c:\temp\targetdirectory

Desired result:

c:\temp\targetdirectory\tonymontana\fileOne.txt
c:\temp\targetdirectory\poker\fileTwo.txt

So far I've come up with:

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?

like image 831
PistolPete Avatar asked Oct 30 '25 08:10

PistolPete


1 Answers

Don't bother with PowerShell cmdlets for this, simply use robocopy:

robocopy C:\temp\sourcedirectory C:\temp\targetdirectory *.txt /s
like image 192
Ansgar Wiechers Avatar answered Nov 02 '25 04:11

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!