ASP Read TXT File Content to Hyperlink

Here's the situation...

\wwwroot
\wwwroot\news

From the wwwroot, I need a dynamic ASP script that will auto hyperlink to files in the news folder.  There will be a txt file in the news folder that will have the hyperlink txt.  It is best explained by example:

\news\news1.htm
\news\news1.txt
\news\news2.doc
\news\news2.txt

I need to hyperlink to news1.htm.  The hyperlink text needs to be the contents of news1.txt.  I also need to hyperlink to news2.doc.  The hyperlilnk text needs to be the contents of news2.txt.

If news1.txt contains the text "Read The first news Item", they hyperlink to news1.htm will look like
<a href="news1.htm">Read The first news Item</a>

If any further questions are needed, let me know.

Thanks
LVL 8
deaditeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darkeryuCommented:
Hi:

can you ajust the file name to
\news\news1.htm
\news\news1.txt (first line to news1.htm,second line to news1.doc)
\news\news1.doc

if you can do this,using the following script:
creating a asp file place in wwwroot
<%
    set fso=server.CreateObject("Scripting.FileSystemObject")
      rpath=server.MapPath("/news")
    set flist=fso.getfolder(rpath)
    For each objfile in flist.files
      if (instr(1,ucase(objfile.name),"HTM")>0) then
        set txt=fso.openTextfile(rpath & "\"  &  mid(objfile.name,1,len(objfile.name)-4) & ".txt",1,false )
        titlehtmtxt=txt.readline
        titledoctxt=txt.readline
%>
       <a href="<%=objfile.name%>">titlehtmtxt</a><br>
       <a href="<%=mid(objfile.name,1,len(objfile.name)-4)&".doc"%>">titledoctxt</a><br>
<%
      end if

    next
    set flist=nothing
    set fso=nothing
%>
thanks
kiddangerCommented:
You don't seem to think this is a difficult script to create.

Will there ever be any .htm, .doc and .txt files all associated with the same number or will there always be just one relation, .htm/.txt or .doc/.txt?

Your example is not accurate.  It should be similar to:
<a href="/news/news1.htm">Read The first news Item</a>

...because you wouldn't want to store this ASP file in the news folder.  This way it will not matter where it is stored.
deaditeAuthor Commented:
hey guys,

sorry for the delay.  I am trying to get darkeryu's script working.  

Kiddanger:  For every file in the /news folder, there will be a .txt file with the same name.  If you have news1.doc there will be a news1.txt, news1.htm there will be a news1.txt, news2.html there will be a news2.txt  and so forth.  You are correct, the asp file will be one directory higher than the news folder, which is where it will read the news items from.  Sorry for the confusion

Do you need any more details or further explanation?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

deaditeAuthor Commented:
wwwroot\news.asp (this is where the script runs from)

wwwroot\news\news1.htm
wwwroot\news\news1.txt
wwwroot\news\news2.doc
wwwroot\news\news2.txt

The hyperlinks in wwwroot\news.asp will look like:
<a href="/news/news1.htm">Read The first news Item</a>

Darkeryu, the files can change the type, and I would like to keep a seperate .txt file for each file such as
news1.htm
news1.txt
news2.htm
news2.txt
news3.asp
news3.txt
news4.doc
news4.txt  and so forth...

If you help me modify your script to do t hat, I'll give you th epoints, and add some to it

Thanks
deaditeAuthor Commented:
Darkeryu, One more thing I should add.. I would like the hyperlinks to be dynamic in the sense I'll never have to edit the wwwroot\news.asp web page.  All I will need to do is drop a file and txt file in the news folder, and the news.asp page will add it.
kiddangerCommented:
test page: http://kiddanger.com/lab/ee/q_21482943.asp

<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = True

sub prt(str)
  Response.Write str & vbCrLf
end sub

sub lprt(str)
  Response.Write str & "<br />" & vbCrLf
end sub

function readfile(file)
  Const ForReading = 1
  dim fso, f, text
  set fso = CreateObject("Scripting.FileSystemObject")
  if fso.FileExists(strPath & "\" & file) then
    set f = fso.OpenTextFile(strPath & "\" & file,ForReading)
    text = f.ReadAll
    set f = nothing
  else
    lprt "File " & file & " does not exist."
  end if
  set fso = nothing
  readfile = text
end function

function filelist(path)
  dim fso, folder
  set fso = CreateObject("Scripting.FileSystemObject")
  set folder = fso.GetFolder(path)
  set filelist = folder.files
  set folder = nothing
  set fso = nothing
end function

function buildLinks(list)
  dim d, i, name, n, ext, fso, str, txt
  set d = CreateObject("Scripting.Dictionary")
  set fso = CreateObject("Scripting.FileSystemObject")
  for each name in list
    ext = fso.GetExtensionName(name)
    if ext = "htm" or ext = "doc" then
      str = virPath & "/" & fso.GetFileName(name)
    else
      n = fso.GetFileName(name)
      txt = readfile(n)
      d.Add str, txt
    end if
  next
  set buildLinks = d
  set fso = nothing
  set d = nothing
end function

dim strPath, fileCollection, links, virPath, j, k
virPath = "/lab/ee/news"
strPath = server.MapPath(virPath)
set fileCollection = filelist(strPath)
set links = buildLinks(fileCollection)
k = links.Keys
prt "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"
prt "<html>"
prt "<body>"
for j = 0 to links.count - 1
  lprt "<a href=""" & k(j) & """>" & links.item(k(j)) & "</a>"
next
prt "</body>"
prt "</html>"
%>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deaditeAuthor Commented:
Hey Guys,
I worked on the scripts and got what I needed on my own tonight.  
Kiddanger, sorry you were a bit too late...  
I'll split the points between you two... Here is the code I used:

<%

 ' ****************************************************************
 ' Code to Auto Hyperlink Files In The News Directory
 ' ****************************************************************

      ' You may first create the FileSystemObject.
      Dim Newsfso
      Set Newsfso = Server.CreateObject("Scripting.FileSystemObject")


      ' Set the reference of the news folder in the current folder
      ' using FileSystemObject's GetFolder method.
      ' Examples: "/" is wwwroot "/subfolder is a subfolder in wwwroot
        '           "./" is current folder and "./subfolder" is a subfolder in current folder

      Dim NewsFolder,NewsList
      set NewsFolder = Newsfso.GetFolder(Server.MapPath("./news"))
      set NewsList = NewsFolder.Files

      ' This Part hyperlinks the news items
      For Each FileIndex in NewsList
        'This Explicitly Excludes Hyperlinking Certain File Extensions
        if Lcase(right(FileIndex.Name, 4)) = ".txt" then
      ' Do Nothing
      ' This Hyperlinks All Other Files
        else
          Response.Write("<ul id='news'>")
          Response.Write("<li>")
          Response.Write("<h4><a href='" & NewsFolder.Name &"/"& FileIndex.Name & "'>")
          Response.write(Left(FileIndex.Name, (InStrRev(FileIndex.name,".") - 1)))
          Response.write("</a></h4>")
          ' Append the Current File Name and Append .txt Extension
          ' Open the FileName.txt and Read and Write the Contents
          Dim Desc
          Desc = (Left(FileIndex.name, (InStrRev(FileIndex.name,".") - 1)) & ".txt")
          Set f = Newsfso.OpenTextFile(server.MapPath(NewsFolder.Name & "\" & Desc), 1)
          Response.Write("<p>")
          Response.Write(f.ReadAll)
          f.Close
          Response.Write("</p>")
          Response.Write("<a href='" & NewsFolder.Name &"/"& FileIndex.name & "' class='more' title='Continue reading News Item'> View ")
          Response.write(Left(FileIndex.name, (InStrRev(FileIndex.name,".") - 1)))
          Response.write("</a>")
          Response.Write("</li>")
          Response.Write("</ul>")
        end if
      Next

      ' Sets Objects to Nothing
      Set Newsfso = nothing
      Set NewsFolder = nothing
      Set NewsList = nothing
      Set f = nothing
 %>
kiddangerCommented:
Actually I had it written before my first post, 3 days ago.  I was just looking for clarification before posting.
One other small item.  Your code creates two links for each item and it doesn't satisfy the original request.  It does not put the content of the txt file as the hyperlink text.

http://kiddanger.com/lab/ee/q_21482943deadite.asp

However, if you change it to this:

<%@ Language=VBScript %>
<%

 ' ****************************************************************
 ' Code to Auto Hyperlink Files In The News Directory
 ' ****************************************************************

     ' You may first create the FileSystemObject.
     Dim Newsfso
     Set Newsfso = Server.CreateObject("Scripting.FileSystemObject")


     ' Set the reference of the news folder in the current folder
     ' using FileSystemObject's GetFolder method.
     ' Examples: "/" is wwwroot "/subfolder is a subfolder in wwwroot
        '           "./" is current folder and "./subfolder" is a subfolder in current folder

     Dim NewsFolder,NewsList
     set NewsFolder = Newsfso.GetFolder(Server.MapPath("/lab/ee/news"))
     set NewsList = NewsFolder.Files

     ' This Part hyperlinks the news items
     For Each FileIndex in NewsList
        'This Explicitly Excludes Hyperlinking Certain File Extensions
       if Lcase(right(FileIndex.Name, 4)) = ".txt" then
     ' Do Nothing
     ' This Hyperlinks All Other Files
       else
         Response.Write("<ul id='news'>")
         Response.Write("<li>")
         Response.Write("<h4><a href='" & NewsFolder.Name &"/"& FileIndex.Name & "'>")
         ' Append the Current File Name and Append .txt Extension
         ' Open the FileName.txt and Read and Write the Contents
         Dim Desc
         Desc = (Left(FileIndex.name, (InStrRev(FileIndex.name,".") - 1)) & ".txt")
         Set f = Newsfso.OpenTextFile(server.MapPath(NewsFolder.Name & "\" & Desc), 1)
         Response.Write(f.ReadAll)
         f.Close
         Response.write("</a></h4>")
         Response.Write("</li>")
         Response.Write("</ul>")
       end if
     Next

     ' Sets Objects to Nothing
     Set Newsfso = nothing
     Set NewsFolder = nothing
     Set NewsList = nothing
     Set f = nothing
 %>

It works as you requested.

http://kiddanger.com/lab/ee/q_21482943deaditemod.asp

Cheers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.