# VB6 find and replace in external file



## iamaran (Dec 22, 2005)

What I need to do is probably simple, but Im not really into VB.
It is this:

I want to hardcode find and replace text in an XML document (about 10 different replacements). These will be the same every time and the document will always be at c:\xxx\yyy.xml

So, for example, the XML document contains something like:

<Name>
<Forename>John</Forename>
<Surname>Smith</Surname>
</Name>
<Name>
<Forename>Peter</Forename>
<Surname>Jones</Surname>
</Name>

I want to find and replace every <Forename> with <Forename ID=1>, every <Surname> with <Surname ID=2> etc. etc. and I will want to do this regularly, the same replacements every time.

There are lots of find and replace utilities, but I would have to type in my find and replace criteria manually every time.

What Id like to do is create a small .EXE to do the job with the find and replace strings coded in.

Can anyone point me in the right direction please?
TIA.


----------



## coderitr (Oct 12, 2003)

```
Private Sub Main()
   Dim intext As String
   Dim outtext As String

   Open "c:\xxx\yyy.xml" For Input As #1
   Open "c:\xxx\yyy.new" For Output As #2
   
   Do While Not EOF(1)
      Line Input #1, intext
      
      If InStr(1, intext, "Forename") > 0 Then
         outtext = Replace(intext, "Forename", "Forename ID=1")
      ElseIf InStr(1, intext, "Surname") > 0 Then
         outtext = Replace(intext, "Surname", "Surname ID=2")
      Else
         outtext = intext
      End If
      
      Print #2, outtext
   Loop
   
   Close

   Kill "c:\xxx\yyy.xml"
   Name "c:\xxx\yyy.new" As "c:\xxx\yyy.xml"

End Sub
```
Note the omission of the "<" and ">" on the string manipulation functions. VB doesn't like doing string operations on those based on my experience. That means you'll have to be careful that you don't run the program on a file that's already been converted.

Welcome to TSG!


----------



## coderitr (Oct 12, 2003)

You might add a comment at the top of the new file indicating that it has already been converted and then if you find that in the top of the source file then abort. Just a thought.


----------



## iamaran (Dec 22, 2005)

Many thanks!

Mind if I ask another question?
How do I replace double Chr(13) & Chr(10)?
I.e. Chr(13) & Chr(10) & Chr(13) & Chr(10)
This doesn't seem to work as the text is read in "line by line".

Thank you.


----------



## coderitr (Oct 12, 2003)

Try creating a string of your own containing those characters:


```
Const newstr As String = Chr(13) & Chr(10) & Chr(13) & Chr(10)
```
Then use the Replace function with the vbBinaryCompare as the fourth (?) parameter. I haven't tested this so I don't know what you'll get.

You're welcome.


----------



## iamaran (Dec 22, 2005)

Thanks again, but it wasn't working, probably because the lines of formatted XML were 2033 characters long (a limitation of ODBC). I needed to strip the lines between the batches of 2033 characters. I did it like this (found elsewhere):

Dim strFileText As String
Dim fnum As Integer
fnum = FreeFile
Open "c:\temp\xxx.xml" For Binary As fnum
strFileText = Space$(LOF(fnum))
Get #fnum, , strFileText
Close #fnum
strFileText = Replace(strFileText, vbCrLf & vbCrLf, "")
fnum = FreeFile
Open "c:\temp\xxx.new" For Binary As fnum
Put #fnum, , strFileText
Close #fnum
Kill "c:\temp\xxx.xml"
Name "c:\temp\xxx.new" As "c:\temp\xxx.xml"

Thanks for all your help with this one.


----------

