我有這個檔案結構

在 PowerShell 中,我的位置設定為Folder. SubSubFolders有很多 xml 檔案,只有當version.txt檔案內容a存在并且該行不存在時,我才想在那里添加一行。
我能夠弄清楚如何特別更改 xml 檔案SubSubFolder,但是當我在Folder檔案夾中開始并考慮到時我無法做到這一點version
#here I need to add: only if version.txt content of xml file in parent folder is "a"
$files = Get-ChildItem -Filter *blah.xml -Recurse | Where{!(Select-String -SimpleMatch "AdditionalLine" -Path $_.fullname -Quiet)} | Format-Table FullName
foreach($file in $files)
{
(Get-Content $file.FullName | Foreach-Object { $_
if ($_ -match "AdditionalLineAfterThisLine")
{
"AdditionalLine"
}
}) | Set-Content $file.FullName
}
uj5u.com熱心網友回復:
如果我理解正確,您正在尋找以下內容:
$files = (
Get-ChildItem -Filter *blah.xml -Recurse |
Where-Object{
-not ($_ | Select-String -SimpleMatch "AdditionalLine" -Quiet) -and
(Get-Content -LiteralPath "$($_.DirectoryName)/../version.txt") -eq 'a'
}
).FullName
請注意,假設是version.txt檔案只包含一個行。如果它包含多行,則該-eq 'a'操作將充當過濾器并回傳內容為 的所有行,如果存在一個或多個此類行,則'a'在 的隱含布爾背景關系-and中將產生$true。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371183.html
標籤:电源外壳
