VB6 find and replace in external file

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

iamaran

Thread Starter
Joined
Dec 22, 2005
Messages
12
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.
 
Joined
Oct 12, 2003
Messages
3,015
Code:
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!
 
Joined
Oct 12, 2003
Messages
3,015
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

Thread Starter
Joined
Dec 22, 2005
Messages
12
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.
 
Joined
Oct 12, 2003
Messages
3,015
Try creating a string of your own containing those characters:

Code:
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

Thread Starter
Joined
Dec 22, 2005
Messages
12
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top