We help IT Professionals succeed at work.

Replacing the First Occurence of a Word in a Text File

MayoorPatel
MayoorPatel asked
on
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

Comment
Watch Question

Top Expert 2007

Commented:
>> 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?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.
Sr. Software Developer
Commented:
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.

Author

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! :)
Joachim CarreinSr. Software Developer

Commented:
was this the same problem as the other thread or also the same one?

Author

Commented:
Solved the problem in another thread therefore points awarded

Explore More ContentExplore courses, solutions, and other research materials related to this topic.