Jump to content

Using FileSystemObject for listing available subfolders in a directory

Recommended Posts

   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)      

   strPath = "/" & strPath

   ' build the page title
   ' strServerName = UCase(Request.ServerVariables("SERVER_NAME"))
   strTitle = "sitelist"

   ' create the file system objects
   strPhysicalPath = Server.MapPath(strPath)
   Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
   Set objFolder = objFSO.GetFolder(strPhysicalPath)
<title>The list of websites available</title>
<base target="blank">
BODY  {background: hsl(24, 100%, 70%);  }
a {text-decoration:none;}
a:hover {color:darkred;font-weight:600;}
a:active {text-decoration:none;}
<body class="w3-container">
<h2 align="center"><%=strTitle%></h2>
<div align="center"><center>

<table border="0" cellspacing="0" cellpadding="2" style="BACKGROUND: #000000; width:70%">
   <th align="left">Name</th>
   <th align="left">Bytes</th>
   <th align="left">KB</th>
   <th align="left">Attributes</th>
   <th align="left">Ext</th>
   <th align="left">Type</th>
   <th align="left">Date</th>
   <th align="left">Time</th>
   ' output the folder list

   Set objCollection = objFolder.SubFolders

   For Each objItem in objCollection
      strName = objItem.Name
      strAttr = MakeAttr(objItem.Attributes)      
      dtmDate = CDate(objItem.DateLastModified)
   <td align="center" style="background: hsl(60, 100%, 75%);COLOR: #000000;font-family:courier;font-size:20px;"><a class="link hvr-underline-from-center" href="<%=strName%>"><%=strName%></a></td>
   <td align="right">N/A</td>
   <td align="right">N/A</td>
   <td align="left"><tt><%=strAttr%></tt></td>
   <td align="left"><b><DIR></b></td>
   <td align="left"><b>Directory</b></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
<% Next %>
   ' output the file list

   Set objCollection = objFolder.Files

   For Each objItem in objCollection
      strName = objItem.Name
      strFile = Server.HTMLEncode(Lcase(strName))

      intSizeB = objItem.Size
      intSizeK = Int((intSizeB/1024) + .5)
      If intSizeK = 0 Then intSizeK = 1

      strAttr = MakeAttr(objItem.Attributes)
      strName = Ucase(objItem.ShortName)
      If Instr(strName,".") Then strExt = Right(strName,Len(strName)-Instr(strName,".")) Else strExt = ""
      dtmDate = CDate(objItem.DateLastModified)
   <td align="left"><a href="<%=strFile%>"><%=strFile%></a></td>
   <td align="right"><%=FormatNumber(intSizeB,0)%></td>
   <td align="right"><%=intSizeK%>K</td>
   <td align="left"><tt><%=strAttr%></tt></td>
   <td align="left"><%=strExt%></td>
   <td align="left"><%=objItem.Type%></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbShortDate)%></td>
   <td align="left"><%=FormatDateTime(dtmDate,vbLongTime)%></td>
<% Next %>
   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

I am using the code above (in a separate index.asp file) to list all the subfolders in a current folder.

It works under IIS and displays the names of all the subfolders (as a hyperlinks to browse to a subfolder). It always sorted them alphabetically (a - z), but now (after folders restoration) it started to add new folders to the bottom of the list, regardless of their name. I've already tried many things: restarted IIS, changed folders attributes etc., but it is the same, however, it works properly on the external webhosting service. What can be wrong? IIS settings? I've changed the file system when I was restoring those folders and files, from NTFS to exFAT, might that be the probable reason?

Link to post
Share on other sites
15 hours ago, justsomeguy said:

I wouldn't count on the filesystem returning data the way you want, I would get the list of all of the files and folders, sort them however you want to sort them, and print the sorted list.

You mean I place them in an array, sort the elements as I want and display them in that order? That makes sense. Because now it only iterates through the folders and prints it out as it was returned from the file system.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...