fso help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Lavin
    New Member
    • Oct 2007
    • 1

    fso help

    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?

    Code:
    <%
      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"),"&","&amp;") & "/>" & (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
    Code:
    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!
    Last edited by JamieHowarth0; Oct 15 '08, 11:17 AM. Reason: Missing code tags
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    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

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      This is how I would do it.. Feel free to ask questions about any of it you don't quite understand
      Code:
      <%
      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
      %>

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        If you want the full path you can use something like this
        Code:
        <%
        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
        %>

        Comment

        Working...