Solved

VBS Script to Link Phrases in Text Files with URLs

Posted on 2008-10-31
6
259 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:timshank
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22851420
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
 

Author Comment

by:timshank
ID: 22851606
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
 
LVL 38

Expert Comment

by:Shift-3
ID: 22851808
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:timshank
ID: 22852079
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
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 22852680

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
 

Author Closing Comment

by:timshank
ID: 31512034
Excellent Job! Thank you very much for your diligence.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

919 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

16 Experts available now in Live!

Get 1:1 Help Now