2nd problem Classic ASP - Recursive search of folders and subfolders

Posted on 2011-05-10
Last Modified: 2012-05-11
I was able to get the search to work and to find the correct file. Initially, it wasn't pulling in the name of the parent to the sub-folder where the file was found. I thought I had fixed it but it's bringing in the name of the subsquent sub-folder instead of the parent.
So if the file is found here: /Docs/Report Archives/10000 - 19999/10500 - 10999
The link shows as this: /Docs/Report Archives/11000 - 19999/11499 - 10999
I'm not sure how to fix it.
My code is attached.
I would appreciate any help. Thanks

function escapeFileName(strname){
return escape(strname);
<title>Report Archives - Search Results</title</head>
	Dim sPath, sLink
	Dim objStartFolder, objFolder
	Dim strSearch, foldName, subFoldName
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	sPath = Server.MapPath("/Docs/Report Archives")
	sLink = "/Docs/Report Archives/"
	objStartFolder = objFSO.GetFolder(sPath)
	Set objFolder = objFSO.GetFolder(objStartFolder)
	strSearch = Request("searchfor")
	strSearch = "10635"
<TABLE width="300" border = "1" cellpadding=5>
  <TR align=center> 
    <TH align=center>Report Archives - Search Results</TH>
<TR align=center><TD align=left>Searching For: <%=strSearch%></TD></TR>
SearchSubfolders objFSO.GetFolder(objStartFolder)
Sub SearchSubFolders(Folder)
	foldName = escape(
    For Each Subfolder in Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
          If(InStr(1, objFile.Name, strSearch, vbTextCompare) > 0 ) then
			intCount = intCount + 1
			subFoldName = escape(Subfolder.Name)
		  response.Write "Folder" & foldname & "Sub-folder" & subFoldName
			fn=sLink & foldName & "/" & subFoldName & "/" & escapeFileName(cstr(objFile.Name))
	<TR><TD align=left>
		<A HREF="<%=fn%>" target="_top"><%=objFile.Name %></A></TD>
			End If 
        SearchSubFolders Subfolder
End Sub



Open in new window

Question by:lakhi

    Author Comment

    Sorry, I accidentally put my code in 3 times.

    Author Comment

    Fixed that, I think but I still really need help :(
    LVL 11

    Accepted Solution

    Hi There,

    I like recursive subs and functions, but they can take a little bit to get your head around.

    This however is not truly recursive. (I mention this because it is relevant.) Although the code supplied is going deeper and deeper into the directory structure, the output variable "fn" is set to:

    \Docs\Report Achives\MasterFolder\SubFolder
    ... even if the file is found at...
    \Docs\Report Achives\Random\Folder\Names\MasterFolder\SubFolder
    ... only the root and the last two folders will be used.

    If this is OK with you the leave it as is. If not, consider converting this to a function, which can pass back to itself the full path which it is using (to make it truly recursive).

    Back to the issue. Each successive time the sub is called "foldName" is set as the current parent directory. This variable however is reset every time the sub is called, or re-called.

    So in your code above, line 51 re-calls the sub. then the outer "next" happens. Now lines 41 and 42 use the modified "foldName" (modified by the sub-sub).

    The solution to this is to Dim the variables used inside the sub. Each iteration of the sub then has it's own set of variables, which will always be correct for it.

    Just add the line:
    Dim foldName, objFolder, colFiles, subFoldName

    Open in new window

    ... as the first line inside the sub-routine.

    I think this will probably make your code work, as the directory structure you are using looks fairly rigid. If not, consider moving to a function, which could be truly recursive.

    Hope this helps,


    Author Comment

    G H, thank you so much! This did the trick.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now