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.

        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"
Who is Participating?
gecko_au2003Connect With a Mentor Commented:
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 ?
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
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.