Powershell:将文件和文件夹复制到新目标,但忽略某些文件和文件夹

时间:2018-04-25 07:38:55

标签: powershell powershell-v2.0 powershell-v3.0

我是Powershell的新手,并为我们的开发过程编写了一个脚本,用于将文件和文件夹从Folder1复制到Folder2,但忽略了一些文件和文件夹。

我无法使下面的脚本工作。

#Exclude list of files and folders to be copied
$excludeFiles = [System.Collections.ArrayList]('*.exe', '*.config', '*.targets', '*.rsp')
$excludeFolders = [System.Collections.ArrayList]('CustomConfiguration', 'abc')

function CopyFilesToFolder ($fromFolder, $toFolder, $ignoreExcludes) {
    $foldersRegex = "($($($excludeFolders.ToArray()) -join "|"))";
    $filesRegex = "($($($excludeFiles.ToArray()) -join "|"))";

    Get-ChildItem -Path $fromFolder -Recurse |
        Where-Object {($_ -ne $null) -and (($ignoreExcludes -eq $false -and $_.fullname -notmatch $foldersRegex) -or ($ignoreExcludes -eq $true)) } |
        Where-Object { ($_ -ne $null) -and (($ignoreExcludes -eq $false -and $_.fullname -notmatch $filesRegex) -or ($ignoreExcludes -eq $true)) } | 
        Copy-Item -Force -Destination {
        if ($_.GetType() -eq [System.IO.FileInfo]) {
            Join-Path $toFolder $_.FullName.Substring($fromFolder.length)
        } 
        else {
            Join-Path $toFolder $_.Parent.FullName.Substring($fromFolder.length)
        }
    }
}

P.S:另外我如何结合使用excludeFiles和excludeFolders列表?

谢谢。

0 个答案:

没有答案
相关问题