Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directory Filename finder page

Status
Not open for further replies.

bigdan

Technical User
Nov 13, 2000
41
CA
I did a intranet for my departement using a MSACCESS DB.

But there's some document on the network and I want a script that will search a string in the filename and return the result.

I found some script on the net and none seem to work...
i did a simple script with fileexists command but i'm unable to put wildcard character in it.

Please help me

Thanks
Dan
 
You could use the FileSystemObject and recursively search each folder and file in the filetree, using InStr to match the name:
Code:
Dim fso, fol ' initial fso and folder objects
Dim searchStr' substring we are looking for
Dim result   'this will be the path to the first match

Set fso = Server.CreateObject("Scripting.FileSystemObject")

'pretend the searchStr got set in Request.Form("txtSearch")
searchStr = Request.Form("txtSearch")

'start the search with current directory
Set fol = fso.GetFolder(Server.MapPath("."))

'search recursively - see below for the actual function that handles the searching
result = FileSearch(searchStr,fol)
'if len(result) > 0 then w have a match, lets print a link
If len(result) > 0 Then
   'since we are only searching a web dirctory we can make this a download link
   Response.Write &quot;File Found: <a href=&quot;&quot;&quot; & result & &quot;&quot;&quot;>&quot; & result & &quot;</a>&quot;
End If

'function that handles the actual searching that is called above
Function FileSearch(str_search, current_fol)
   Dim fil, fol  'temp file and folder objects
   Dim temp      'temp string for subfolder searching
   
   'search the current directory for a match first
   For Each fil in current_fol.Files
      'If one matches, return it and exit
      If InStr(fil.Name,str_search) > 0 Then
         FileSearch = fil.Name
         Exit Function
      End If
   Next

   'If it gets this far we haven't found it, so search the subfolders
   For Each fol in current_fol.Folders
      'If this sub-folder returns a result for FileSearch
      ' than it had a match so return it and exit
      temp = FileSearch(str_search,fol)
      If len(temp) > 0 Then
         'pass back this folder name as part of the path to the file so we can make a link later
         FileSearch = fol.Name & &quot;/&quot; & temp
         Exit Function
      End If
   Next

   'no files were matched, no files in subfolders were matched, return an empty string
   FileSearch = &quot;&quot;
End Function


Anyways, that was written on the fly so it mayhave some kludginess, but the theory should be sound,

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
It's working with few mod thanks

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top