2016-09-05 9 views
1

私はこの質問を編集しました。私が今やっていることは、特定の単語や文字の後に文字列を置き換えることです。私が欲しいのは、すべてのxmlタグ<xml></xml>を内容SampleValue-(whatever char after the dash)で置き換えてSampleValue-Testと置き換えることです。 PowerShellは、おそらく最も簡単な方法で使用特定の単語の後の文字列をバッチファイルのfindstrを使用して置き換えます。

@ECHO OFF 
set "file=testinput.xml" 
set "getValue=<xml>SampleValue-</xml>" 
set "setValue=<xml>SampleValue-Test</xml>" 
(
for /f "delims=" %%a IN ('findstr /N "^" %file%') DO (
set "line=%%a" 
setlocal ENABLEDELAYEDEXPANSION 
set "line=!line:%getValue%=%setValue%!" 
echo !line:*:=! 
endlocal 
) 
)>tmp 
move /Y tmp output.xml 
+3

あなたが知っている、あなたはエスケープの必要性を除去するために、引用符で変数=値のペアを囲むことができ、以下に私のコードです。例: 'set" getValue = SampleValue - 。* "'。それで 'for/f"トークン= 3 delims = - <"%% I in( 'findstr/i"%getValue% "textfile')do echo %% I'。 XMLをXMLとして解析し、必要なデータをDOMから取得する方がよいでしょう。 selectSingleNode( '// xml1/text()')。data -replace 'SampleValue - ' "' – rojo

+0

私の質問を編集しました。ありがとう:) – creulo

+0

質問は私の 'setValue'とDASH記号の後の文字列を置換テキストに置き換えるために何をすべきですか? – creulo

答えて

0

@ECHO OFF 
set "inFile=testinput.xml" 
set "outFile=testoutput.xml" 
set "getValue=<xml>SampleValue-</xml>" 
set "setValue=<xml>SampleValue-Test</xml>" 

powershell -Command "(Get-Content %inFile%) -replace '%getValue%', '%setValue%' | Set-Content %outFile%" 
関連する問題