Search code examples
powershellpowershell-4.0copy-item

Powershell not excluding specific sub folders when use copy-item command


I'm bit new dumb in PS. I've gone through very much all questions logged here and tried my level best to work for me, but unfortunately didn't work for me as I want to.

I'm trying to copy the whole folder structure apart of excluding some. For e.g I want to copy whole c:\Program Files\myTest to Destination C:\Program Files\TargetTest except (exclude below folders) c:\Program Files\myTest\_Backup and c:\Program Files\myTest\Processor\Reports

     $SourceFolder = "$env:ProgramFiles\myTest\"
     $ReportExclude="$env:ProgramFiles\myTest\Processor\Reports\*.rdl"
     $TargetFolder = "$env:ProgramFiles\TargetTest"
     $BackupRootFolderName = "_Backup"
     $BackupRootFolderPath = "$TargetFolder\$BackupRootFolderName"
     $BackupFolderPath = "$BackupRootFolderPath\$(Get-Date -format yyyyMMdd_HHmmss)"

      Get-Item -Path $SourceFolder\* -Exclude $ReportExclude, _Backup| %  { write-host $_.fullname; Copy-Item -path $_.fullname -destination $TargetFolder -recurse}

When I run above code it copies everything and exclude the only _Backup but copy the Reports folder/files anyway, which I don't want to be copied.

Could someone please look into this and point me out what I'm doing wrong.

Thank you very much


Solution

  • solution was not that easy, the -Exclude switch exclude folder but not sub items, the -Filter switch use wildcard but take a single string as parameters.. (by the way @whatever was right when he said the -recurse switch on Copy-Item cancel your exclusion on Get-ChildItems)

    finally i solved it in a dirty but working way :

    Get-ChildItem -Path $SourceFolder -Recurse | ? { $_.FullName -notlike $ReportExclude -and $_.FullName -notlike $BackupRootFolderName } | % { write-host $_.FullName; Copy-Item -Path $_.FullName -Destination $($_.FullName -replace $SourceFolder, $TargetFolder) -Force} 
    

    Edit, because i don't understand what is not working in your configuration, let's test a full script with loggin :

    $SourceFolder = "$env:ProgramFiles\myTest\" 
    $ReportExclude = "$env:ProgramFiles\myTest\Processor\Reports\*.rdl" 
    $TargetFolder = "$env:ProgramFiles\TargetTest" 
    
    $BackupRootFolderName = "_Backup" 
    $BackupRootFolderPath = "$TargetFolder\$BackupRootFolderName" 
    $BackupFolderPath = "$BackupRootFolderPath\$(Get-Date -format yyyyMMdd_HHmmss)" 
    
    $ErrorActionPreference = "Continue"
    $WarningPreference = "Continue"
    $VerbosePreference = "Continue"
    
    Function Copy-MyFiles($FileFullNameSource)
    {
        try
        {
            Write-Verbose "file source : $FileFullNameSource"
    
            if ($FileFullNameSource -notlike $ReportExclude -and $FileFullName -notlike $BackupRootFolderName)
            {
                $FileFullNameDestination = $FileFullNameSource -replace $SourceFolder, $TargetFolder
    
                Write-Verbose "file destination : $FileFullNameDestination"
    
                Copy-Item -Path $FileFullNameSource -Destination $FileFullNameDestination -Force
    
                if (Test-Path $FileFullNameDestination)
                {
                    Write-Host "$FileFullNameDestination ok" -ForegroundColor Green
                }
                else
                {
                    Write-Warning "$FileFullNameDestination is missing in destination folder"
                }
            }
            else
            {
                Write-Host "$FileFullNameSource is excluded"
            }
        }
        catch
        {
            Write-Warning ($_.Exception | Select-Object -Property Message |  Out-String)
        }
    }
    
    Write-Verbose "source folder : $SourceFolder"
    Write-Verbose "target folder : $TargetFolder"
    
    Get-ChildItem -Path $SourceFolder -Recurse | % { Copy-MyFiles $_.FullName }