powershell 腳本錯誤了幾次 b/c 遺漏的逗號等。現在它似乎運行沒有錯誤,但沒有任何反應——檔案沒有移動,文本沒有被替換。
僅供參考,我讓替換自己作業 - 它掃描了所有 .XML 檔案并用較大的字串替換了那個小字串。替換確實包括 XML 中需要的新行和空格,但我在此示例中對其進行了簡化,以便于閱讀。
一如既往,非常感謝所有幫助。
編輯:添加解決方案。謝謝你,TheMadTechnician
$CurrFilePath = "C:\Folder\Path1"
$TransIncludePattern = "*.xml"
$ProcessFilePath = "C:\Folder\Path2"
try #Get-ChildItem -Path $CurrFilePath\* -Include $TransIncludePattern | Select-Object -First 1 | ForEach-Object
{
$CurrFileName = Get-childitem -Path $CurrFilePath\* -Include $TransIncludePattern;
$CurrFileName |
Select-Object -ExpandProperty FullName |
ForEach-Object {
Write-Host "Changing file $_";
$content = (Get-Content $_);
$newContent = $content | ForEach-Object {
($_-replace '</InitgPty>', '<Id> <OrgId> <Id>DAMNID</Id> </OrgId> </Id></InitgPty>');
}
$newContent | Set-Content $_; #or [IO.File]::WriteAllLines($_, $newContent) :)
Write-Host "File Changed: $_";
}
Write-Host "Has DamnID";
Move-Item -CurrFileName $file -Destination $ProcessFilePath;
exit 0;
}
catch {
Write-Error $_;
}
作業腳本:
$CurrFilePath = "C:\Folder\Path1"
$TransIncludePattern = "*.xml"
$ProcessFilePath = "C:\Folder\Path2"
try #Get-ChildItem -Path $CurrFilePath\* -Include $TransIncludePattern | Select-Object -First 1 | ForEach-Object
{
$CurrFileName = Get-childitem -Path $CurrFilePath\* -Include $TransIncludePattern;
$CurrFileName |
Select-Object -ExpandProperty FullName |
ForEach-Object {
$content = (Get-Content $_);
$newContent = $content | ForEach-Object {
($_-replace '</InitgPty>', '<Id> <OrgId> <Id>DAMNID</Id> </OrgId> </Id></InitgPty>');
}
$NewContent = Set-Content -Path $_ -Value $newContent;
}
Move-Item -Path .\*.xml -Destination $ProcessFilePath;
exit 0;
}
catch {
Write-Error $_;
}
uj5u.com熱心網友回復:
你失去了$_ 當你去的背景關系Set-Content。 $newContent = Set-Content $_我假設$_應該參考檔案名,但此時它包含檔案的修改內容。試試Set-Content -Path $_ -Value $newContent在那條線上。
然后,當您移動檔案時,該引數-CurrFileName不是有效引數。而是使用Move-Item -Path $_ -Destination $ProcessFilePath
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474179.html
標籤:电源外壳
