2

テキストファイルから3つのテキストを置き換えようとしています。これを試しました-

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Aron_", "Lori_")
strNewText1 = Replace(strText, "Aron", "Jason")   'Not working
strNewText2 = Replace(strText, "Sketa", "Skicia") 'Not working


Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForWriting)
objFile.WriteLine strNewText 
objFile.WriteLine strNewText1 'Not working
objFile.WriteLine strNewText2 'Not working


objFile.Close

複数の置換を行う方法がわかりません。単一の置換関数に最適なコードですが、複数の置換はできません...plzヘルプ

4

1 に答える 1

9

Replaceあなたは前の結果を呼び出す必要がありますReplace

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Aron_", "Lori_")
strNewText1 = Replace(strNewText, "Aron", "Jason")
strNewText2 = Replace(strNewText1, "Sketa", "Skicia")

Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForWriting)
objFile.WriteLine strNewText2 

objFile.Close


strNewText、、を使用する代わりに、 実際には1つの変数を再利用できstrNewText1ますstrNewText2

strText = Replace(strText, "Aron_", "Lori_")
strText = Replace(strText, "Aron", "Jason")
strText = Replace(strText, "Sketa", "Skicia")

以降:

objFile.WriteLine strText


また、一度に複数の値を照合するために正規表現を使用することを検討することもできます。(SOでVBScript正規表現またはVBA正規表現を検索します。)

于 2013-02-20T05:52:57.633 に答える