Link to home
Start Free TrialLog in
Avatar of Member_2_6492660_1
Member_2_6492660_1Flag for United States of America

asked on

ASP Code to List Folders Only

ASP Classic
IIS 6

Would like to know how I can just list all the folders that are under one folder on my server

Topfolder
   folder1
   folder2
   folder3
    etc
Have a total of over 2600 folders under this main folder.

This is s far as I got

<%
 dim fs,f
 set fs=Server.CreateObject("Scripting.FileSystemObject")


Thanks for any help
Avatar of Big Monty
Big Monty
Flag of United States of America image

Here's an example you can use as a reference

ListFolder "P:\ATH\TO\THE\FOLDER\ON\THE\SERVER"

' -- Main Functions ----------------------------------------------------
Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

' ----------------------------------------------------------------------
Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
    If Not IsHidden(child) Then
      ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
    End If  
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
    If Not IsHidden(child) Then
      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
    End If
  Next

  Say "</ul>"
End Sub

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

Function IsHidden(File)
  IsHidden = File.Attributes And 2 = 2
End Function

Open in new window

If you need it customized let me know and I can do it for you when I get home and nor on my phone :)
Avatar of Member_2_6492660_1

ASKER

Thanks Big Monty

checking it out now and going to eat diner
No problem. Again let me know if you get stuck and I'll gladly help you out
Big Monty

I m back now

This code produces a list of all files in every folder.

I would just like to list only the folder names similar to a dir /ad /b

thoughts
Then just call the sub ListFolders and comment or take out the code altogether. If you're still having problems post your code so far with this new code added in
Not sure what to remove

Tried a few options keep getting line errors
OK I ought to be home in an hour, I'll get you the code then
Ok great thanks
ok let's try something like this. put the following code into a blank asp page and run it, and make sure you set the correct path on the first line

<%
ListFolder "c:\folderToList\"          '-- this can be any physical path your web site has access to

Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = Server.CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
       ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
  Next

   '--***********uncomment the lines below to list the files as well
'  relativePath = h(relativePath)

'  For Each child In folder.Files
'    If Not IsHidden(child) Then
'      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
'    End If
'  Next

'  Say "</ul>"
End Sub

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

%>

Open in new window

Big Monty

That's now listing the folders and the sub folders not the files

I was needing this one next.

But the output is indented way too far to the right

check it out

www.tomsmp3.com/music4.asp


Only need first level folders

music
   mp3musicalbums
          artist a
          artist b
          artist c
         2600 more
i just tested out the following code and it displayed all files and folders on my web site. I set my path to the root of my site, you can set it to whatever:

<%@language=vbscript%>

<%

ListFolder Server.MapPath("/")          '-- this can be any physical path your web site has access to

Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = Server.CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
       ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
  Next

  Say "</ul>"
End Sub

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

%>

Open in new window

the line

ListFolder Server.MapPath("/")

is where you want to set your path. If you use a physical path, don't user Server.MapPath.
Response object error 'ASP 0251 : 80004005'

Response Buffer Limit Exceeded

/music4.asp, line 0

Execution of the ASP page caused the Response Buffer to exceed its configured limit.

fixed that by

<%Response.Buffer = false%>

but it lists all files I only need folder names

Take a look at it www.tomsmp3.com/music4.asp
flush out the buffer in each loop:

<%@language=vbscript%>

<%
Response.Buffer = false
ListFolder Server.MapPath("/")          '-- this can be any physical path your web site has access to

Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = Server.CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
       ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
       Response.Flush
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
       Response.Flush
  Next

  Say "</ul>"
End Sub

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

%>

Open in new window

•mp3musicalbums
¿
!!!
¿
Louden Up Now

¿01-Louden Up Now-When The Going Gets Tough, The Tough Gets Krazee.mp3

Response object error 'ASP 0159 : 80004005'

Buffering Off

/music4.asp, line 31

Buffering must be on.

that is still listing the files too



I only need the !!! to be displayed

<%@language=vbscript%>

<%
Response.Buffer = false
ListFolder "m:\music\mp3musicalbums"          '-- this can be any physical path your web site has access to

Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = Server.CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
       ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
       Response.Flush
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
       Response.Flush
  Next

  Say "</ul>"
End Sub

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

[code]<%@language=vbscript%>

<%
Response.Buffer = false
ListFolder "m:\music\mp3musicalbums"          '-- this can be any physical path your web site has access to

Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = Server.CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
       ListFolderContents child, relativePath & PathEncode(child.Name) & "/"
       Response.Flush
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
      Say "<li><a href=""" & relativePath & h(PathEncode(child.Name)) & """>" & h(child.Name) & "</a></li>"
       Response.Flush
  Next

  Say "</ul>"
End Sub

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

%>

Open in new window

i'm not sure I'm following, first you say you want just the folders, then you say you want the files as well, i'm a bit confused...try the code below on just one of your subfolders and see if that works. then try the main folder and set the first line to

Response.Buffer = true

<%@language=vbscript%>

<%
Response.Buffer = false
ListFolder "m:\music\mp3musicalbums"          '-- this can be any physical path your web site has access to

Sub ListFolder(path) 
  Dim fs, rootPath

  Set fs   = Server.CreateObject("Scripting.FileSystemObject")
  rootPath = Replace(path, Server.MapPath("/"), "") & "\"

  ListFolderContents fs.GetFolder(path), PathEncode(rootPath)
End Sub

Sub ListFolderContents(folder, relativePath)
  Dim child

  Say "<ul>"
  Say "<li><div class=""folder"">" & h(folder.Name) & "</div>"

  For Each child In folder.SubFolders
       ListFolderContents child, relativePath & child.Name & "/"
       Response.Flush
  Next

  relativePath = h(relativePath)

  For Each child In folder.Files
      Say "<li><a href=""" & relativePath & child.Name & """>" & child.Name & "</a></li>"
       Response.Flush
  Next

  Say "</ul>"
End Sub

Function PathEncode(s)
  ' this creates a more correct variant of what Server.URLEncode would do
  PathEncode = Replace(s, "\", "/")
  PathEncode = Server.URLEncode(PathEncode)
  PathEncode = Replace(PathEncode, "+", "%20")
  PathEncode = Replace(PathEncode, "%2F", "/")
  PathEncode = Replace(PathEncode, "%2E", ".")
  PathEncode = Replace(PathEncode, "%5F", "_")
End Function

' -- Helper Functions / Shorthands ---------------------------------------
Sub Say(s)
  Response.Write s & vbNewLine
End Sub

Function h(s)
  h = Server.HTMLEncode(s)
End Function

%>

Open in new window

Big Monty

I found this  maybe you can understand it better than I

http://www.devx.com/tips/Tip/27130
Your latest code

•mp3musicalbums
¿
!!!
¿
Louden Up Now

¿01-Louden Up Now-When The Going Gets Tough, The Tough Gets Krazee.mp3

Response object error 'ASP 0159 : 80004005'

Buffering Off

/music4.asp, line 31

Buffering must be on.
I worked this up

<%@language=vbscript%>
<%Response.Buffer = false%>
<%
Dim strFolders
strRoot = "m:\music\mp3musicalbums"
Set objFSO = CreateObject("Scripting.FileSystemObject")
FindDirectories(strRoot)
WScript.Echo strfolders
 
Sub FindDirectories(input)
	Set objFolder = objFSO.GetFolder(input)
	Set colSubFolders = objFolder.SubFolders
	For Each Folder In colSubFolders
		strFolders = strFolders  & Replace(Folder.Path,Folder.Drive,"") & ";"
		FindDirectories(Folder.Path)
	Next
End Sub


%>

Open in new window



But I get this

Microsoft VBScript runtime  error '800a01a8'

Object required: 'WScript'

/music4.asp, line 8


No little VB
Big Monty

this works from my windows 7 computer  (see attached file)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("M:\MUSIC\MP3MUSICALBUMS")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name, objSubfolder.Size
Next

Open in new window


But when I do this

<%@language=vbscript%>
<%Response.Buffer = false%>
<%
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("M:\MUSIC\MP3MUSICALBUMS")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name, objSubfolder.Size
Next

%>

Open in new window



I get this

Microsoft VBScript runtime  error '800a01a8'

Object required: 'Wscript'

/music4.asp, line 9
test.txt
Big Monty

New code  needed to change wscript.echo to response.write

<%@language=vbscript%>
<%Response.Buffer = false%>
<%
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("M:\MUSIC\MP3MUSICALBUMS")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    response.write(objSubfolder.Name)
Next

%>

Open in new window


Check it out www.tomsmp3.com/music4.asp

I got the information I need but now I need to parse it out onto separate lines with a checkbox on each line.

Long day for me heading to sleep now early to raise.

Any ideas let me know.

Thanks
that's pretty much the same code I offered up, except without the file names. either way if its working, that's all that matters :)

to get the checkboxes in, use the following code, which assigns the folder name as the value for each checkbox:

<%@language=vbscript%>
<%Response.Buffer = true%>
<%
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("M:\MUSIC\MP3MUSICALBUMS")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    folderName = objSubfolder.Name
    Response.Write "<p><input checkbox name="chkFolders" value='" & folderName & "' /></p>"
Next

%>

Open in new window


now, when you post the data to another page, since all of checkboxes are named chkFolders, you'll do a single Request.Form("chkFolders") which will give you a comma delimited string of all of the options that are checked
Big Daddy

Just tested

Microsoft VBScript compilation  error '800a0401'

Expected end of statement

/music4.asp, line 10
Response.Write "<p><input checkbox name="chkFolders" value='" & folderName & "' /></p>"
-----------------------------------------^


<%@language=vbscript%>
<%Response.Buffer = true%>
<%
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("M:\MUSIC\MP3MUSICALBUMS")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    folderName = objSubfolder.Name
    Response.Write "<p><input checkbox name="chkFolders" value='" & folderName & "' /></p>"
Next

%>

Open in new window

sorry about that, try this:

<%@language=vbscript%>
<%Response.Buffer = true%>
<%
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("M:\MUSIC\MP3MUSICALBUMS")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    folderName = objSubfolder.Name
    Response.Write "<p><input checkbox name='chkFolders' value='" & folderName & "' /></p>"
Next

%>

Open in new window

Thanks we are getting close

check out the site

www.tomsmp3.com/muic4.asp

It placed the names of the folders inside the check box and since the check box is not so big the full folder name can not be seen.

I need the check box then folder name
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
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
Big Monty

That's it

Now I can code a submit button so when you click on the check box it will list all the songs or albums from that artist

going out now will try later
Thanks again
Big Monty

Back online

Just got back my daughter just had her second Baby proud Pop Pop

Would like to modify the code a bit for a new page

What we did was list all the artists

Now I would like to list all the artists and the albums

M:\Music\mp3musicalbums>cd the doors

M:\Music\mp3musicalbums\The Doors>dir
 Volume in drive M is Music
 Volume Serial Number is 0C03-7207

 Directory of M:\Music\mp3musicalbums\The Doors
04/09/2014  11:18 AM    <DIR>          Billboard Top 100 - 1967
04/09/2014  11:18 AM    <DIR>          Billboard Top 100 - 1968
04/09/2014  11:18 AM    <DIR>          Billboard Top 100 - 1969
04/09/2014  11:18 AM    <DIR>          Billboard Top 100 - 1971
04/09/2014  11:18 AM    <DIR>          L.A. Woman
04/09/2014  11:18 AM    <DIR>          Live At The Hollywood Bowl
04/09/2014  11:18 AM    <DIR>          Morrison Hotel
04/09/2014  08:41 PM    <DIR>          The Best of the Doors
04/09/2014  11:19 AM    <DIR>          The Doors
04/09/2014  11:19 AM    <DIR>          The Matrix- San Fran '67


Like this
Big Monty

Thanks again

I will open a new question later