2013-11-21 37 views
0

我需要針對大約3000個文檔的庫運行以下命令,但一直在阻止獲取正常工作正則表達式(不是我最強壯的套裝),或者等效於PowerShell中的/v選項。有人可以請指點我正確的方向嗎?如何在PowerShell中使用此命令?

我的命令

C:\findstr /v "<?xml version=" filename.htm > ..\testOut\filename.htm 

我在哪裏使用PowerShell

到目前爲止
(Get-Content ($srcfiles)) | Foreach-Object {$_.srcfiles -replace '<?xml version="1.0" encoding="utf-8"?>', ("")} | Set-Content ($srcfiles) 

回答

0

Get-Content返回線的陣列,而不是文件的全文爲一個字符串。

如果你正在做的是試圖從每個文件刪除XML聲明,試試這個,假設$srcfiles是完整的文件路徑的集合:

foreach($file in $srcfiles) 
{ 
    $content = Get-Content $file | ? { $_ -notmatch "<\?xml[^>]+>" } 
    $content | Set-Content $file -Force 
} 

基本上,遍歷所有的文件,得到每個文件的內容,忽略任何xml聲明行,然後將該數據推回原始文件。我分兩步這樣做,因爲PowerShell中不會讓你寫的內容到你在獲取數據的同一管道的文件。

0
$path = "C:\Path\To\Documents" 
$outputPath = "C:\Path\To\OutputDocuments" 

Get-ChildItem $path | % { 
    $content = (Get-Content -Raw $_) -replace '<?xml version="1.0" encoding="utf-8"?>', '' 
    $fileName = Join-Path $outputPath $_.Name 
    Set-Content -Path $fileName -Value $content 
} 

如果你在PowerShell的2.0或更低,取代「獲取「內容-Raw」和「Get-Content -ReadCount 0」。

您還需要過濾Get-ChildItem的輸出以僅返回文件而不是目錄。在PowerShell 3.0或更高版本中,您可以將「-File」參數添加到Get-ChildItem。否則,試試這個:

Get-ChildItem $path | ? { $_.GetType() -eq "FileInfo" } | % { 
相關問題