我使用類似下面的腳本來排除文件夾。 問題是,有一個文件夾名稱作爲文件夾名稱,我只想排除文件夾而不是文件。例如在下面我想排除「B」文件夾而不是「B.txt」文件,而下面顯示的當前代碼不包括文件和文件夾。Powershell具有匹配文件名的文件夾排除
$exclude = @("*.cer")
$excludeMatch = @("Member", "A", "B" , "C" , "D")
[regex] $excludeMatchRegEx = ‘(?i)‘ + (($excludeMatch |foreach {[regex]::escape($_)}) –join 「|」) + ‘’
Get-ChildItem -Path $source -Recurse -Exclude $exclude |
where { $excludeMatch -eq $null -or `enter code here`$_.FullName.Replace($Source, "") -notmatch $excludeMatchRegEx} |
Copy-Item -Destination {
if ($_.PSIsContainer) {
Join-Path $Dest `enter code here`$_.Parent.FullName.Substring($source.length)
} else {
Join-Path $Dest $_.FullName.Substring($source.length)
}
} -Force -Exclude $exclude
感謝您的回覆, – MAhmad
問題是我有文件夾如下面的結構文件夾C:\ Customers \ Customer1 \ Config \ x1,X2現在我想複製文件夾「Config」中的一些文件並排除文件夾「X1」和「X2」。下一層將是C: \ Customers \ Customer2 \ Config \ x1,x2,所以我想再次排除「X1」,「X2」並複製好友。底線是有幾個配置文件在每個customers文件夾下重複,我想僅複製這些文件有幾個文件夾也與我想排除的其中一個文件的名稱(僅文件夾)相匹配而不是文件 – MAhmad
如果我理解你,你只需要頂層文件vel dir?或者加上子文件夾,但不是兩個文件夾? – PSGuy