Solved

fso loop through folders and files

Posted on 2010-08-25
9
1,615 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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
How to update a dummy recordset with classic ASP 4 98
FileUp - Classic ASP 5 19
Html Table Looping (part 2) 5 27
Html value of radio 14 28
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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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