Click here to Skip to main content
15,880,956 members
Articles / Web Development / ASP.NET
Article

Auto generate hyperlinks from a text file

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
4 Oct 20044 min read 53K   525   14   6
Autolink automatically generates a text file containing a list of hyperlinks. You provide a list of htm pages and a list of page names as input.

Introduction

Autolink automatically generates a list of hyperlinks. You provide a list of htm pages (text file) and a list of page names (text file) as inputs and autolink creates an output file that contains a list of hyperlinks that you can embed in your source code.

Detail

This is a very very simple utility. It was written in ASP.NET. Its main function is to auto generate a list of hyperlinks (URLs). Recently i came across a simple project which required me to generate a list of urls for approximately 2000 files. Obviously it was a tedious task to do manually and could have wasted a lot of my precious time. I was certain that i would find a utility on the net to accomplish this simple task but to my surprise there was no such utility. After searhing internet comprehensively i found a small application that did this task but guess what, it's price was $395. So there was no solution left but to write a simple utility of my own. Code is very simple. I used streamReader class to read and write to the files. I used two files. One file contained list of pages and the other contained names of files. For example, list1 contains:

this_is_html_file1.htm
this_is_html_file2.htm
this_is_html_file3.htm

list2 contains:

this is html file1
this is html file2
this is html file3

You must be thinking that creating these files is a tedious job in itself. Yes, you are right! Its difficult to create these files manually but there is a solution to every problem and solution to our problem is using a DIR command in DOS mode. For example, folder contains 2000 files in it. How will you generate a list of files from this folder? Simple! Use following command at DOS Prompt:

ASP.NET
Dir >

Execute this command in the folder that contains files. It will generate a list of all files. Use notepad to clean this list. Press Ctrl + H to run find and replace command. Remove dates from the list. Your file should only contain list of html files as shown above. Now, create another file by copying this file and run find and replace command once again. Remove .htm extension from each file. Now  you have two files. One contains list of html files and the other contains list of names of those html files. Edit code and use your own paths. Locate ::Dim fylename As String = "C:\temp\list.txt":: in the code, this is your first file that contains list of html pages. Then locate ::Dim fylename2 As String = "C:\temp\list2.txt"::, this is your second file that contains list of names. Locate ::oWriter = oFile.CreateText("C:\temp\finallist.txt")::, this is your output file. Apply formatting to the contents of file1 and file2 and write results into file3. Later copy contents of file3 into your htm page. That's it!  This utility is very helpful if you want to generate a list of urls for more than 100 files. I tried this utility and created a list of urls for 2000 files. It saved me lot of time. I know it should have contained a way to get a list of pages automatically so that we dont have to clean the list using notepad. May be if i get another project of creating a URL list for 10000 pages, i will add that functionality as well. You can modify the code and add that functionality yourself. Use Regular expressions to format the text file. Regexlib.com is a very helpful site and contains hundreds of pre-built regular expressions. You can download required regular expressions from that site. Lets briefly look at the code:

oStreamReader = File.OpenText(fylename)

This line instantiates ostreamreader object, fylename contains name of the file to be read.

oWriter = oFile.CreateText("C:\temp\finallist.txt")

This line instantiates owriter object, Use createtext method of file object to create a file.

Do
    Line = oStreamReader.ReadLine
    Line2 = ostreamreader2.ReadLine
    newLine = "<font face='Verdana,Helvetica' size='2'><a href='" & Line & _
     "'>" & Line2 & "</a></font><br>"
    oWriter.WriteLine(newLine)
Loop Until Line Is Nothing

In this loop you read contents of both files line by line, apply formatting, create hyperlinks and write the result to the third file. User ReadLine method of streamreader class to read each line from the file. Use writeline method of streamwriter class to write each line to a new file.

'Destroy objects
oStreamReader.Close()
oStreamReader = Nothing
ostreamreader2.Close()
ostreamreader2 = Nothing
oWriter.Close()
oWriter = Nothing


In the end, destroy all objects. Its a good practice to manually destroy all objects in your code.

Happy programming!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
S.S. Ahmed is a senior software engineer and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic web sites. He has been creating customized SharePoint solutions for the last five years. Ahmed has worked in Visual Basic, Visual C, Java, ASP, .NET, SharePoint, InfoPath, BizTalk, etc. Ahmed enjoys travelling and has been to many parts of the world. Ahmed distributes most of his work freely to many internet sites. Ahmed also participates in different newsgroups and tries to help people out with their problems. Ahmed can be reached at share.point@yahoo.com

PEACE

S.S. Ahmed
Web: www.walisystems.com
Blog:www.sharepointblogs.com/ssa

Comments and Discussions

 
QuestionExe Pin
Member 113586117-Jan-15 1:59
Member 113586117-Jan-15 1:59 
Questioncomplete novice needs help Pin
geniekids15-Aug-06 13:56
geniekids15-Aug-06 13:56 
GeneralRe: complete novice needs help Pin
dfgdsg14-Mar-08 8:26
dfgdsg14-Mar-08 8:26 
Generaldos dir command Pin
Anonymous4-Oct-04 10:28
Anonymous4-Oct-04 10:28 
GeneralRe: dos dir command Pin
S.S. Ahmed4-Oct-04 19:49
S.S. Ahmed4-Oct-04 19:49 
GeneralRe: dos dir command Pin
BloodBaz14-Oct-04 21:57
BloodBaz14-Oct-04 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.