使用相对路径复制文件

Pis*_*ete 3 powershell

我想从某个子目录复制某个类型的所有文件,其中包含从该子目录到相对路径完整的另一个目录的相对路径.例如:

来源子目录:

c:\temp\sourcedirectory
Run Code Online (Sandbox Code Playgroud)

源文件:

c:\temp\sourcedirectory\tonymontana\fileOne.txt
c:\temp\sourcedirectory\poker\fileTwo.txt
Run Code Online (Sandbox Code Playgroud)

目标目录:

c:\temp\targetdirectory
Run Code Online (Sandbox Code Playgroud)

期望的结果:

c:\temp\targetdirectory\tonymontana\fileOne.txt
c:\temp\targetdirectory\poker\fileTwo.txt
Run Code Online (Sandbox Code Playgroud)

到目前为止,我想出了:

Set-Location $srcRoot
Get-ChildItem -Path $srcRoot -Filter $filePattern -Recurse |
    Resolve-Path -Relative |
    Copy-Item -Destination {Join-Path $buildroot $_.FullName}
Run Code Online (Sandbox Code Playgroud)

然而,PowerShell的这一切"一切都是一个对象"正在击败我(至少我怀疑这是).即文件被复制,但没有相对路径.

谁可以开导我一点?

Ans*_*ers 7

不要为此花费PowerShell cmdlet,只需使用robocopy:

robocopy C:\temp\sourcedirectory C:\temp\targetdirectory *.txt /s
Run Code Online (Sandbox Code Playgroud)