Microsoft KB Archive/224364

= Creating a directory browsing page using ASP =

Article ID: 224364

Article Last Modified on 8/8/2007

-

APPLIES TO


 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q224364



We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

http://www.microsoft.com/technet/security/prodtech/IIS.mspx



SUMMARY
Microsoft IIS provides file and folder listings through the Directory Browsing access control setting, which can be set at the folder or Web level. Using the file system objects for Active Server Pages (ASP), you can create pages that function like directory browsing without having to enable directory-browsing access, and allow a greater level of personalization and customization.



MORE INFORMATION
Save the following ASP code as "Default.asp" (without the quotation marks) in folders where you require directory listings: WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. <%@LANGUAGE="VBSCRIPT"%> <%  Option Explicit On Error Resume Next

' this section is optional - it just denies anonymous access If Request.ServerVariables("LOGON_USER")="" Then Response.Status = "401 Access Denied" End If

' declare variables Dim objFSO, objFolder Dim objCollection, objItem

Dim strPhysicalPath, strTitle, strServerName Dim strPath, strTemp Dim strName, strFile, strExt, strAttr Dim intSizeB, intSizeK, intAttr, dtmDate

' declare constants Const vbReadOnly = 1 Const vbHidden = 2 Const vbSystem = 4 Const vbVolume = 8 Const vbDirectory = 16 Const vbArchive = 32 Const vbAlias = 64 Const vbCompressed = 128

' don't cache the page Response.AddHeader "Pragma", "No-Cache" Response.CacheControl = "Private"

' get the current folder URL path strTemp = Mid(Request.ServerVariables("URL"),2) strPath = ""

Do While Instr(strTemp,"/") strPath = strPath & Left(strTemp,Instr(strTemp,"/")) strTemp = Mid(strTemp,Instr(strTemp,"/")+1) Loop

strPath = "/" & strPath

' build the page title strServerName = UCase(Request.ServerVariables("SERVER_NAME")) strTitle = "Contents of the " & strPath & " folder"

' create the file system objects strPhysicalPath = Server.MapPath(strPath) Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strPhysicalPath) %> <%=strServerName%> - <%=strTitle%>  BODY { BACKGROUND: #cccccc; COLOR: #000000; FONT-FAMILY: Arial; FONT-SIZE: 10pt; } TABLE { BACKGROUND: #000000; COLOR: #ffffff; } TH   { BACKGROUND: #0000ff; COLOR: #ffffff; } TD   { BACKGROUND: #ffffff; COLOR: #000000; } TT   { FONT-FAMILY: Courier; FONT-SIZE: 11pt; }

<%=strServerName%> <%=strTitle%> Please choose a file/folder to view.

<%  Set objFSO = Nothing Set objFolder = Nothing

' this adds the IIf function to VBScript Function IIf(i,j,k) If i Then IIf = j Else IIf = k  End Function

' this function creates a string from the file atttributes Function MakeAttr(intAttr) MakeAttr = MakeAttr & IIf(intAttr And vbArchive,"A","-") MakeAttr = MakeAttr & IIf(intAttr And vbSystem,"S","-") MakeAttr = MakeAttr & IIf(intAttr And vbHidden,"H","-") MakeAttr = MakeAttr & IIf(intAttr And vbReadOnly,"R","-") End Function %> Because this page functions in place of directory browsing access, you should disable directory browsing for any folder where this page is saved. To do this, use the following methods.

Method 1: Windows NT Server 4.0

 * 1) Click Start, click Run, type Inetmgr, and then click OK.
 * 2) Double-click the server that is running the WWW service.
 * 3) Click the Directories tab, click the folder that you want, and then click to clear the Directory Browsing Allowed check box.
 * 4) Click OK.

Method 2: Windows 2000 Server

 * 1) Click Start, click Run, type Inetmgr, and then click OK.
 * 2) Double-click the server that you want. Expand the Web site that you want, if it is required.
 * 3) Right-click the Web site or the virtual directory that you want, and then click Properties.
 * 4) Click the Home Directory tab, and then click to clear the Directory browsing check box.

Note The Home Directory tab may appear as the Directory tab or the Virtual Directory tab.
 * 1) Click 'OK'.

For more information about Microsoft scripting languages, view the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms950396.aspx

Additional query words: asp vbscript

Keywords: kbinfo KB224364

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.