PoshCode Archive  Artifact [3dabeed4ee]

Artifact 3dabeed4ee4adef5a7b696cb34e6ec1f0e0eeb083c341763071e30f6635c1d11:

  • File Copy-Item-extended.ps1 — part of check-in [d6d2ebe7d5] at 2018-06-10 13:26:15 on branch trunk — My replacement of Copy-Item addresses 3 problems. 1) if $dest doesn’t exist, it creates a directory 2) $exclude works on all levels 3) It handles wildcards in $src (in the filepart, this extends the solution to 2) in http://stackoverflow.com/questions/731752/exclude-list-in-powershell-copy-item-does-not-appear-to-be-working. Wildcards in the directory part of $dest are supported, but I do not recommend this use. This is a workaround for my personal needs, it does not try to use include. Note this solves the defect of Get-Childitem which doesn’t return the relative path and the object simultaneously in case of wildcards in $path. (user: Bernd Kriszio size: 2543)

# encoding: ascii
# api: powershell
# title: Copy-Item extended
# description: My replacement of Copy-Item addresses 3 problems. 1) if $dest doesn’t exist, it creates a directory 2) $exclude works on all levels  3) It handles wildcards in $src (in the filepart, this extends the solution to 2) in http://stackoverflow.com/questions/731752/exclude-list-in-powershell-copy-item-does-not-appear-to-be-working. Wildcards in the directory part of $dest are supported, but I do not recommend this use. This is a workaround for my personal needs, it does not try to use include. Note this solves the defect of Get-Childitem which doesn’t return the relative path and the object simultaneously in case of wildcards in $path.
# version: 0.1
# type: function
# author: Bernd Kriszio
# license: CC0
# function: Copy-ToCreateFolder
# x-poshcode-id: 3475
# x-archived: 2016-04-20T01:36:53
# x-published: 2013-06-24T10:57:00
#
#
function Copy-ToCreateFolder
{
    param(
        [string]$src,
        [string]$dest,
        $exclude,
        [switch]$Recurse
    )
    
    # The promlem with Copy-Item -Rec -Exclude is that -exclude effects only top-level files
    # Copy-Item $src $dest    -Exclude $exclude       -EA silentlycontinue -Recurse:$recurse
    # http://stackoverflow.com/questions/731752/exclude-list-in-powershell-copy-item-does-not-appear-to-be-working
    
    if (Test-Path($src))
    {
        # nonstandard: I create destination directories on the fly
        [void](New-Item $dest -itemtype directory -EA silentlycontinue )
        Get-ChildItem -Path $src -Force -exclude $exclude | % {
            
            if ($_.psIsContainer)
            {
                if ($Recurse) # non standard: I don't want to copy empty directories
                {
                    $sub = $_
                    $p = Split-path $sub
                    $currentfolder = Split-Path $sub -leaf
                    #Get-ChildItem $_ -rec -name  -exclude $exclude -Force | % {  "{0}    {1}" -f $p, "$currentfolder\$_" }
                    [void](New-item $dest\$currentfolder -type directory -ea silentlycontinue)
                    Get-ChildItem $_ -Recurse:$Recurse -name  -exclude $exclude -Force | % {  Copy-item $sub\$_ $dest\$currentfolder\$_ }
                }
            }
            else
            {
                
                #"{0}    {1}" -f (split-path $_.fullname), (split-path $_.fullname -leaf)
                Copy-Item $_ $dest
            }
        }
    }
}