简体   繁体   中英

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?

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()
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM