Link to home
Start Free TrialLog in
Avatar of Larry Rungren
Larry RungrenFlag for United States of America

asked on

Search & replace link strings in HTML using VB6 and fso

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.

ie:
        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"
ASKER CERTIFIED SOLUTION
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vinnyd79
vinnyd79

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

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

Set MyFile = fso.OpenTextFile("2000371WFM_A.html", 2, True)
MyFile.Write Filedata
MyFile.Close
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