Exclude list in PowerShell Copy-Item does not appear to be working

前端 未结 10 926
旧时难觅i
旧时难觅i 2020-12-02 11:40

I have the following snippet of PowerShell script:

$source = \'d:\\t1\\*\'
$dest = \'d:\\t2\'
$exclude = @(\'*.pdb\',\'*.config\')
Copy-Item $source $dest -R         


        
10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 12:28

    As comments format code badly I'll post as answer but it's just an addition to @landyman's answer. The proposed script has a drawback - it will create double-nested folders. For example for 'd:\t1\sub1' it will create empty directory 'd:\t2\sub1\sub1'. That's due to the fact that Copy-Item for directories expects parent directory name in -Destination property not directory name itself. Here's a workaround I found:

    Get-ChildItem -Path $from -Recurse -Exclude $exclude | Copy-Item -Force -Destination {
      if ($_.GetType() -eq [System.IO.FileInfo]) {
        Join-Path $to $_.FullName.Substring($from.length)
      } else {
        Join-Path $to $_.Parent.FullName.Substring($from.length)
      }
    }
    

提交回复
热议问题