fso help | Newbie | | Join Date: Oct 2007 Location: Connecticut
Posts: 1
| |
Hello. I am trying ot make a menu for a web page that is built from the folder structure on the server. I have go the basics working, but have 2 questions.
1) Can this code be cleaned up? -
<%
-
for each FolderIndex in MSubfolders
-
'create menu items for all folders that do not start with an underscore
-
if Left(FolderIndex.name,1) <> "_" Then
-
response.write ("<li><a class='qmparent' href=" & (HOME) & "?loc=/intranet/" & Replace ((FolderIndex.name)," ","%20") & "/>" & (FolderIndex.name) & "</a>")
-
MSubpath = Server.MapPath(FolderIndex.name)
-
MSubfolder = FSO.GetFolder(MSubpath)
-
MSubfolders = MSubfolder.SubFolders
-
if (MSubfolders.count > 1) then
-
response.write("<ul>")
-
for each MSubfolderIndex in MSubfolders
-
if Left(MSubfolderIndex.name,1) <>"_" Then
-
response.write("<li><a href=" & (HOME) & "?loc=/intranet/" & Replace((FolderIndex.name)," ","%20") & "/" & Replace(Replace(MSubfolderIndex.name," ","%20"),"&","&") & "/>" & (MSubfolderIndex.name) & "</a></li>")
-
End If
-
next
-
response.write("</ul>")
-
End If
-
MSubfolders = nothing
-
response.write ("</li>")
-
End If
-
next
-
MSubolders = nothing
-
%>
-
Within the current folder, I need to build a list of folders, files, subfolders, subfolder files, and check if the subfolders contain more subfolders or if they are the last level. Can this be done directly with FSO or do I need to keep repathing a variable (like MSubFolders above)?
2) is there a way to handle '&' in the folder name? I tried - Replace(Replace((FolderIndex.name)," ","%20"),"&","&")
to replace both ampersands and blank spaces, but it wouldn't work. Any ides?
Thanks for looking this over!
|  | Moderator | | Join Date: May 2007 Location: United Kingdom
Posts: 395
| | | re: fso help
Hi Rob,
Welcome to Bytes.com. Please be sure to take a read through the Posting Guidelines and FAQs.
I've edited your posts to include use of our code tags - you can use these yourself, when posting a message, you'll see the # sign on your toolbar.
With regards to your question, the answer is normally a recursive function - one that calls itself iteratively until a certain condition is met (in this instance, there are no more subfolders). When I was doing classic ASP, recursion had me completely lost unfortuantely, so I'll leave it to another expert to help you out.
codegecko
Moderator
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: fso help
This is how I would do it.. Feel free to ask questions about any of it you don't quite understand -
<%
-
Dim Home
-
Home="www.somewhere.com"
-
set fs=Server.CreateObject("Scripting.FileSystemObject")
-
set MainFolder=fs.GetFolder(Server.MapPath("/"))
-
set MSubfolders=MainFolder.SubFolders
-
pWrite("<UL>") 'add crlf to make it pretty
-
for each FolderIndex in MSubfolders
-
if Left(FolderIndex.name,1) <> "_" Then 'create menu items for all folders that do not start with an underscore
-
if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
-
call makeMenu(HOME, FolderIndex.name, "qmparent")
-
call getSubFolders(FolderIndex)
-
end if
-
end if
-
next
-
pWrite("</UL>")
-
set MSubolders = nothing
-
-
-
'recursion ;)
-
public function getSubFolders(current_folder)
-
set MSubfolders = current_folder.SubFolders
-
pWrite("<UL>")
-
for each FolderIndex in MSubfolders
-
if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
-
call makeMenu(HOME, FolderIndex.name, "qmchild")
-
call getSubFolders(FolderIndex) 'call itself to get all this folders sub folders. and so on till the end recursion()
-
end if
-
next
-
pWrite("</UL>")
-
end function
-
-
public function pWrite(str)
-
response.write str & vbcrlf
-
end function
-
-
'generalized function
-
public function makeMenu(Home, Link, className)
-
pWrite("<LI>")
-
pWrite("<a class=""" & className & """ href=""" & HOME & "?loc=/intranet/" & server.URLEncode((Link)) & """/>" & Link & "</a>")
-
pWrite("</LI>")
-
end function
-
%>
-
|  | Forum Leader | | Join Date: Jul 2006 Location: Oklahoma
Posts: 1,076
| | | re: fso help
If you want the full path you can use something like this -
<%
-
Dim Home
-
Dim strTopath
-
Home="http://www.somewhere.com"
-
set fs=Server.CreateObject("Scripting.FileSystemObject")
-
set MainFolder=fs.GetFolder(Server.MapPath("/"))
-
set MSubfolders=MainFolder.SubFolders
-
pWrite("<UL>") 'add crlf to make it pretty
-
for each FolderIndex in MSubfolders
-
if Left(FolderIndex.name,1) <> "_" Then 'create menu items for all folders that do not start with an underscore
-
if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
-
call makeMenu(HOME, fs.GetParentFolderName(FolderIndex) & "\" & FolderIndex.name, "qmparent", false)
-
call getSubFolders(FolderIndex)
-
end if
-
end if
-
next
-
pWrite("</UL>")
-
set MSubolders = nothing
-
-
-
'recursion ;)
-
public function getSubFolders(current_folder)
-
set MSubfolders = current_folder.SubFolders
-
pWrite("<UL>")
-
for each FolderIndex in MSubfolders
-
if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
-
call makeMenu(HOME, fs.GetParentFolderName(FolderIndex) & "\" & FolderIndex.name, "qmchild", false)
-
call getSubFolders(FolderIndex)
-
end if
-
next
-
pWrite("</UL>")
-
end function
-
-
public function pWrite(str)
-
response.write str & vbcrlf
-
end function
-
-
'generalized function
-
public function makeMenu(Home, Link, className, showFullLogical)
-
if not showFullLogical then
-
link=replace(link, Request.ServerVariables("APPL_PHYSICAL_PATH"), "")
-
end if
-
pWrite("<LI>")
-
pWrite("<a class=""" & className & """ href=""" & HOME & "/" & server.URLEncode((Link)) & """/>" & Link & "</a>")
-
pWrite("</LI>")
-
end function
-
%>
-
|  | Similar ASP / Active Server Pages bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|