Search & replace link strings in HTML using VB6 and fso

Posted on 2005-04-21
Last Modified: 2008-03-03
I have a directory full of .HTML files that have been renamed.  I have already used fso to read from one directory and according to data in a SQL database copy the file to another directory with no problem.  Now I need some help in pasing the files in the destination directory and replacing part of a link definition.

        File name is 2000371WFM_A.html

        Contains 1 or more links that are coded as "Data/20005212040B.html"

        Need to replace with "2000371WFM_B.html"
Question by:Jeff_Kingston
    LVL 23

    Accepted Solution

    From your example how exactly do you intend on replacing the link defintion ? Are you going to take the file name "2000371WFM_A.html" and the coded link ""Data/20005212040B.html" to ascertain this "2000371WFM_B.html"

    ie you take the first 11 chars of the file name ie "2000371WFM_" and store that in one variable ( whether it is an array that is used to store different file names or just one variable ) then truncate the "Data/" and the next 11 digits off of the link(s) that are coded to leave you with B.html ( hence you can just use the left function to get the letter B)

    then from there you can concatenate the variable ( which holds the first part of the file name ) with the letter B and also concatenate that with the file type ( which in this example is .html)

    Is that what you are trying to get out of this as an answer or am I looking at this wrong ?
    LVL 28

    Expert Comment

    Dim fso, MyFile, Filedata
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.OpenTextFile("2000371WFM_A.html")
    Filedata = MyFile.ReadAll

    Filedata = Replace(Filedata, "Data/20005212040B.html", "2000371WFM_B.html")

    Set MyFile = fso.OpenTextFile("2000371WFM_A.html", 2, True)
    MyFile.Write Filedata
    LVL 12

    Expert Comment

    Open the file and assign the content of the html file to a string HTML_Str
        Dim s2 As String
        s2 = Replace(HTML_Str, "Data/20005212040B.html", "2000371WFM_B.html", , , vbTextCompare)

    This will replace all Data/20005212040B.html with 2000371WFM_B.html and it will return s2 containing the new HTML with the changes

    The trouble will be that it is always going to be Data/20005212040B.html

    One workaround to that will be to read the file a line at a time with fso then use instr function to determine if the line contains the string "Data/" when you find it then use Right function starting from the position the Instr function returned up to length of 22 (which the length of your file name to replace)  This will give you the file name then use the replace function as above fiving the file name in the second parameter

    Need more help with this let me know


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now