January 18th, 2013, 07:35 PM
Search files in a folder
Hi, does anyone know how to search files in a folder using asp?
My work project is to select a .mpg file from a dropdown menu which lists all the files in that folder. Once the file is selected it will play in a media player. If that is a clip the person wants, he/she then clicks a button to copy the file to a folder. All of this works in testing.
However, I want to add a search box that will allow the person to search the files in the folder.
If the files are:
I want the person to be able to type tre and the files tree.mpg and tree-1.mpg are the only files displayed - instead of all the files in the folder.
I have searched in google, bing, and this forum and can not find any code or information that has helped. I was surprised at how few returns came back.
I have been tweaking asp stuff for a few years now and this is my first time to post a question. I am really stuck on this one.
Thanks in advance. I really appreciate it.
January 19th, 2013, 06:09 PM
I have finally found some code that I may be able to use for this. I will need to see if I can tweak the results to feed a dropdown menu. I found a "possible" solution here. Now back to tweaking. Still appreciate any thoughts on this. Thanks!
ASP File Finder by Blowno
January 22nd, 2013, 09:39 AM
Are you want to reload your dropdown menu when use type on search box or you displaying file names in the list? How you displaying files from that folder? Is each user has own folder? It looks like you expect has copy of original file for each user. Are you using database to track user activity? We need to know more details and publish your code for page with search box
if you found this post is useful click Give Rep button (bottom side on this reply ) and agree
January 24th, 2013, 08:20 PM
Thanks for the response gk53. I found a link that got me what I needed. http://http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=6346&lngWId=4
I tried to post this the other day but it went to a moderator....unsure why. I wanted to provide the link for anyone looking for the same thing. In my case, this worked great but some of the folders have so many files that it was taking too long for a search. Now, I am doing the same thing but I am writing the file names to a database and searching the database. That way we only have to "build the database" as needed.
Here is what worked for me (the non database version). It was just too slow on folders with a lot of files.
document.getElementById('video').innerHTML='<object width="300" height="300" '
+'standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">'
+'<param name="url" value="'+what+'">'
+'<param name="uiMode" value="full">'
+'<param name="autoStart" value="true">'
+'<param name="loop" value="true">'
+'<embed type="application/x-mplayer2" '
+'showcontrols="true" uimode="full" width="300" height="300" '
+'src="'+what+'" autostart="true" loop="true">'
<INPUT TYPE="button" value="Refresh this page" onClick="parent.location='index.asp'">
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
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:\inetpub\wwwroot\clips\video\" 'if no directory the set to c:\
'Write the Search Form to the page
response.write "<form name='f2' action='index.asp' method=get>" & _
" <input type=text name=SearchText size=20 value=" & Chr(34) & searchtext & _
Chr(34) & "> <input type=hidden name=Directory size=20 value=" & _
Chr(34) & directory & Chr(34) & "> <input type=submit value='Search Newsclips' onClick='parent.location='index.asp'></form><br></div>"
if searchtext <> "" Then 'if there is a file to search for then search
set rs = createobject("adodb.recordset")
Recurse directory 'call the subroutine to traverse the directories
'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
Response.Write ("<form action=""submit.asp"" method=""post"" name=""f1"">")
Response.Write ("<center><select name=""files"" onchange=""PlayIt(this.value);this.blur()"" >")
Response.Write ("<option value=""null.mpg"">" & "-- Please Select Searched Newsclip --" & "</option>")
For Each Folder In Folders
FoldersArray(i) = Folder.Path
i = i + 1
'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.fields("FilePath") = File.Path
rs.fields("FileName") = File.Name
rs.Sort = "FileName ASC"
'recurse through the current directory until
'all subfolders have been traversed
For i = 0 To UBound(FoldersArray)
If FoldersArray(i) <> "" Then
'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 ("<option value=""" & "video/" & rs.fields("FileName") & """>" & rs.fields("FileName") & "</option>" & vbCrLf)
Response.Write("no files found") 'no files were found
<input type="submit" value="Send this clip to Carbon Farm">
<div id="video" align="center">
<object width="300" height="300"
standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">
<param name="url" value="null.wmv">
<param name="uiMode" value="full">
<param name="autoStart" value="true">
<param name="loop" value="true">
showcontrols="true" uimode="full" width="300" height="300"
src="" autostart="1" loop="true">
January 26th, 2013, 10:48 PM
Thanks for your response gk53. I found a link that helped me do what I wanted to do; however, I am moving in a different direction. Now, I am writing the files to a database and pulling from the database. I tried to post the link to the page that helped me but it said a moderator needed to verify my post and it's been a week and....well....thanks anyway.