VBS Script to Link Phrases in Text Files with URLs

I need a VBScript that will do the following.

Currently, I'm writing articles for my websites which will link to various other pages, either on my own website or on a product page of another one of my websites.  Each of these articles will be in it's own text file, which will include the title of the article, a keyword phrase and corresponding URL (either one or multiple) and then the body of the article.  It will be in the following format. (This example shows to give keyword phrases, it can be as few as one or an indefinite amount)

This is the title of the article

Keyword: keyword phrase 1
URL: http://www.mysite.com/product1.html
Keyword: keyword phrase 2
URL: http://www.mysite.com/product2.html

This is paragraph one of the article and it may contain keyword phrase 1 or 2

This is paragraph two of the article and it may contain keyword phrase 2 or 1

-----

I would like the script to find the first occurrence of each keyword phrase in the body of the article (ignoring the article title) and then hyperlink it, using the corresponding URL. The final text file should be in the following format.

This is the title of the article

This is paragraph one of the article and it may contain <a href="http://www.mysite.com/product1.html">keyword phrase 1</a> or 2

This is paragraph two of the article and it may contain <a href="http://www.mysite.com/product2.html">keyword phrase 2</a> or 1

-----

It should do this for each text file in the folder where the script resides.

Thank You,
Tim
timshankAsked:
Who is Participating?
 
Shift-3Connect With a Mentor Commented:

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
Set objShell = CreateObject("WScript.Shell")
strCurrentDir = objShell.CurrentDirectory
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strCurrentDir)
 
For Each objFile in objFolder.Files
    strFile = objFile.Name
 
    If LCase(Right(strFile, 4)) = ".txt" Then
        intCount = 0
        Set objKeywords = CreateObject("Scripting.Dictionary")
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
 
        Do Until objTextFile.AtEndOfStream
            strNextLine = objTextFile.Readline
            
            If Left(strNextLine, 4) = "URL:" Then
                strURL = Mid(strNextLine, 6)
                objKeyWords.Add strKW, strURL
                blnSkip = True
            End If
            
            If Left(strNextLine, 8) = "Keyword:" Then
                strKW = Mid(strNextLine, 10)
                blnSkip = True
                intCount = intCount + 1
                
                If intCount = 1 Then
                    strNewFile = Left(strNewFile, Len(strNewFile) - 2)
                End If
            Else
                For Each strKeyword in objKeywords.Keys
                    intPos = InStr(1, strNextLine, strKeyword, 1)
                    If intPos <> 0 Then
                        strOld = Mid(strNextLine, intPos, Len(strKeyword))
                        strNextLine = Replace(strNextLine, strOld, "<a href=" & Chr(34) & _
                            objKeywords.Item(strKeyword) & Chr(34) & ">" & strOld & "</a>", 1, 1)
                        objKeywords.Remove(strKeyword)
                    End If
                Next
            End If
            
            If Not blnSkip Then
                strNewFile = strNewFile & strNextLine & vbCrLf
            End If
            
            blnSkip = False
        Loop
 
        objTextFile.Close
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
 
        objTextFile.WriteLine strNewFile
        objTextFile.Close
        
        strNewFile = ""
        Set objKeywords = nothing
    End If
Next

Open in new window

0
 
Shift-3Commented:
Paste the script below into a text file with a .vbs extension.  Running it will perform the requested substitutions on each .txt file in the same directory.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
Set objShell = CreateObject("WScript.Shell")
strCurrentDir = objShell.CurrentDirectory
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strCurrentDir)
 
For Each objFile in objFolder.Files
    strFile = objFile.Name
    
    If LCase(Right(strFile, 4)) = ".txt" Then
        Set objKeywords = CreateObject("Scripting.Dictionary")
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
 
        Do Until objTextFile.AtEndOfStream
            strNextLine = objTextFile.Readline
            
            If Left(strNextLine, 4) = "URL:" Then
                strURL = Mid(strNextLine, 6)
                objKeyWords.Add strKW, strURL
            End If
            
            If Left(strNextLine, 8) = "Keyword:" Then
                strKW = Mid(strNextLine, 10)
            Else
                For Each strKeyword in objKeywords.Keys
                    If InStr(strNextLine, strKeyword) <> 0 Then
                        strNextLine = Replace(strNextLine, strKeyword, "<a href=" & Chr(34) & _
                            objKeywords.Item(strKeyword) & Chr(34) & ">" & strKeyWord & "</a>")
                        objKeywords.Remove(strKeyword)
                    End If
                Next
            End If
 
            strNewFile = strNewFile & strNextLine & vbCrLf
        Loop
 
        objTextFile.Close
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
 
        objTextFile.WriteLine strNewFile
        objTextFile.Close
        
        strNewFile = ""
        Set objKeywords = nothing
    End If
Next

Open in new window

0
 
timshankAuthor Commented:
Thank you. There are just a couple of things that aren't working for me.

It will hyperlink the phrase, anytime it is included in the article. I only want it to do it on the first occurrence of the phrase.

It still includes the URL: and Keywords: lines in the finished document. To be honest, this isn't that big of a deal to me but I would prefer that they are not there. I'm not sure if that would take extensive coding to remove them.

Is there a way to make it ignore the case of the text? For example, if the keyword is "Dog Training" I don't care if it hyperlinks "dog training" without the capital letters. (or "Dog training" for that matter, just in case I start a sentence with the keyword)

Thank you very much.
Tim
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Shift-3Commented:
So modified.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
Set objShell = CreateObject("WScript.Shell")
strCurrentDir = objShell.CurrentDirectory
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strCurrentDir)
 
For Each objFile in objFolder.Files
    strFile = objFile.Name
    
    If LCase(Right(strFile, 4)) = ".txt" Then
        Set objKeywords = CreateObject("Scripting.Dictionary")
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
 
        Do Until objTextFile.AtEndOfStream
            strNextLine = objTextFile.Readline
            
            If Left(strNextLine, 4) = "URL:" Then
                strURL = Mid(strNextLine, 6)
                objKeyWords.Add strKW, strURL
                blnSkip = True
            End If
            
            If Left(strNextLine, 8) = "Keyword:" Then
                strKW = Mid(strNextLine, 10)
                blnSkip = True
            Else
                For Each strKeyword in objKeywords.Keys
                    intPos = InStr(1, strNextLine, strKeyword, 1)
                    If intPos <> 0 Then
                        strOld = Mid(strNextLine, intPos, Len(strKeyword))
                        strNextLine = Replace(strNextLine, strOld, "<a href=" & Chr(34) & _
                            objKeywords.Item(strKeyword) & Chr(34) & ">" & strOld & "</a>")
                        objKeywords.Remove(strKeyword)
                    End If
                Next
            End If
            
            If Not blnSkip Then
                strNewFile = strNewFile & strNextLine & vbCrLf
            End If
			
            blnSkip = False
        Loop
 
        objTextFile.Close
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
 
        objTextFile.WriteLine strNewFile
        objTextFile.Close
        
        strNewFile = ""
        Set objKeywords = nothing
    End If
Next

Open in new window

0
 
timshankAuthor Commented:
Thank you, it is getting close to what I need. Right now, it is hyperlinking the first occurrence but then also hyperlinking anytime it finds another occurrence that is the same. For example, it will hyperlink KW, skip kw and then hyperlink KW later in the article. Here is the test that I did, just to provide you with how I discovered it.

There are also 2 blank lines between the article title and the article, can one of those be removed?

Thank you,
Tim

This is the original article

Article 1

Keyword: KW 1
URL: 1.html
Keyword: KW 2
URL: 2.html
Keyword: kw 3
URL: 3.html
Keyword: kw 4
URL: 4.html

Cap wrong first - kw 1 - KW 1 - kw 1 - KW 1.

Cap correct first - KW 2 - kw 2 - KW 2 - kw 2

Lowercase wrong first - KW 3 - kw 3 - KW 3 - kw 3.

Lowercase correct first - kw 4 - KW 4 - kw 4 - KW 4

---

This is the finished article

Article 1


Cap wrong first - <a href="1.html">kw 1</a> - KW 1 - <a href="1.html">kw 1</a> - KW 1.

Cap correct first - <a href="2.html">KW 2</a> - kw 2 - <a href="2.html">KW 2</a> - kw 2

Lowercase wrong first - <a href="3.html">KW 3</a> - kw 3 - <a href="3.html">KW 3</a> - kw 3.

Lowercase correct first - <a href="4.html">kw 4</a> - KW 4 - <a href="4.html">kw 4</a> - KW 4
0
 
timshankAuthor Commented:
Excellent Job! Thank you very much for your diligence.
0
All Courses

From novice to tech pro — start learning today.