Powershell Copy Files with a Blacklist (Exclude) and a Whitelist (Include)

风格不统一 提交于 2019-12-03 16:40:54

Copy-Item -Recurse, as of Windows PowerShell v5.1 / PowerShell Core 6.2.0, has its quirks and limitations; here's what I found:

If you have additional information or corrections, please let us know.

There are two fundamental ways to call Copy-Item -Recurse:

  • (a) specifying a directory path as the source - c:\windows\system32

  • (b) using a wildcard expression as the source that resolves to multiple items in the source directory - c:\windows\system32\*

There are two fundamental problems:

  • The copying behavior varies based on whether the target directory already exists - see below.

  • The -Include parameter does not work properly and neither does -Exclude, though problems are much more likely to arise with -Include; see this GitHub issue.

DO NOT USE THE SOLUTIONS BELOW IF YOU NEED TO USE -Include - if you do need -Include, use LotPing's helpful solution.


Case (a) - a single directory path as the source

If the source is a single directory (or is the only directory among the items that a wildcard pattern resolved to), Copy-Item implicitly also interprets the destination as a directory.

However, if the destination directory already exists, the copied items will be placed in a subdirectory named for the source directory, which in your case means: C:\work9\MsBuildExamples\FileCopyRecursive\PowershellResults\System32

There is an issue in the PowerShell GitHub repository that - rightfully - complains about this counter-intuitive behavior.

There are two basic workarounds:

If acceptable, remove the destination directory first, if it exists - which is obviously to be done with CAUTION (remove -WhatIf once you're confident that the command works as intended):

# Remove a pre-existing destination directory:
if (Test-Path $destinationDirectory) {
  Remove-Item $destinationDirectory -Recurse -WhatIf
}

# Now Copy-Item -Recurse works as intended.
# As stated, -Exclude works as intended, but -Include does NOT.
Copy-Item $sourceDirectory $destinationDirectory -Recurse

Caveat: Remove-Item -Recurse, regrettably, can intermittently act asynchronously and can even fail - for a robust alternative, see this answer.

If you want to retain a preexisting destination dir. - e.g., if you want to add to contents of the destination directory,

  • Create the target dir. on demand; that is, create it only if it doesn't already exist.
  • Use Copy-Item to copy the contents of the source directory to the target dir.
# Ensure that the target dir. exists.
# -Force is a quiet no-op, if it already exists.
$null = New-Item -Force -ItemType Directory -LiteralPath $destinationDirectory

# Copy the *contents* of the source directory to the target, using
# a wildcard.
# -Force ensures that *hidden items*, if any, are included too.
# As stated, -Exclude works as intended, but -Include does NOT.
Copy-Item -Force $sourceDirectory/* $destinationDirectory -Recurse

Case (b) - a wildcard expression as the source

Note:

  • If there's exactly 1 directory among the resolved items, the same rules as in case (a) apply.

  • Otherwise, the behavior is only problematic if the target item doesn't exist yet. - see below.

  • Therefore, the workaround is to ensure beforehand that the destination directory exists:
    New-Item -Force -Path $destinationDirectory -ItemType Directory

If the target item (-Destination argument) doesn't exist yet:

  • If there are multiple directories among the resolved items, Copy-Item copies the first directory, and then fails on the second with the following error message:
    Container cannot be copied onto existing leaf item

  • If the source is a single file or resolves to files only, Copy-Item implicitly interprets a non-existent destination as a file.
    With multiple files, this means that a single destination file is created, whose content is the content of the file that happened to be copied last - i.e, there is data loss.

granadaCoder

Using a suggestion I found from here (the "touch" approach)

Should Copy-Item create the destination directory structure?

I got the below to work.

I'll leave this question as "Unanswered" for a few days, maybe somebody has something better. The New-Item seems weird to me, since the Copy-Item should not fluke-out in an Include and Exclude list IMHO.

(use variable setters as per the original question)

IF (Test-Path -Path $destinationDirectory -PathType Container) 
{ 
    Write-Host "$destinationDirectory already exists" -ForegroundColor Red
} 
ELSE 
{ 
    New-Item -Path $destinationDirectory -ItemType directory 
} 


Get-ChildItem $sourceDirectory -Include $includeFiles -Exclude $excludeFiles -Recurse -Force |
    New-Item -ItemType File -Path  {
        Join-Path $destinationDirectory $_.FullName.Substring($sourceDirectory.Length)
    }  -Force

Get-ChildItem $sourceDirectory -Include $includeFiles -Exclude $excludeFiles -Recurse -Force |
    Copy-Item -Destination {
        Join-Path $destinationDirectory $_.FullName.Substring($sourceDirectory.Length)
    }

To keep an overview I shortened your vars (and changed dst to fit my env)

$src = 'c:\windows\System32'
$dst = 'Q:\Test\2017-03\15\PowershellResults'
$exc = @('EventViewer_EventDetails.xsl')
$inc = @('*.xsl','*.xslt','*.png','*.jpg')

Get-ChildItem -Path $src -Rec -Inc $inc -Exc $exc -ea silentlycontinue|ForEach{
  $DestPath = $_.DirectoryName -replace [regex]::escape($src),$dst
  If (!(Test-Path $DestPath)){MkDir $DestPath}
  $_|Copy-Item -Destination $DestPath -Force
}

Edit Essentially the same as your example, maybe a bit denser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!