我是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列表?
谢谢。