Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for FSo script that will display folder contents 2

Status
Not open for further replies.
Jul 13, 2001
180
US
Hello,

I am looking for a script that i can put on a asp page that will show all contents of a folder as links.
I made a folder for a client on their website where they can upload their word and pdf documents.

I would like it so that when I call a particular page, all contents of that folder will show up as links.

Thanks in advance!
 
I love that google power

Lots of hits there

___________________________________________________________________

The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page faq333-3811
 
as for the links section. (sorry failed to read that up very well)

Just output the File selected through the looping structure incapsed in a link

something like

Code:
For Each File In myFolders.Files
	Response.Write "<a href='" & Server.MapPath(File.Name) & "'> " & File.Name & "</a>"
Next


___________________________________________________________________

The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page faq333-3811
 
This should pretty much work out of the box. All you need to do is change the value of the folderspec variable to the folder that you want and format to suit your tastes.
Code:
<%
newpath = request.querystring("newpath")
folderspec = "ChangeThisToYourFolderNameAndLeaveTheBackslash/"

if  newpath<>"" then 
    folderspec = folderspec & newpath & "/"
end if

Set fs = CreateObject("Scripting.FileSystemObject")

Set folder = fs.GetFolder(server.mappath(folderspec))
Set subfolder = folder.subfolders

response.write "<h4>Folders</h4>"

If newpath<>"" then
	Pointer = instrrev(newpath,"/")

    If Pointer>0 then
        UpOneLevel = Left(newpath,Pointer-1)
        response.write "<a href='" & request.servervariables("script_name")
        response.write "?newpath=" & UpOneLevel & "'>Up One Level</a><br>"

    Else
        response.write "<a href='" & request.servervariables("script_name")
        response.write "'>Up One Level</a><br>"
    End If
End If

For Each newfolder in subfolder
    response.write "<A HREF='"
    response.write request.servervariables("script_name")

    If newpath="" then
        response.write "?newpath=" & newpath & newfolder.name
    Else
        response.write "?newpath=" & newpath & "/" & newfolder.name
    End If
    response.write "'>"
    response.write newfolder.name & "<br>"
    response.write "</A>"
Next

response.write "</blockquote><hr>"
response.write "<h4>Files</h4><blockquote>"
Set filelist = folder.files

' iterate through the files and make them links
For Each file in filelist
    response.write "<A HREF='"
    response.write folderspec & file.name
    response.write "'>"
    response.write file.name
    response.write "</A><br>"
Next

set folder=nothing
set subfolder=nothing
set filelist=nothing
set fs=nothing

response.write "</blockquote><hr>"
%>
 
Thank you kindly to both!!
I will implement both approached tomorrow at work.

Cheers!!! :)
 
Nice, one of the cleaner inplementations I have seen of that level of listing also,

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Second that!


___________________________________________________________________

The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page faq333-3811
 
Veep, I was wondering how could I add a breadcrumb on top of the page along with the up one level ?

Again, awesome, awesome script!

I am truly indebted!
Thank you

Cheers,

Manny
 
You could add something simple like this to display where you are:

(untested code)
whereAmI = request.ServerVariables("PATH_INFO")
myPos = inStrRev(whereAmI,"/")
strippedWhereAmI=mid(whereAmI,2,myPos-1)
whereAmI = strippedWhereAmI & newPath
whereAmI = "You are here: " & replace(whereAmI,"/"," > ")
response.Write(whereAmI & "<br>")
 
Thank you Veep, I was able to do just that already with this code:
newpath_a=replace(newpath,"/"," > ")
response.write "<span class='header'>"+newpath_a+"</span><br>"

But what I can;t figure out is how to make each part of those clickable links to go to it's respective folders.

ex. C>B>A

Clicking on C will bring you to C, etc.

This possible?

Thanks in advance.
 
here is a recursive version that displays all Children on the same page. I just whipped this up, so you would want to make changes to it, since the file list could get pretty long.
Code:
<%

	Class cDirectoryList
	
		Private oFS
		Private m_sRootpath
		Private m_oRootFolder
		Private b_Initialized
		Private aFolderArray() ' Array of SubFolder Paths
		Private sWorkingPath
		Private bTraverseChildren 'Not used in Current version
		
		'======================================
		' Class Properties
		'======================================
		Public Property Get sRootPath()
			sRootpath = m_sRootpath
		End Property
		
		Public Property Let sRootPath(sInput)
	  		m_sRootpath = sInput
		End Property
		
		
		Private Property Set oRootFolder(oInput)
	  		Set m_oRootFolder = oInput
		End Property

		
		Private Property Get oRootFolder()
			Set oRootFolder = m_oRootFolder
		End Property
		
				
		
		
		'============================
		' Initialization
		'============================
		Private Sub Class_Initialize()
			Set oFS = Server.CreateObject("Scripting.FileSystemObject")
			b_Initialized = False
			bTraverseChildren = True
			ReDim Preserve aFolderArray(0)
			sWorkingPath = Left(Request.ServerVariables("PATH_TRANSLATED"), InStrRev(Request.ServerVariables("PATH_TRANSLATED"), "\"))
		End Sub
		
		'==========================
		' Cleanup
		'==========================
		Private Sub Class_Terminate()
			If isObject(oFS) Then Set oFS = Nothing
		End Sub
		
		Private Sub Init()
			If b_Initialized = False Then
				Set oRootFolder = oFS.GetFolder(Server.mapPath(sRootPath))
				b_Initialized = True
			End If
		End Sub
		
		
		'===========================
		' Class Members
		'===========================
		
		
		Sub GetSubFolders(oParent)
			Call Init()
			'On Error Resume Next
			
			If oParent = "" Then 
				Set oParent = oRootFolder
			End IF
			
			Dim oSubFolder
			
			If sRootPath = "" Then
				Echo("ERROR: Specify a root folder.")
				Response.End()
			End If
			
			
			Dim iSubFolderCount
			Dim iCurrentIndex
			Dim iIterator
			
			'=======================
			' Update Array bounds
			'=======================
			iCurrentIndex = UBound(aFolderArray)
			iSubFolderCount = (oParent.SubFolders.Count + iCurrentIndex)
			
			ReDim Preserve aFolderArray(iSubFolderCount)
			
			IF oParent.Path = oRootFolder.Path Then
				aFolderArray(iCurrentIndex) = oParent.Path
			End IF
			
			For Each oSubFolder In oParent.SubFolders
				iCurrentIndex = (iCurrentIndex + 1)
				aFolderArray(iCurrentIndex) = oSubFolder.Path
				
				If oSubFolder.SubFolders.Count > 0 Then
					Call GetSubFolders(oSubFolder)
				End If
			Next
			
		End Sub
		
		Private Sub GetFiles(sPath)
		
			Dim oFolder, oFile
			Dim iIterator
			For iIterator = 0 To UBound(aFolderArray)
				Set oFolder = oFS.GetFolder(aFolderArray(iIterator))
				
				Echo("<B>" & NormalizePath(aFolderArray(iIterator)) & "</B><BR>")
				
				For Each oFile In oFolder.Files
					Echo("&nbsp;&nbsp;&nbsp;&nbsp; <a href=" & Chr(34) & NormalizePath(aFolderArray(iIterator)) & "/" & oFile.Name & Chr(34) & ">" _
						& oFile.name & "</a><BR>")
				Next
				
				
			Next
		End Sub
		
		
		
		Public Sub DisplayFolders()
			Dim x
			For x = 0 To UBound(aFolderArray)
				
				GetFiles(aFolderArray(x))
			
			Next
			

		End Sub
		
		Private Function NormalizePath(sPath)
			NormalizePath = Replace(Mid(sPath, Len(sWorkingPath), Len(sPath)), "\", "/")
		End Function
		
		
		
		Private Sub Echo(sInput)
			Response.Write(sInput & vbCrLf)
		End Sub
		
		
		
	End Class
	
	
	
	Dim oDirLister
		Set oDirLister = New cDirectoryList
		oDirLister.sRootPath = "\"	'Virtual Root
		oDirLister.GetSubFolders ""
		oDirLister.DisplayFolders
	Set oDorLister = nothing

%>
 
whoops.

The last line should read:
Set oDirLister = nothing

There wasn't anything wrong with the original solution, I am just not a very big fan of procedural coding style.

- Jason
 
Thank you Jason, but not being an expert in asp, I cannot get it to work with the code Veep gave me. But thank you just the same.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top