Arron
Arron

Reputation: 25

Pure ASP hyperlink for all files in a folder

I recently had a project and couldn't find simple code anywhere.. hope this helps someone! Make sure to set NTFS permissions for the IIS app account on the folder. Use file:// for files http:// for a direct and others for relative links.

<%
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("C:\Path")

for each x in fo.files
Response.Write("<a href=file:///" &x & ">" & x & "</a>" & "</br>")
next

set fo=nothing
set fs=nothing
%> 

Upvotes: 1

Views: 731

Answers (2)

tisaconundrum
tisaconundrum

Reputation: 2292

Offering another solution in regards to this.
For situations where someone may be sort of bound to explicit location of their files but do not want to deal with the website not linking correctly.

Essentially, we iterate through all the files in a given directory and then we chop off the complete directory using GetFileName(). We then utilize the root / in the href to then point to the files that we want. From there the user can then download what they need.

<%
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("C:\Folder0\Folder1\Folder2\main\reports\archive")

for each x in fo.files
        x = fs.GetFileName(x)
        Response.Write("<a href=/main/reports/archive/" & x & " target=_blank>" & x & "</a>" & "</br></br>")
next

set fo=nothing
set fs=nothing
%>

Upvotes: 0

Wayferer
Wayferer

Reputation: 109

I found your post and it was really useful to me. I changed it up a bit and wanted to post it here in case others find this useful too. Due to a difference in the way I have IIS set up locally and how it is set up on the server I have to un/comment some lines when uploading to the server vs running locally. Please bear in mind that I'm a front end guy and this is the first time I have written any code like this so please give feedback :)

<%
    'Dim previewURL As String = "http://XXXXXX/"'Preview
    Dim previewURL As String = ""'Local

    'Dim site As String = "XXXXX"'Preview
    Dim site As String = ""'Local

    Dim currentDir As String = HttpContext.Current.Request.PhysicalApplicationPath.ToString()+site+"\web"

    Dim di As New IO.DirectoryInfo(currentDir)

    Dim diar1 As IO.FileInfo() = di.GetFiles("*.html")', IO.SearchOption.AllDirectories)'change this line if you want sub directories as well

    Dim dra As IO.FileInfo

    Response.Write("<h1>HTML Pages</h1>")
    Response.Write("<ul>")

    'list the names of all files in the specified directory
    For Each dra In diar1
        Response.Write("<li><a href="+previewURL+site+"/web/"+dra.Name+" target=_blank>"+dra.Name+"</a></li>")
    Next

    Response.Write("</ul>")
%>

Upvotes: 1

Related Questions