Powershell:将文件和文件夹复制到新目标,但忽略某些文件和文件夹
我对Powershell非常陌生,为我们的开发过程编写了一个脚本,将文件和文件夹从(比如)文件夹1复制到文件夹2,但忽略一些文件和文件夹 我无法使下面的脚本工作Powershell:将文件和文件夹复制到新目标,但忽略某些文件和文件夹,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我对Powershell非常陌生,为我们的开发过程编写了一个脚本,将文件和文件夹从(比如)文件夹1复制到文件夹2,但忽略一些文件和文件夹 我无法使下面的脚本工作 #Exclude list of files and folders to be copied $excludeFiles = [System.Collections.ArrayList]('*.exe', '*.config', '*.targets', '*.rsp') $excludeFolders = [System.Colle
#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列表
多谢各位
Get-ChildItem
将-Exclude
和-Include
作为参数。你可以直接利用它。