• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

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

0
MayoorPatel
Asked:
MayoorPatel
  • 3
  • 3
  • 2
  • +1
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now