2016-05-16 9 views
1

キャリッジリターン( `r)&改行文字(` n)をXMLファイルから削除するにはヘルプが必要です。 1PowershellスクリプトでXMLファイルからキャリッジリターンと改行を削除する

入力ファイル形式

<File1> 
<SubFile1> </SubFile1> 
<SubFile2> </SubFile2> 
<SubFile3> </SubFile3> 
......... 
<SubFilen> </SubFilen> 
</File1> 

コードが

$content = [IO.File]::ReadAllText($input_File) 
$content = $content.Replace("`r","") 
$content = $content.Replace("`n","") 
[system.io.file]::WriteAllText($Output_File,$content) 
を使用:行の600メガバイト

番号:私はエラー

ファイルのサイズをSystem.OutOfMemoryExceptionにを取得しています

Get-Content 
私はMaxMemoryPerShellMB 1024、2048、4096が、運と試みました。

+0

'(ゲット・コンテンツを\ file.xml。)私はPowerShellのプロンプトから実行した場合、私はすべてのエラーを取得していないよ –

+0

を-join'''。 「C:\ Windows \ syswow64 \ Windowspowershell \ v1.0 \ Powershell.exe」のように実行しようとするとエラーが発生します。「C:\ Temp \ File_Encoding.ps1」 noprofile -executionpolicy bypass -File "C:\ Temp \ Change_File_Encoding.ps1" "input.xml" "output.xml" – Praveen

答えて

1

Don Jones Why Get-Content Ain’t Yer Friendからの素晴らしい記事があります。

StreamReaderを使用してファイルを1行ずつ読み込み、StreamWriterを使用して、新しい(一時)ファイルを1行ずつ書き込むようにしてください。あなたが終了したら、単にファイルを置き換える:

$streamReader = New-Object System.IO.StreamReader -Arg "yourFile.xml" 
$streamWriter = [System.IO.StreamWriter] "tmp.xml" 

while ($line = $streamReader.ReadLine()) { 
    $replacedLine = $line -replace '`r|`n' 
    $streamWriter.Write($replacedLine); 
} 

$streamReader.close() 
$streamWriter.close() 

Remove-Item "yourFile.xml" -Force 
Rename-Item "tmp.xml" "yourFile.xml" 
+0

WriteLine()は別の改行を導入し、目的を破る –

+0

もちろん、ヒントのおかげです。代わりに 'Write'を使う必要があります - 私の答えを修正しました。 –

+0

まだ同じエラーが発生しています。 'エラーが発生しました - コントロールがキャッチブロックに渡されました 'System.OutOfMemoryException'タイプの例外がスローされました。 56行目でエラーが発生しました – Praveen

関連する問題