0

My txt file (links.txt) contains several links as follows

<A HREF="file1.oft">File 1</A>
<A HREF="file2.oft">File 2</A>
<A HREF="file3.oft">File 3</A>
<A HREF="file4.oft">File 4</A>
<A HREF="file5.oft">File 5</A>

What I need is to open the file and read the value of the links (File 1, File 2, etc)

How can I do that? regex? I imagine this is something simple but I could find exactly what I need to accomplish.

??? strRegX = "<\s*A(.|\n)*?\s*>((.|\n)*?)<\s*\/A\s*>"  ?????

Const ForReading = 1
Set objTextFile = objFSO.OpenTextFile("links.txt", ForReading)
Do Until objTextFile.AtEndOfStream
***** CODE GOES HERE ******
Loop

Thanks in advance!

JustQn4
  • 439
  • 1
  • 6
  • 12
  • Something like this expression should give you groups which you can easily read in your code: `\<[A-Z]+ [A-Za-z]+="([a-zA-Z0-9]+[.]*[a-zA-Z0-9]+)"\>.*` – Harsh Dec 02 '15 at 23:55

1 Answers1

0

I really hate regular expressions. I would do it more "old style":

Option Explicit

Private Sub showLinks()
    Dim content As String
    Dim pos As Integer

    ' read file content
    content = readFile("C:\Temp\links.txt")

    ' search for links
    Do While InStr(content, "<A HREF")
        ' find begin of link
        pos = InStr(content, "<A HREF")
        content = Mid(content, pos + 7)

        ' find closing >
        pos = InStr(content, ">")
        content = Mid(content, pos + 1)

        ' find begin of closing tag
        pos = InStr(content, "<")

        ' print out link text
        Debug.Print Left(content, pos - 1)
    Loop
End Sub

Private Function readFile(ByVal pFile As String) As String
    Dim ret As String
    Dim row As String

    ' create file handle
    Dim hnd As Integer
    hnd = FreeFile

    ' open file
    Open pFile For Input As hnd

    ' read file
    Do Until EOF(hnd)
        Line Input #hnd, row
        ret = ret & row
    Loop

    ' close file
    Close hnd

    ' return content
    readFile = ret
End Function

of course it's a little bit simplified to show the concept. It would need solid error handling for example to ensure that the file handle is released for example. But it should give you only an idea of how it could work.

You can also use the FSO to read the file (or maybe also ADO) ... but I wanted to show you that it is possible without any external library as this often causes problems.

cboden
  • 813
  • 9
  • 14
  • Thanks. I had to adapt it a bit but it works just fine to accomplish what I need. Very much appreciated :-) – JustQn4 Dec 03 '15 at 22:29