Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2002
    Posts
    74

    Unanswered: VB Script to find and replace text in a file

    Hi folks!

    I have the following script that does the find and replace and create a new file with the updated data. My question is - how can I make the find and replace occur only ONCE, since there is only one occurance of the tag that I need to fix. The reason being, the loop is going through file and is taking a long time - it really needs to read the first line only. Any ideas how I can modify this script?

    *************************
    dim fso, readfile, contents, writefile, work
    dim FullFileName, ParentDirName, Filename, NewFilename
    set fso = CreateObject("Scripting.FileSystemObject")
    FullFileName="c:\testParulRoot.xml"
    NewFilename="c:\testParulRootNew.xml"
    set readfile = fso.OpenTextFile(FullFileName, 1, false)
    set writefile = fso.CreateTextFile(NewFileName, 2)
    Do until readfile.AtEndOfStream = True
    contents = readfile.ReadLine
    contents = Replace(contents, "/><DistrictData", " ?> <DistrictData")
    writefile.writeLine contents
    loop
    readfile.close
    writefile.close
    set fso=nothing
    *************************

    Thanks for your help!

    -Parul

  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    replace has three optional parameters that you're not using.

    Code:
    str = Replace(strSource, strTarget, strReplaceWith, [Start], [Count], [Compare])
    • Start is the optional starting position - it defaults to 1
    • Count is the optional number of times to replace a target string with the replacement string. The default is -1, which represents all occurrances.
    • Compare is the vb string compare method.
    Obviously, just use Replace with a Count = 1.
    Code:
    str = Replace(strSource, strTarget, strReplaceWith,, 1,)
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  3. #3
    Join Date
    Oct 2004
    Posts
    26
    Parul, is there any reason you are not choosing "If" condition in this case?
    It looks like you want to end the loop once you make a replacement. Your script is so time consuming because of this statement: Do until readfile.AtEndOfStream = True

  4. #4
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    Quote Originally Posted by Dniz
    Parul, is there any reason you are not choosing "If" condition in this case?
    It looks like you want to end the loop once you make a replacement. Your script is so time consuming because of this statement: Do until readfile.AtEndOfStream = True
    He has to continue, in order to finish copying the file.

    However, I have another question that I'm unclear about.

    You say you wish to only replace the first instance of "/><DistrictData". Are you discussing the first instance in ANY given line of the file, or ONLY the first instance in the entire file?

    If you only need to replace the first instance in the entire file, your code will need some additional modification.

    First, add a boolean variable so that you know if you've found the first match.

    Then, wrap your existing replace line with an IF/Then code block.
    Code:
    Dim blFound
    blFound = False  ' (Maybe redundant in VBA, but, just to be sure...)
    ...
    contents = readfile.ReadLine    ' Existing code
    
    If NOT blFound then
      If Instr(contents, "/><DistrictData") > 0 Then 
        contents = Replace(contents, "/><DistrictData", " ?> <DistrictData",,1)    ' Existing code
        blFound = True  ' Set flag so we won't even check after this
      End If
    End If
    
    writefile.writeLine contents    ' Existing code
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  5. #5
    Join Date
    Oct 2004
    Posts
    26
    Yes, loquin, you have written out my point in your code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •