Important alert: (current site time 7/15/2013 10:44:39 PM EDT)
 

VB icon

ASP File Finder

Email
Submitted on: 10/8/2000 2:00:31 AM
By: Blowno  
Level: Intermediate
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 47601
(About the author)
 
     Did you ever want to search for files using your web browser instead of the MS Find Files program? This ASP file searches your hard drive (or web server) for files containing a given string. You can specify a string to search for and the directory to search in (or leave the default c:\ directory).
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :ASP File Finder
'**************************************
NOTE: The code that traverses the folders was lifted from another app from the PSC site so I am not posting this for purposes of obtaining votes, but just for informational purposes.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: ASP File Finder
' Description:Did you ever want to search for files using your web browser instead of the MS Find Files program? This ASP file searches your hard drive (or web server) for files containing a given string. You can specify a string to search for and the directory to search in (or leave the default c:\ directory).
' By: Blowno
'
' Inputs:SearchText = The file containing the text you are searching for.
Directory = The Directory (and it's subdirectories) to search (default is c:\)
'
' Returns:A list of files on your hard drive (or web server) matching the string you searched for.
'
' Assumes:I realize this app is much slower than the MS Find Files program, but it is web enabled and will not return files that do not contain the search text like the MS program does.
'
' Side Effects:On slow machines - This script may time out if you search the full directory. Note: On my 1GHz machine it can take up to 2 minutes to search the entire 34GB hard drive (I set the script timeout to 5 minutes).
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6346&lngWId=4'for details.'**************************************

<%@LANGUAGE="VBSCRIPT"%>
<%
Response.AddHeader "Pragma", "No-Cache"	'try not to cache page
Response.CacheControl = "Private"		'try not to cache page
	server.scripttimeout = 300		'script will time out after 5 minutes
%>
<html>
<head><title>Find Files Using ASP</title>
<style>
 body {font:10pt Arial;background-color:papayawhip;color:antiquewhite;font-weight:bold;margin-top:0px;margin-left:0px;margin-right:0px}
 A:link {color:black;text-decoration:none}
 A:hover {color:red;text-decoration:underline}
 A:visited {color:black;text-decoration:none}
 td {color:black;border-bottom:1pt solid black;font:9pt Arial}
 th {color:black;border-bottom:1pt solid black;font:9pt Arial;font-weight:bold}
</style>
</head>
<body>
<div style="background-color:tan">
<center>
Find Files<br>
<%
dim filecounter, searchtext, directory	'dimention variables
dim fcount, fsize
filecounter = 0				'initialize filecounter to zero
searchtext = Trim(request("SearchText"))	'get the querystring SearchText
directory = Trim(request("Directory"))		'get the querystring Directory
if directory = "" then directory = "c:\"	'if no directory the set to c:\
						'Write the Search Form to the page
response.write "<form action='FindFiles.asp' method=get>Search For:" & _
	" <input type=text name=SearchText size=20 value=" & Chr(34) & searchtext & _
	Chr(34) & "> Change Directory: <input type=text name=Directory size=20 value=" & _
	Chr(34) & directory & Chr(34) & "> <input style='background-color:blanchedalmond;" & _
	"color:chocolate' type=submit value='Start Search'></form><br></div>"
if searchtext <> "" Then	'if there is a file to search for then search
response.write "<table border=0 width='100%'>"
response.write "<tr><th width='60%'>File Name</th><th width='10%'>File Size</th><th width='30%'>Date Modified</th></tr>"
		'create the recordset object to store
		'the filepath, filename, filesize and last modified date
 set rs = createobject("adodb.recordset")
 rs.fields.append "FilePath",200,255
	 rs.fields.append "FileName",200,255
 rs.fields.append "FileSize",200,255
	 rs.fields.append "FileDate",7,255
 rs.open
	Recurse directory	'call the subroutine to traverse the directories
	Sub Recurse(Path)
			'create the file system object
		Dim fso, Root, Files, Folders, File, i, FoldersArray(1000)
		Set fso = Server.CreateObject("Scripting.FileSystemObject")
		Set Root = fso.getfolder(Path)
		Set Files = Root.Files
		Set Folders = Root.SubFolders
		fcount = 0			'zero out the file count variable
			'traverse through the subdirectories in the current directory
		For Each Folder In Folders
			FoldersArray(i) = Folder.Path
			i = i + 1
		Next
			'traverse through the files in the current folder or subfolder
		For Each File In Files
				'check if the search string is found
			num = InStr(UCase(File.Name), UCase(searchtext))
				'if it is then update the recordset and sort it
			if num <> 0 then
			filecounter = filecounter + 1
			rs.addnew
		rs.fields("FilePath") = File.Path
			rs.fields("FileName") = File.Name
			rs.fields("FileSize") = File.Size
			rs.fields("FileDate") = File.DateLastModified
			rs.update
 		rs.Sort = "FileName ASC"
			end if
		Next
			'recurse through the current directory until 
			'all subfolders have been traversed
		For i = 0 To UBound(FoldersArray)
			If FoldersArray(i) <> "" Then 
				Recurse FoldersArray(i)				
			Else
				Exit For
			End If
		Next
	End Sub
		'if files were found then write them to the document
	If filecounter <> 0 then
			filecounter = 0
		do while not rs.eof
			filecounter = filecounter + 1
			response.write "<tr><td width='50%' valign=top><a href=""" & rs.fields("FilePath") & """>" & rs.fields("FileName") & "</td><td width='10%' align=right valign=top>"
					'get the file size so we can
					'assign the proper Bytes, KB or MB value
				fsize = CLng(rs.fields("FileSize"))
				'if less than 1 kilobyte then it's Bytes
			if fsize >= 0 And fsize <= 999 then
				fnumber = FormatNumber(fsize,0) & " Bytes"
			end if
				'if 1 KB but less then 1 MB then assign KB
			if fsize >= 1000 And fsize <= 999999 then
				fnumber = FormatNumber((fsize / 1000),2) & " KB"
			end if
				'if 1 MB or more then assign MB
			if fsize >= 1000000 then
				fnumber = FormatNumber((fsize / 1000000),2) & " MB"
			end if
				'write each file and corresponding info to the document
			response.write fnumber & "</td><td width='30%' align='center'>" & rs.fields("FileDate") & "</td></tr>"
			rs.movenext
		loop
		response.write "</table>"	'end the table
	else
			'no files were found
	end if
end if
%>
</body>
</html>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/8/2000 2:16:42 AMblowno

Please give your feedback regarding this code - I am still working on allowing the user to key in wildcard strings such as "string*.jpg" - The code currently searches for the exact string within the file name. I'm not sure how to accomplish the wildcard search - Any feedback would be greatly appreciated.
(If this comment was disrespectful, please report it.)

 
1/17/2001 12:15:33 PMSam

I am trying to create a page that will allow individuals within my company intranet to easily copy files from their hard drive to our document folder on our server. However, when I try to use this code to view my local drive I get a Permission Denied error. It works well as long as I stay in the InetPub folder. Is there anyway around the Permission problem to view a file outside of the InetPub folder?
(If this comment was disrespectful, please report it.)

 
1/17/2001 11:31:43 PMBlowno

Sam - If the asp page is running on a server that is not on your local machine (such as pws) then it will not be able to search your local drive as that would be a security breach. Also, you cannot search in a domain other than the domain the file is located in.
(If this comment was disrespectful, please report it.)

 
1/19/2001 10:23:31 PMtest@testit.com

A file is missing. The missing file is: FindFiles.asp
(If this comment was disrespectful, please report it.)

 
1/20/2001 6:59:44 PMblowno

Hey Test - When you copy the code above, and paste it into a document, you need to name the document FindFiles.asp - Otherwise you need to change the submit parameter in the tag to whatever you named your document. It's submitting the form back to itsself. Thanks - Hope it works for you.
(If this comment was disrespectful, please report it.)

 
1/23/2001 3:51:49 PMHasse

It seems as if the last post got jammed, but what I wanted to know was: how can I use it for my website? What do I put for the Domain? server.mappath? And do I have to specify all my other directories?

thnx, Hasse
(If this comment was disrespectful, please report it.)

 
1/23/2001 4:06:50 PMHans Dahlgren

Hi,
Im trying to use your script (FindFiles), but it doesn't seem to work when I use it on my website... do I use
(If this comment was disrespectful, please report it.)

 
1/23/2001 11:10:42 PMblowno

To use this script on a web site just change the line that reads as follows:
if directory = "" then directory = "c:\" TO: if directory = "" then directory = server.mappath("/"), and then place the FindFiles.asp document in your root directory and it should search all of the directories on your web site.
(If this comment was disrespectful, please report it.)

 
1/24/2001 3:21:56 PMHasse

thnkz man, it works ok now! However, when I click the link to the document or picture, it doesn't go to the file, because the link is like this for example:

file:///C:/Inetpub/wwwroot/www.epartnersonline.com/images/phones/ericsso n_r310s.jpg

And
the browser doesn't under stand that it's actually a server it's looking for... how can I change this and only use the link "/images/phones/ericsson_r310s.jpg"???

/ Hasse
(If this comment was disrespectful, please report it.)

 
2/5/2001 8:14:14 AMDan Landry

I am kind of new to this code, I am having some trouble with it. I want to try and use this code to allow someone to enter a filename and search a server for it. How do I implement this code, I tried naming it FindFiles.asp, but it doesn't load anything. What am I doing wrong?
(If this comment was disrespectful, please report it.)

 
5/7/2001 11:33:47 AMXao Xiong

How do I list directory on a network? It works great listing directories/files from local drives, but crashes when I try to list "mapped" (M:\) or network drives (ie \\mycomputer\myshare). Does anybody have a solution for this?

Thank you,

Xao
(If this comment was disrespectful, please report it.)

 
5/23/2001 7:00:08 PMRon

Excellent! Just what I was looking for! It works great!
(If this comment was disrespectful, please report it.)

 
5/6/2002 2:20:43 AMSachp

The Code works great.Thanks
(If this comment was disrespectful, please report it.)

 
9/15/2002 11:52:26 PM

Response object error 'ASP 0156 : 80004005'

Header Error

/search.asp, line 41

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.

(If this comment was disrespectful, please report it.)

 
10/23/2002 2:11:46 AM

excellent utility, practical and handy. i have my own solution to turning the physical dir to a virtual dir so the search result links work on the server : string manipulation. also, i added a few dirs to exclude from a search, as well as exclude certain file extentions. for example, don't want people downloading your .js files or other files, unless you want them to. the code i added is nothing flash, but it works and is functional. so if you need to look at it, email me.
(If this comment was disrespectful, please report it.)

 
1/29/2003 9:44:20 AM

I have the asp page on my webserver. How do you get it to search for a file on the client's machine? My search results have come back with nothing so far and I am assuming that it is searching on the server's file system and not on the clients.
(If this comment was disrespectful, please report it.)

 
5/25/2003 9:56:38 PM

this is good script!! and working properly...
but anybody can modify to increase the speed for serching file...please?!!
(If this comment was disrespectful, please report it.)

 
5/26/2003 11:01:55 PM

I get the following:

Microsoft VBScript runtime error '800a0046'
Permission denied

/FindFiles.asp, line 56


any suggestions?
(If this comment was disrespectful, please report it.)

 
3/4/2004 10:36:35 PM

Hi BlowNo,
(1) My drop list contain 2 drives (C: & M:).
codings:
if Drive_option.selected = "C:" then directory = "C:\" else if Drive_option.selected = "M:" then directory = "M:\"
**Drive_option is the name of my drop list

Is the above coding right?

(If this comment was disrespectful, please report it.)

 
6/23/2004 11:12:05 PM

The Code is great.
But I have the same problem as Hasss
However,when I click the link to the document
or picture, it doesn't go to the file,
because the link is like this for
example:

file:///C:/Inetpub/wwwroot/w
ww.epartnersonline.com/images/phones /eri
csson_r310s.jpg

And
the browser
doesn't under stand that it's actually
a server it's looking for... how can I
change this and only use the link
"/images/phones/ericsson_r310s.jpg"???

Can anyone help ??
(If this comment was disrespectful, please report it.)

 
1/30/2005 8:56:51 AMC. Sibon

For everybody with the path problem. Use an custom download script which can send the file to the client browser without it being in inetpub or even web shared. Create a file called download.asp and past the following code into it: (see next post)
(If this comment was disrespectful, please report it.)

 
1/30/2005 8:58:41 AMC. Sibon

<%@ Language=VBScript %>
<%
option explicit
dim sFile, sRoot, sExt, objFSO, objStream, sFileSize, objFILESIZE

'change or make this line dynamic:
sFile = "c:\pathtofilehere"

set objFSO = server.CreateObject("Scripting.FileSystemObject")
if not objFSO.FileExists(sFile) then
Response.Write "Error: Failed to locate the file for binary read."
Response.End
end if
set objFILESIZE = objFSO.GetFile(sFile)
sExt = objFSO.GetExtensionName (sFile)
sFileSize = objFILESIZE.size
set objFSO = nothing
set objFILESIZE = nothing
(If this comment was disrespectful, please report it.)

 
1/30/2005 8:59:20 AMC. Sibon

Response.ContentType = "application/octetstream"
Response.AddHeader "Content-Length", sFileSize
Response.AddHeader "Content-Disposition", "attachment; filename=" & Request.QueryString("file") & ";"
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile sFile
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing
Response.Flush
%>
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.