ASP File Finder
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).
Original Author: Blowno
Inputs
SearchText = The file containing the text you are searching for.
Directory = The Directory (and it's subdirectories) to search (default is c:)
Assumptions
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.
Returns
A list of files on your hard drive (or web server) matching the string you searched for.
Side Affects
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).
API Declarations
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
<%@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
%>
Find Files
<%
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 "
"
if searchtext <> "" Then 'if there is a file to search for then search
response.write ""
response.write ""
'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 ""
rs.movenext
loop
response.write "File Name File Size Date Modified " & rs.fields("FileName") & " "
'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 & " " & rs.fields("FileDate") & "
" 'end the table
else
'no files were found
end if
end if
%>
Comments
No comments have been added for this post.
You must be logged in to make a comment.