Venkat
Venkat

Reputation: 89

Exclude few folders while copying data and copy remaining folders and only certain file types in Powershell

I have a source path defined in a variable ( reading the data from configuration xml file ) . The source contains the below structure. SourcePath value : \servername\D$\BACKUP Inside the backup folder, I have many sub folders. Each of the sub folder contains *.log files, *.bak files. I want to exclude few sub folders like - temp , model, msdb and copy the rest of the folders. In the folders which are being copied, it should only copy the bak file type. I have tried this, but the code os not complete, as I am not sure of filtering the file types and the folders. I have tried the below copy-item script

 $FileTypes = "*.bak"
  $excludes = "master","model","msdb","test01"

Get-ChildItem $DBSourcePath -Directory | 
    Where-Object{$_.Name -notin $excludes} | 
    Copy-Item -Destination $DestPath -Recurse -Force

How to achieve this in powershell?

Upvotes: 0

Views: 109

Answers (1)

Farbkreis
Farbkreis

Reputation: 644

It should be something like this:

It might still need a little bit debuggin but you should get the idea


$files = @()
$FileTypes | foreach {$files += Get-ChildItem $DBSourcePath -recurse -Filter $_}

[System.Collections.ArrayList]$files = $files 



for($i=0;$i -lt $files.Count; $i++) {
    $folder = Split-Path $files[$i].fullname
    $excludes | foreach {
        if($folder.Contains($_)) {
            $files[$i].Remove()
        }
    }
}

Upvotes: 0

Related Questions