powershell - 使用相对路径复制文件

标签 powershell

我想将某个类型的所有文件从某个子目录及其相对路径从该子目录复制到另一个目录,并且相对路径完好无损。例如。:

源子目录:

c:\temp\sourcedirectory

源文件:
c:\temp\sourcedirectory\tonymontana\fileOne.txt
c:\temp\sourcedirectory\poker\fileTwo.txt

目标目录:
c:\temp\targetdirectory

想要的结果:
c:\temp\targetdirectory\tonymontana\fileOne.txt
c:\temp\targetdirectory\poker\fileTwo.txt

到目前为止,我想出了:
Set-Location $srcRoot
Get-ChildItem -Path $srcRoot -Filter $filePattern -Recurse |
    Resolve-Path -Relative |
    Copy-Item -Destination {Join-Path $buildroot $_.FullName}

但是,PowerShell 中的“一切都是对象”正在击败我(至少我怀疑是这样)。 IE。文件被复制,但没有它们的相对路径。

谁能给我一点启发?

最佳答案

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

robocopy C:\temp\sourcedirectory C:\temp\targetdirectory *.txt /s

关于powershell - 使用相对路径复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616002/

相关文章:

powershell - 如何关闭控制台中的语法高亮?

powershell - Powershell允许取消启动过程-wait

电源外壳 : using get-content as body of an smtp mail

powershell - Powershell 中的正斜杠

Azure 编辑服务主体所需的声明

powershell - 在 PowerShell 中将新行写入文本文件

azure - 将 PowerShell 对象数据记录到 Azure blob,因为 CSV 缺少换行符

powershell - Invoke-WebRequest 在 Azure 自动化中不起作用

collections - 在 PowerShell 中展平数组

powershell - 对空值表达式的错误处理方法调用