我們有一種情況,cp.exe 和 cp.dll 被復制到所有檔案夾的構建工件中,我們希望它們只成為 PS.Manager.Service 和 PS.A.Service 檔案夾/工件的一部分。
從其余所有檔案夾中,必須洗掉 cp.exe 及其 cp.dll。我嘗試使用以下腳本執行此操作,但它不起作用。我不確定我在這里錯過了什么。
任何建議都會有幫助。
謝謝。
$SourceDirectory = "e:\cache\Agent03\2\s"
$EXcludeManagerFolder = "e:\cache\Agent03\2\s\PS.Manager.Service"
$EXcludeAFolder = "e:\cache\Agent03\2\s\PS.A.Service"
gci -path "$SourceDirectory" -Include "cp.exe, cp.dll" -Exclude "$EXcludeManagerFolder","$EXcludeAFolder " -Recurse -Force | Remove-Item -Recurse -Force
uj5u.com熱心網友回復:
正如zett42已經評論的那樣,Include, Exclude(以及Filter)僅適用于物件名稱,而不是完整路徑。
最簡單的方法是使用您需要排除的檔案夾創建一個正則運算式字串,以便您可以匹配(或-notmatch在這種情況下)
$SourceDirectory = "e:\cache\Agent03\2\s"
# create a regex of the files and/or folders to exclude
$exclude = 'PS.Manager.Service', 'PS.A.Service' # foldernames to exclude
# each item will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($exclude | ForEach-Object { [Regex]::Escape($_) }) -join '|'
(Get-ChildItem -Path $SourceDirectory -Include 'cp.exe', 'cp.dll' -File -Recurse).FullName |
Where-Object { $_ -notmatch $notThese } |
Remove-Item -Force
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/493363.html
