我正在嘗試修改下面的 .pgm 檔案示例資料。
<?xml version="1.0" ?>
<Document>
<Attachments>
<File Name="H:\PT000224.pdf"/>
上面我需要將上面檔案名中的 H: 更改為 L: 并讓它遍歷多個檔案并進行相同的更改。我已經從這個開始了。
Get-ChildItem "C:\Users\test\Desktop\Change PGM" | ForEach-Object {
$content = Get-Content $_.FullName
$content = ForEach-Object { $content -like "*H:*" -replace "H:","Z:"}
Set-Content $_.FullName $content -Force
}
它會遍歷所有內容并更改我想要的值,但會洗掉 .pgm 檔案中的所有其他內容。
uj5u.com熱心網友回復:
洗掉-like操作 - 如果輸入字串不包含H:,-replace將簡單地按原樣回傳字串,因此無需“預過濾”資料:
PS ~> @('H:', 'I:') -replace 'H:','Z:'
Z:
I:
因此,您可以按如下方式簡化代碼:
Get-ChildItem "C:\Users\test\Desktop\Change PGM" | ForEach-Object {
# read file contents
$content = Get-Content $_.FullName
# replace and write back to disk
$content -replace "H:","Z:" |Set-Content $_.FullName -Force
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434572.html
標籤:电源外壳
