Link to home
Start Free TrialLog in
Avatar of KAbbott
KAbbottFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to display list of files in a directory?

Hi,

My ISP doesn't allow me to browse directorys on the server, but I want visitors to my site to be able to view all files contained with a directory and allow them to download them.

Is there a way (using javascript) of allowing me to do this?  The files will have random names so there will be no way to determine what there called.

Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this using ASP. Run the below code in a web server like IIS.

<%
    Dim objFSO
    Dim objFolder, objFile, objSFolder
    Dim thisFolder
    Dim strFolder
   
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

    thisFolder = "c:\windows\profiles\hongjun\desktop"
    Set objFolder = objFSO.GetFolder(thisFolder)
   
    ' display directory
    Response.Write "<b>Directories</b><BR>"
    For Each strFolder In objFolder.SubFolders
        Response.Write strFolder & "<BR>"
    Next
   
    Response.Write "<BR>"
   
    ' display files
    Response.Write "<b>Files</b><BR>"
    For Each objFile In objFolder.Files
        Response.Write " <a href='" & objFile.Name & "'>" & objFile.Name & "</a>"
        Response.Write "<BR>"
    Next
%>


However, note that it is not possible to download .asp. This is because it will be run on the server before returning you the download. So, you actually download the html source of the asp returned by the server.



hongjun
NOTE!!!! MY CODE ABOVE GOT SOME LOGIC ERROR. Use the below code instead.


<%
    Dim objFSO
    Dim objFolder, objFile, objSFolder
    Dim thisFolder
    Dim strFolder
   
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

    thisFolder = "c:\windows\profiles\hongjun\desktop"
    Set objFolder = objFSO.GetFolder(thisFolder)
   
    ' display directory
    Response.Write "<b>Directories</b><BR>"
    For Each strFolder In objFolder.SubFolders
        Response.Write strFolder & "<BR>"
    Next
   
    Response.Write "<BR>"
   
    ' display files
    Response.Write "<b>Files</b><BR>"
    For Each objFile In objFolder.Files
        Response.Write " <a href='" & thisFolder & "\" & objFile.Name & "'>" & objFile.Name & "</a>"
        Response.Write "<BR>"
    Next
%>


hongjun
Avatar of KAbbott

ASKER

ISP doesn't support ASP,

Is it possible to do it with client-side javascript?
I am reasonably sure that you can not do that as JavaScript fires client-side.

Fritz the Blank
Avatar of KAbbott

ASKER

Dam!

If I gave all the files that where uploaded to the site .html extensions, is there a way I could see them then.

The files that I upload are only text files.
It makes no difference about the extension type. It is an issue of client-side code not being able to list server-side directory items.

Fritz the Blank
Change your web server to one that support asp script or one that actually allows you to do directory listing.

hongjun
or advise your hosting co. to start using Apache. there seems to be a prob with the server and not the isp.
Most ISP's won't allow directory browsing for security reasons. Displaying directory and file information via server-side script is preferable as it allows tighter control over what can and can't be viewed.

Fritz the Blank
well, we have no such isp's in india atleast,

anyway do i owe u some points ? :-)
Why the grade of B? Just because the answer is not what you want to hear, it doesn't make it any less right. Indeed, you could have spent days trying to code a solution that wouldn't work, and my answer (among others here) saved you that aggrevation.

Fritz the Blank