Solved

fso loop through folders and files

Posted on 2010-08-25
9
1,628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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