我有一個非常簡單的腳本(或者我認為),我試圖使用遞回遍歷每個檔案夾和子檔案夾,并檢查該檔案夾中是否包含“backup.log”,同時排除 OneDrive 檔案夾。我嘗試了我能想到的一切,在線搜索并嘗試了提供的解決方案,但 PowerShell 只是繼續進入 OneDrive 檔案夾。我嘗試過的一些常見腳本如下。任何幫助深表感謝。
$exclude = @("$env:OneDrive")
get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse -Exclude $exclude
get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse | Where {$_.FullName -notmatch "$env:onedrive"}
get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse | Where-Object { !($_.FullName).StartsWith($env:OneDrive) }
get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse | Where {$_.FullName -notmatch "$env:onedrive\*"}
我嘗試了更多的東西,但它幾乎遵循相同的邏輯。我還嘗試通過手動撰寫路徑來更改環境變數,但結果相同。還嘗試了 DirectoryName 而不是 FullName。
編輯:這是我得到的錯誤:
找不到路徑“C:\Users\username\OneDrive - Company\Desktop\master\packages\System.Runtime.InteropServices.RuntimeInformation\runtimes\win\lib\netstandard1.1”的一部分。在 line:1 char:1 get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse | ~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : ReadError: (C:\Users\username\netstandard1.1:String) [Get-ChildItem], DirectoryNotFoundException FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand –
uj5u.com熱心網友回復:
-exclude 不適用于 -recurse 或 -filter;排除完整路徑不起作用:
get-childitem -Path $env:USERPROFILE -Exclude 'OneDrive - Company' |
get-childitem -Filter backup.log -Recurse
在正則運算式中,路徑中的反斜杠需要轉義 \\:
get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse |
Where {$_.FullName -notmatch [regex]::escape("$env:onedrive")}
.StartsWith() 區分大小寫:
get-childitem -Path $env:USERPROFILE -Filter 'backup.log' -Recurse |
Where-Object { !($_.FullName.tolower()).StartsWith($env:OneDrive.tolower()) }
或 - 不喜歡:
dir -r ~ backup.log | ? fullname -notlike $env:onedrive*
啟用長路徑?
Set-ItemProperty HKLM:\System\CurrentControlSet\Control\FileSystem LongPathsEnabled 1 -type dword
# or
get-childitem -LiteralPath "\\?\$env:userprofile" -Recurse -filter backup.log
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527294.html
標籤:电源外壳
