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

前端 未结 10 865
旧时难觅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条回答
  • 2020-12-02 12:26

    I was looking for a way to copy files modified after a certain date/timestamp so as to archive them. This way I could save off exactly what files I worked on (assuming I know when I started). (Yes, I know this is what SCM is for, but there are times when I just want to snapshot my work without checking it in.)

    Using landyman's tip, and stuff I found elsewhere, I found that this worked:

    $source = 'c:\tmp\foo'
    $dest = 'c:\temp\foo'
    $exclude = @('*.pdb', '*.config')
    Get-ChildItem $source -Recurse -Exclude $exclude |  
        where-object {$_.lastwritetime -gt "8/24/2011 10:26 pm"} | 
        Copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}
    
    0 讨论(0)
  • 2020-12-02 12:26
    $sourcePath="I:\MSSQL\Backup\Full"
    $excludedFiles=@("MASTER", "DBA", "MODEL", "MSDB")
    $sourceFiles=(ls $sourcePath -recurse -file) | where-object { $_.directory.name -notin $excludedFiles }
    

    this is what i did, i needed to copy out a bunch of backup files to a separate location on the network for client pickup. we didn't want them to have the above system DB backups.

    0 讨论(0)
  • 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)
      }
    }
    
    0 讨论(0)
  • 2020-12-02 12:31

    I had a similar problem extending this a bit. I want a solution working for sources like

    $source = "D:\scripts\*.sql"
    

    too. I found this solution:

    function Copy-ToCreateFolder
    {
        param(
            [string]$src,
            [string]$dest,
            $exclude,
            [switch]$Recurse
        )
    
        # The problem 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
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:37

    The below snippet will copy all files and folders from $source to $dest, excluding .pdb and .config files from the root folder and sub-folders:

    Get-ChildItem -Path $source | Copy-Item -Destination $dest -Recurse -Container -Exclude @('*.pdb','*.config')
    
    0 讨论(0)
  • 2020-12-02 12:38

    The exclude parameter won't work with dirs. A variant of Bo's script does the trick:

    $source = 'c:\tmp\foo'
    $dest = 'c:\temp\foo'
    $exclude = '\.bak'
    Get-ChildItem $source -Recurse  | where {$_.FullName -notmatch $exclude} | 
        Copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}
    
    0 讨论(0)
提交回复
热议问题