Replace multiple characters or strings in a text file
Windows Server Forum Index Windows Server
Server discussion on Windows platform.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web winserverhelp.com
Replace multiple characters or strings in a text file

 
Post new topic   Reply to topic    Windows Server Forum Index -> Programming
Author Message
John
Guest





Posted: Sat Oct 22, 2005 8:50 pm    Post subject: Replace multiple characters or strings in a text file Reply with quote

How can I change the script below to search and replace multiple characters?
The current script only changes "a" to "2".
I would like to add many more, such as:
"b" to "7"
"q" to "4"
"a" to "2"
"s" to "1"

I'm using this script from
http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx

------------------------------------------------------------
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("xp.enc", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "a", "2")

Set objFile = objFSO.OpenTextFile("xp.enc", ForWriting)
objFile.Write strNewText
objFile.Close
------------------------------------------------------------

TIA.
Back to top
McKirahan
Guest





Posted: Sat Oct 22, 2005 8:50 pm    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

"John" <anonymous@microsoft.com> wrote in message
news:ObzYfD01FHA.2008@TK2MSFTNGP10.phx.gbl...
Quote:
How can I change the script below to search and replace multiple
characters?
The current script only changes "a" to "2".
I would like to add many more, such as:
"b" to "7"
"q" to "4"
"a" to "2"
"s" to "1"

[snip]


Will this help? Watch for word-wrap.

Option Explicit
'*
'* Declare Constants
'*
Const cVBS = "replaces.vbs"
Const cOTF = "xp.enc"
Const ForReading = 1
Const ForWriting = 2
'*
'* Declare Variables
'*
Dim strOTF
Dim intREP
intREP = 1
Dim strREP
strREP = "s=1,a=2,q=4,b=7"
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
'*
'* Read File
'*
Set objOTF = objFSO.OpenTextFile(cOTF,ForReading)
strOTF = objOTF.ReadAll
Set objOTF = Nothing
'*
'* Replace Characters
'*
Do
strOTF = Replace(strOTF,Mid(strREP,intREP,1),Mid(strREP,intREP+2,1))
intREP = intREP + 4
If intREP > Len(strREP) Then Exit Do
Loop
'*
'* Write File
'*
Set objOTF = objFSO.OpenTextFile(cOTF,ForWriting,True)
objOTF.Write(strOTF)
Set objOTF = Nothing
'*
'* Destroy Objects
'*
Set objFSO = Nothing
'*
'* Finish
'*
MsgBox "Done!",vbInformation,cVBS
Back to top
Guest






Posted: Sun Oct 23, 2005 12:50 pm    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

John wrote:

Quote:
How can I change the script below to search and replace multiple characters?
The current script only changes "a" to "2".
I would like to add many more, such as:
"b" to "7"
"q" to "4"
"a" to "2"
"s" to "1"


Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set FileIn = objFSO.OpenTextFile("xp.enc", ForReading)

strNewText = EncodeSTR( FileIn.ReadAll )
FileIn.Close

Set FileIn = objFSO.OpenTextFile("xp.enc", ForWriting)
FileIn.Write strNewText
FileIn.Close

WScript.Quit

Function EncodeSTR( STRz )
' from Christoph Basedau in
'
http://groups.google.de/groups?q=ToASCII+%3D+Replace&hl=de&lr=&newwindow=1&selm=ugGVQok3AHA.1604%40tkmsftngp02&rnum=1
EncodeSTR = Replace(STRz, "a", "2")
EncodeSTR = Replace(EncodeSTR, "b", "7")
EncodeSTR = Replace(EncodeSTR, "q", "4")
EncodeSTR = Replace(EncodeSTR, "s", "1")
EncodeSTR = Replace(EncodeSTR, Chr(214), chr(153))
EncodeSTR = Replace(EncodeSTR, Chr(246), chr(148))
EncodeSTR = Replace(EncodeSTR, Chr(223), chr(225))
End Function ' EncodeSTR( STRz )
Back to top
McKirahan
Guest





Posted: Mon Oct 24, 2005 12:50 am    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

"John" <anonymous@microsoft.com> wrote in message
news:#zN0RoB2FHA.2064@TK2MSFTNGP09.phx.gbl...
Quote:
Works Perfectly.
Can you explain how intREP is being used.

Thank you.
John


strREP = "s=1,a=2,q=4,b=7"

Do
strOTF = Replace(strOTF,Mid(strREP,intREP,1),Mid(strREP,intREP+2,1))
intREP = intREP + 4
If intREP > Len(strREP) Then Exit Do
Loop

Starting at 1 and incrementing by 4,
"intREP" refers to psoitions in the "strREP" string:
"Mid(strREP,intREP,1)" = "s" then "a" then "q" then "b";
"Mid(strREP,intREP,1+2)" = "1" then "2" then "4" then "7".

Just expand the string for assitional characters; for example,
strREP = "s=1,a=2,q=4,b=7,x=8"
Back to top
John
Guest





Posted: Mon Oct 24, 2005 12:50 am    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

That's perfect. Exactly what I want.

Thank you.
John
Back to top
John
Guest





Posted: Mon Oct 24, 2005 12:50 am    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

Works Perfectly.
Can you explain how intREP is being used.

Thank you.
John
Back to top
McKirahan
Guest





Posted: Wed Nov 09, 2005 9:51 pm    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

"John" <anonymous@microsoft.com> wrote in message
news:etccrRW5FHA.2560@TK2MSFTNGP12.phx.gbl...
Quote:
How can I change this script to run in an html page? I'd like to use a
form.
See http://www.irt.org/script/242.htm for an example.

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set FileIn = objFSO.OpenTextFile("file.txt", ForReading)
strNewText = EncodeSTR( FileIn.ReadAll )
FileIn.Close

Set FileIn = objFSO.OpenTextFile("file.txt", ForWriting)
FileIn.Write strNewText
FileIn.Close
WScript.Quit

Function EncodeSTR( STRz )
EncodeSTR = Replace(STRz, "a", "2")
EncodeSTR = Replace(EncodeSTR, "b", "7")
EncodeSTR = Replace(EncodeSTR, "q", "4")
EncodeSTR = Replace(EncodeSTR, "s", "1")
End Function



What are you trying to do now?

Before it was a ".vbs" file and now you want it in an ".htm" file?

VBScript code will only run in IE browsers.

For cross-platform usage you should use JavaScript.

Also, reading and writing files is a security violation.

Please clarify your requirements and environment.
Back to top
John
Guest





Posted: Wed Nov 09, 2005 9:51 pm    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

I'm still using the other version too. You didn't do all that work for nothing.
Not trying to be a pain here. It's just a great learning experience.

I don't want to access the file system from the html page.
I'd like to manually paste text into the form's text area in an IE browser and click a button to convert it.

The JavaScript version at http://www.irt.org/script/242.htm does exactly what I want,
but I'd like to see it written in VBScript using the one provided by hrwagner@web.de from Christoph Basedau.
Back to top
McKirahan
Guest





Posted: Wed Nov 09, 2005 9:51 pm    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

"John" <anonymous@microsoft.com> wrote in message
news:eI$8XvW5FHA.3636@TK2MSFTNGP09.phx.gbl...
Quote:
I'm still using the other version too. You didn't do all that work for
nothing.
Not trying to be a pain here. It's just a great learning experience.

I don't want to access the file system from the html page.
I'd like to manually paste text into the form's text area in an IE browser
and click a button to convert it.

The JavaScript version at http://www.irt.org/script/242.htm does exactly
what I want,
but I'd like to see it written in VBScript using the one provided by
hrwagner@web.de from Christoph Basedau.



See my reply under
Re: Stack error at line: # when running text replace script
in
microsoft.public.inetexplorer.scripting,
microsoft.public.scripting.jscript
Back to top
John
Guest





Posted: Wed Nov 09, 2005 9:51 pm    Post subject: Re: Replace multiple characters or strings in a text file Reply with quote

How can I change this script to run in an html page? I'd like to use a form.
See http://www.irt.org/script/242.htm for an example.

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set FileIn = objFSO.OpenTextFile("file.txt", ForReading)
strNewText = EncodeSTR( FileIn.ReadAll )
FileIn.Close

Set FileIn = objFSO.OpenTextFile("file.txt", ForWriting)
FileIn.Write strNewText
FileIn.Close
WScript.Quit

Function EncodeSTR( STRz )
EncodeSTR = Replace(STRz, "a", "2")
EncodeSTR = Replace(EncodeSTR, "b", "7")
EncodeSTR = Replace(EncodeSTR, "q", "4")
EncodeSTR = Replace(EncodeSTR, "s", "1")
End Function
Back to top
 
Post new topic   Reply to topic    Windows Server Forum Index -> Programming All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




New Topics Powered by phpBB