Tuesday, 15 January 2013

Replace multiple text in a text file using vbscript -



Replace multiple text in a text file using vbscript -

i trying replace 3 text text file.i tried this-

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

i not able figure out how multiple replacement.the code working perfect single replace function not more one...plz help

you need phone call replace on results of previous 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

reuse single variable, instead of having strnewtext, strnewtext1, strnewtext2.

strtext = replace(strtext, "aron_", "lori_") strtext = replace(strtext, "aron", "jason") strtext = replace(strtext, "sketa", "skicia")

and later:

objfile.writeline strtext

also, might want consider using regular expressions matching multiple values @ time. (search on vbscript regular expressions or vba regular expressions.)

vbscript replace text-files

No comments:

Post a Comment