我需要從許多文件中刪除相同的html代碼。我試圖寫一個PowerShell腳本,但它不工作。腳本刪除多行html代碼
$htmlFiles = Get-ChildItem . *.html -rec
$old = '<form method="GET" action="http://localhost/index.php" name="head2">`r`n
<input type="hidden" name="akcja" value="szukaj">`r`n
<input type="hidden" name="ind" value="0" >`r`n
`r`n
<table border="0" cellpadding="1" cellspacing="0" style="margin-left:11px" >`r`n
`r`n
SOME MORE CODE
`r`n
</table>`r`n
`r`n
</form>'
$new = ""
foreach ($file in $htmlFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace $old, $new} |
Set-Content $file.PSPath
}
我用了很多'r`n,因爲我在html文件中有這個相同。也許我需要用正則表達式來完成,但超過50行的正則表達式對我來說太過分了。 我認爲腳本不工作,因爲空格不匹配。如何使它工作?
我的腳本運行但對文件 ps沒有任何影響。它需要在窗口上工作
使用Html Agility Pack進行此類工作:http://www.leeholmes.com/blog/2010/03/05/html-agility-pack-rocks-your-screen-scraping-world/ –