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

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(Folder.name)
    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

Who is Participating?
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,

lakhiAuthor Commented:
Sorry, I accidentally put my code in 3 times.
lakhiAuthor Commented:
Fixed that, I think but I still really need help :(
lakhiAuthor Commented:
G H, thank you so much! This did the trick.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.