ADS

Featured

List files and folders in a directory

In cases where you need to dynamically display the contents of a directory, or customize the appearance of an online file directory, but do not have time to add or remove files, this script will automatically display the contents of the directory files for you.

The file will list the directories and folders whenever they are available in the directory.

It can be implemented as an error script for 404, and thus always display the current directory and its files, with a customized layout.

File: listar.asp, copy the code and create the file containing the following code:
<%@language="vbscript" codepage="65001" lcid="1043" %>
<% response.charset = "utf-8" %>
<%
folder403=replace("/"&replace(request.querystring,"403;http://" & request.serverVariables("server_name")&":"&request.serverVariables("Server_port")&"/",""),"/","\")
if folder403 <> "\" then
myfolder = folder403
else
myfolder="."
end if
%>
<h3><%=myfolder%></h3>
<ul>
    <%
        Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
        Set MyFolders=MyDirectory.GetFolder(Server.MapPath(myfolder))
        For each filefound in MyFolders.SubFolders
    %>
    <li>
    <a href="<% =filefound.Name %>" target="blank"><% =filefound.Name %></a>
    </li>

    <% Next %>
</ul>
<ul>
    <%
        Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
        Set MyFiles=MyDirectory.GetFolder(Server.MapPath(myfolder))
        For each filefound in MyFiles.files
    %>
    <li>
    <a href="<% =filefound.Name %>" target="blank"><% =filefound.Name %></a>
    </li>

    <% Next %>
</ul>

Now we are going to use it together with the following Web.Config (available from IIS 7.5 or higher) with custom errors:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="Custom">
            <remove statusCode="403" />
            <error statusCode="403" prefixLanguageFilePath="" path="/teste/listar.asp" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

And now we will have a personalized page to navigate between the files on the server.


1 comment: