Solved

fso loop through folders and files

Posted on 2010-08-25
9
1,592 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

772 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