Solved

fso loop through folders and files

Posted on 2010-08-25
9
1,559 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 information …
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/…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now