Connecting Tech Pros Worldwide Forums | Help | Site Map

fso help

Newbie
 
Join Date: Oct 2007
Location: Connecticut
Posts: 1
#1: Oct 14 '08
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?

Expand|Select|Wrap|Line Numbers
  1. <%
  2.   for each FolderIndex in MSubfolders
  3.   'create menu items for all folders that do not start with an underscore
  4.     if Left(FolderIndex.name,1) <> "_" Then
  5.       response.write ("<li><a class='qmparent' href=" & (HOME) & "?loc=/intranet/" & Replace ((FolderIndex.name)," ","%20") & "/>" & (FolderIndex.name) & "</a>")
  6.         MSubpath = Server.MapPath(FolderIndex.name)
  7.         MSubfolder = FSO.GetFolder(MSubpath)
  8.         MSubfolders = MSubfolder.SubFolders
  9.         if (MSubfolders.count > 1) then
  10.           response.write("<ul>")
  11.           for each MSubfolderIndex in MSubfolders
  12.             if Left(MSubfolderIndex.name,1) <>"_" Then
  13.             response.write("<li><a href=" & (HOME) & "?loc=/intranet/" & Replace((FolderIndex.name)," ","%20") & "/" & Replace(Replace(MSubfolderIndex.name," ","%20"),"&","&amp;") & "/>" & (MSubfolderIndex.name) & "</a></li>")
  14.           End If
  15.           next
  16.           response.write("</ul>")
  17.         End If
  18.         MSubfolders = nothing
  19.         response.write ("</li>")
  20.       End If
  21.       next
  22.     MSubolders = nothing
  23. %>
  24.  
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
Expand|Select|Wrap|Line Numbers
  1. Replace(Replace((FolderIndex.name)," ","%20"),"&","&amp;")
to replace both ampersands and blank spaces, but it wouldn't work. Any ides?

Thanks for looking this over!

codegecko's Avatar
Moderator
 
Join Date: May 2007
Location: United Kingdom
Posts: 395
#2: Oct 15 '08

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
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#3: Oct 20 '08

re: fso help


This is how I would do it.. Feel free to ask questions about any of it you don't quite understand
Expand|Select|Wrap|Line Numbers
  1. <%
  2. Dim Home
  3. Home="www.somewhere.com"
  4. set fs=Server.CreateObject("Scripting.FileSystemObject")
  5. set MainFolder=fs.GetFolder(Server.MapPath("/"))
  6. set MSubfolders=MainFolder.SubFolders
  7. pWrite("<UL>") 'add crlf to make it pretty
  8. for each FolderIndex in MSubfolders 
  9.     if Left(FolderIndex.name,1) <> "_" Then 'create menu items for all folders that do not start with an underscore 
  10.         if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
  11.             call makeMenu(HOME, FolderIndex.name, "qmparent")
  12.             call getSubFolders(FolderIndex)
  13.         end if
  14.     end if
  15. next 
  16. pWrite("</UL>")
  17. set MSubolders = nothing 
  18.  
  19.  
  20. 'recursion ;)
  21. public function getSubFolders(current_folder)
  22.     set MSubfolders = current_folder.SubFolders 
  23.     pWrite("<UL>")            
  24.     for each FolderIndex in MSubfolders 
  25.         if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
  26.             call makeMenu(HOME, FolderIndex.name, "qmchild")    
  27.             call getSubFolders(FolderIndex) 'call itself to get all this folders sub folders. and so on till the end recursion()
  28.          end if
  29.     next    
  30.     pWrite("</UL>")            
  31. end function
  32.  
  33. public function pWrite(str)
  34.     response.write str & vbcrlf
  35. end function    
  36.  
  37. 'generalized function
  38. public function makeMenu(Home, Link, className)
  39.         pWrite("<LI>")
  40.         pWrite("<a class=""" & className & """ href=""" & HOME & "?loc=/intranet/" & server.URLEncode((Link)) & """/>" & Link & "</a>") 
  41.         pWrite("</LI>")
  42. end function
  43. %>
  44.  
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#4: Oct 20 '08

re: fso help


If you want the full path you can use something like this
Expand|Select|Wrap|Line Numbers
  1. <%
  2. Dim Home
  3. Dim strTopath
  4. Home="http://www.somewhere.com"
  5. set fs=Server.CreateObject("Scripting.FileSystemObject")
  6. set MainFolder=fs.GetFolder(Server.MapPath("/"))
  7. set MSubfolders=MainFolder.SubFolders
  8. pWrite("<UL>") 'add crlf to make it pretty
  9. for each FolderIndex in MSubfolders 
  10.     if Left(FolderIndex.name,1) <> "_" Then 'create menu items for all folders that do not start with an underscore 
  11.         if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
  12.             call makeMenu(HOME, fs.GetParentFolderName(FolderIndex) & "\" & FolderIndex.name, "qmparent", false)
  13.             call getSubFolders(FolderIndex)
  14.         end if
  15.     end if
  16. next 
  17. pWrite("</UL>")
  18. set MSubolders = nothing 
  19.  
  20.  
  21. 'recursion ;)
  22. public function getSubFolders(current_folder)
  23.     set MSubfolders = current_folder.SubFolders 
  24.     pWrite("<UL>")        
  25.     for each FolderIndex in MSubfolders 
  26.         if Not FolderIndex.Attributes AND 2 then 'if its not a hidden folder
  27.                 call makeMenu(HOME, fs.GetParentFolderName(FolderIndex) & "\" & FolderIndex.name, "qmchild", false)    
  28.                 call getSubFolders(FolderIndex)
  29.          end if
  30.     next    
  31.     pWrite("</UL>")
  32. end function
  33.  
  34. public function pWrite(str)
  35.     response.write str & vbcrlf
  36. end function    
  37.  
  38. 'generalized function
  39. public function makeMenu(Home, Link, className, showFullLogical)
  40.         if not showFullLogical then
  41.             link=replace(link, Request.ServerVariables("APPL_PHYSICAL_PATH"), "")
  42.         end if
  43.         pWrite("<LI>")
  44.         pWrite("<a class=""" & className & """ href=""" & HOME & "/" & server.URLEncode((Link)) & """/>" & Link & "</a>") 
  45.         pWrite("</LI>")
  46. end function
  47. %>
  48.  
Reply