Solved

fso loop through folders and files

Posted on 2010-08-25
9
1,620 Views
Last Modified: 2012-05-10
I need to do a simple fso loop through a root level and multiple sub levels and display the contents of each sub folder

Images
    accessories
           image 1
           image 2
    speakers
           image 1
           image 2
   
I have the code for each part but putting them together is confusing...

'SHOW FOLDERS
dim fs,fo,xFolder
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("D:\corporate\Denon\2010images\images\")

for each xFolder in fo.SubFolders
  Response.write(xFolder.Name & "<br />")
next

set fo=nothing
set fs=nothing



'SHOW FILES IN A FOLDER
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("c:\test\")

for each x in fo.files
  'Print the name of all files in the test folder
  Response.write(x.Name & "<br />")
next

set fo=nothing
set fs=nothing

Open in new window

0
Comment
Question by:jweissdandm
  • 6
  • 2
9 Comments
 
LVL 7

Expert Comment

by:mquiroz
ID: 33526386
i don't know much about asp but if you can put this into a function it would be a recursive beauty:

function beauty (directory_path as string)
'WORK THE FILES
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("c:\test\")

for each x in fo.files
  'Print the name of all files in the test folder
  Response.write(x.Name & "<br />")
next


'FOLDERS
dim fs,fo,xFolder
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("D:\corporate\Denon\2010images\images\")

for each xFolder in fo.SubFolders
  Response.write(xFolder.Name & "<br />")
  beauty (xFolder)
next

set fo=nothing
set fs=nothing


end function
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 33526745
Try this, [untested]
<%
dim fs,fo,xFolder, theFolder
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("D:\corporate\Denon\2010images\images\")

for each xFolder in fo.SubFolders
	theFolder = xFolder.Name
	Response.write(theFolder & "<br />")
	set fo=fs.GetFolder(theFolder)

	for each x in fo.files
		'Print the name of all files in the test folder
		Response.write(x.Name & "<br />")
	next
next

set fo=nothing
set fs=nothing
%>

Open in new window

0
 
LVL 18

Accepted Solution

by:
mgfranz earned 250 total points
ID: 33526802
This is tested and works, just change the folder path as necessary;
<head>
	<title>Untitled</title>
<Style type="text/css">
.title {
	color: #00008b;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	text-decoration: underline;
	height: 2px;
}
.file {
	color: #6869ff;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 10px;
	height: 1px;
}
</style>
</head>

<body>
<%
dim fs,fo,xFolder, theFolder

thePath = Server.MapPath("/templates")
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(thePath)

for each xFolder in fo.SubFolders
	theFolder = xFolder.Name
	Response.write("<div class=title>" &theFolder & "</div><br />")
	set fo=fs.GetFolder(thePath & "/" & theFolder)

	for each x in fo.files
		'Print the name of all files in the test folder
		Response.write "<div class=file>&nbsp;&nbsp&nbsp;&nbsp;" & x.Name & "</div><br />"
	next
next

set fo=nothing
set fs=nothing
%>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:mgfranz
ID: 33526818
I just saw that you might want sub-folder of sub-folder too... hang on a minute.  Gotta make an adjustment to loop through if there is a sub-solder of the folder.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 33527271
OK, try this, again, just change the path of your base folder.  You might want to play with the <div> and maybe add a <span> to get the files and sub-directories to layout better...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<html>
<head>
	<title>Untitled</title>
<Style type="text/css">
.title {
	color: #00008b;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	height: 0px;
}
.file {
	color: #6869ff;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	text-decoration: underline;
	height: 0px;
}
</style>
</head>
<body>
<% 
Dim objFSO, strPath, strCurrentFolder, objFolder
Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
'Set the default directory path
strPath = Server.MapPath("/templates")
strCurrentFolder = strPath 
Set objFolder = objFSO.GetFolder(strCurrentFolder) 

Response.Write "<div class=title>" & objFolder & "</div><br>"
'Call the sub to loop through the folder
IterateThroughDirectory objFolder 

Sub IterateThroughDirectory(objFolder) 
'Get the parent folder
parentFldr = objFSO.GetParentFolderName(objFolder)

'Write out each file found in the folder
For Each objFile in objFolder.Files 
	Response.Write "<div class=file>" & objFile.name & "</div><br>" 
Next 

'Write out each folder and sub-folder name
For Each objSubFolder in objFolder.SubFolders 
	Response.Write "<div class=title>"& parentFldr & "\" & objFolder.Name & "\" & objSubFolder.Name & "</div><br>"
	IterateThroughDirectory objSubFolder 'Call the sub for each subfolder found
Next 
End Sub 

%>
</body>
</html>

Open in new window

0
 
LVL 2

Author Closing Comment

by:jweissdandm
ID: 33532301
Thanks for the complete code!
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 33532967
This one works even better, you can play with the <li> and <ul> tags with DIV and CSS in order to get the lists to line up real pretty...
<%

ListFolder strCurrentFolder 

Sub ListFolder(path)
Dim fs, folder, file, item

     Set fs = CreateObject("Scripting.FileSystemObject")
     Set folder = fs.GetFolder(path)

     'Display the target folder.
     Response.Write("<li>" & folder.Name)
     Response.Write("<ul>" & vbCrLf)

     If (folder.Files.Count > 0)OR(folder.SubFolders.Count >0) Then         
         
         'Display a list of sub folders.
         for each item in folder.SubFolders
           ListFolder(item.Path)
         next
         
         'Display a list of files.
         For each item in folder.Files
           Response.Write("<li>" & item.Name & "</li>" & vbCrLf)
         Next  
     End If

     Response.Write("</ul>" & vbCrLf)
     Response.Write("</li>" & vbCrLf)
End Sub
%>

Open in new window

0
 
LVL 2

Author Comment

by:jweissdandm
ID: 33535055
Thanks governor.  I am actually using some jquery to dress this up.  Your combo of the code made it easy to understand it.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 33535640
I would love to see the resulting code when you finish it!  I will add it to my library.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 100
Html fieldset fix its height and width 4 43
JQuery Autocomplete Tag AJAX (Need nice script) 11 57
How to post data to an API using ASP Classic 3 25
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question