Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>

<head>
<SCRIPT LANGUAGE=jscript RUNAT=Server>
function escapeFileName(strname){
return escape(strname);
}
</SCRIPT>
<title>Report Archives - Search Results</title</head>
<body>
<% 
	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>
<TR align=center><TD align=left>Searching For: <%=strSearch%></TD></TR>
<%
intCount=0
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>
		
	</TR>
<%
			End If 
        Next
        SearchSubFolders Subfolder
    Next
End Sub

%>

</body>
</html>

Open in new window

0
lakhi
Asked:
lakhi
  • 3
1 Solution
 
lakhiAuthor Commented:
Sorry, I accidentally put my code in 3 times.
0
 
lakhiAuthor Commented:
Fixed that, I think but I still really need help :(
0
 
G_HCommented:
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,

GH
0
 
lakhiAuthor Commented:
G H, thank you so much! This did the trick.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now