Replacing the First Occurence of a Word in a Text File

Hi there I have a piece of code that loops through all the files in a folder. I need some code which will replace a variable "Variable A" with the 3rd occurence of the word "the" in a text file. Here is the code I have so far. Seems to do the looping fine, however this part

 if instr(sFile,sFromString) > 0 then
                  sFile = left(sFile,instr(sFile,sFromString)-1) & sToString & mid(sFile,instr(sFile,sFromString)+len(sFromString))
            end if

Is clearly wrong as it completely mashes up my files as you can see here.

http://www.mayoor.co.uk/backyard_pondsUpdated.TXT (after update)

http://www.mayoor.co.uk/backyard_ponds.TXT (before update)

Anyone have any better ways of doing the replace?
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% Option Explicit %>
 
<!-- #include file="includes/connection.asp" -->
<!-- #include file="includes/adovbs.inc" -->
 
<%
 
Dim adocmd, objRSURLS, sFold, fso, fold, fil, f, sFile, sToString, sFromString
 
'Gather the checkboxes for the key Features
Set adocmd = Server.CreateObject("ADODB.Command")
With adocmd            
      .ActiveConnection = objConn
      .CommandText = "p_RetrieveURLS"
      .CommandType = adCmdStoredProc      
      Set objRSURLS = .Execute      
End With
Set adocmd = Nothing
 
if not objRSURLS.EOF then
 
    sFold = "output/articles/"
    set fso = server.CreateObject("Scripting.FileSystemObject")
    if fso.FolderExists(server.mappath(sFold)) then
          set fold = fso.getFolder(server.mappath(sFold))
          for each fil in fold.files
                set f=fso.OpenTextFile(server.Mappath(sFold & fil.name),1)
                sFile = f.ReadAll
                f.close 
                sToString = objRSURLS.Fields("url")
            sFromString = "the"
            if instr(sFile,sFromString) > 0 then
                  sFile = left(sFile,instr(sFile,sFromString)-1) & sToString & mid(sFile,instr(sFile,sFromString)+len(sFromString))
            end if
                set f=fso.OpenTextFile(server.Mappath(sFold & fil.name),2)
                f.write sFile
                f.close
            objRSURLS.MoveNext
          next
          response.write "Articles updated"      
    else
          response.write "No articles found."
    end if
    set fso = nothing
 
end if
objRSURLS.close
Set objRSURLS = Nothing
 
%>

Open in new window

LVL 1
MayoorPatelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BadotzCommented:
>> I need some code which will replace a variable "Variable A" with the 3rd occurence of the word "the" in a text file.

This doesn't make sense to me. Can you clarify what you are trying to do please?
0
RobSampsonCommented:
Hi, try this between your for and next tags to change the specified occurrance of sFromString to sToString:

                set f=fso.OpenTextFile(server.Mappath(sFold & fil.name),1)
                sFile = f.ReadAll
                f.close
                sToString = objRSURLS.Fields("url")
            sFromString = "the"
            sToString = "my"
            intOccurrance = 3
            intPos = InStr(sFile,sFromString)
            For intCount = 1 To intOccurrance - 1
                  If intPos > 0 Then
                        intPos = InStr(intPos + 1, sFile, sFromString)
                  End If
            Next
            If intPos > 0 Then
                  sFile = Left(sFile, intPos - 1) & sToString & Mid(sFile, intPos + Len(sFromString))
            End If
                set f=fso.OpenTextFile(server.Mappath(sFold & fil.name),2)
                f.write sFile
                f.close


Regards,

Rob.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

MayoorPatelAuthor Commented:
Nope that didnt work, question is continued in this thread.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_23107331.html
0
RobSampsonCommented:
This is the code (shown above) that I have used to change an occurance of the word from a text file. This is written in VBScript, so you can test it with a VBS file.

'==================
Set fso = CreateObject("Scripting.FileSystemObject")
sFold = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
Set fil = fso.GetFile(sFold & "textfile.txt")
Set f=fso.OpenTextFile(sFold & fil.name, 1)
sFile = f.ReadAll
f.close
'sToString = objRSURLS.Fields("url")
sToString = "my"
sFromString = "the"
intOccurrance = 3
intPos = InStr(sFile,sFromString)
For intCount = 1 To intOccurrance - 1
      If intPos > 0 Then
            intPos = InStr(intPos + 1, sFile, sFromString)
      End If
Next
If intPos > 0 Then
      sFile = Left(sFile, intPos - 1) & sToString & Mid(sFile, intPos + Len(sFromString))
End If
Set f=fso.OpenTextFile(sFold & fil.name, 2)
f.write sFile
f.close
MsgBox "Done"
'==================

and this is the text file that I tested against, before running it:
This is the first line
This is the second line
This is the third line
This is the fourth line

and this is after running it:
This is the first line
This is the second line
This is my third line
This is the fourth line


You can see on the third line the word "the" changed to "my".

Regards,

Rob.
0
Joachim CarreinSr. Software DeveloperCommented:
i've had the same thing tested succesfully with the previous code (see previous thread)
but apparently there are some strange invisible characters in his text file, which makes it screwed up. you should give it a try with the files he attached in his other question. I'm also still testing with those.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MayoorPatelAuthor Commented:
Rob Sampson can you test with the articles in the files posted in the other question

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_23107331.html

joachimcarrein- Thankyou for your continued efforts! :)
0
Joachim CarreinSr. Software DeveloperCommented:
was this the same problem as the other thread or also the same one?
0
MayoorPatelAuthor Commented:
Solved the problem in another thread therefore points awarded
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.