Solved

ASP code to and pass input to code variable field Help

Posted on 2013-12-20
14
645 Views
Last Modified: 2013-12-23
Windows 2003 Web Server IIS 6 ASP HTML

I have a web site I am working on I wrote some ASP code with the help of experts on this site.

The code works great it produces a full directory listing of all files in the folder that is selected.

Now I would like to allow my users to enter key words the pass them along to the script to produce smaller listings.

If I create a form that will allow them to enter the data then press a Go button will that work?

Was thinking about keeping this ode for a full listing then making a copy of it to modify for the selected listing



See my current code below.

<% ListFolderContents(Server.MapPath("/mp3/mp3musicalbums")) %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.
Response.Write("<ul>")
    for each item in folder.Files
       If UCase(fs.GetExtensionName(item.name)) = "MP3" or UCase(fs.GetExtensionName(item.name)) = "M4A" or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
          url = MapURL(item.path)
          Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
       end if
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     url = "/mp3/" & Right(path, Len(path) - 9)
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window


New at this so any ideas or suggestion would be helpful

Thanks

Tom
0
Comment
Question by:Thomas Grassi
  • 7
  • 7
14 Comments
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39732724
forgot to mention

I have an html page that will be the front end to this asp code.

So the form is on that page and then I want to pass the input from that form to the asp code above to search for only that key work.

If you need more info let me know

thanks
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39732782
search=request.form("search")

 for each item in folder.Files
       If UCase(fs.GetExtensionName(item.name)) = "MP3" or UCase(fs.GetExtensionName(item.name)) = "M4A" or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
         if instr(search,item.name)>0 then
         
                        url = MapURL(item.path)
                        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
         end if

      end if
    next

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39732786
Above  I am assuming you have a file name like abc123.   If you have  form with a field named "search" and somebody enters either "abc" or "123" or "c12" it will work.  

What if somebody enters in "abc123x" for the search term, then this will not produce a result for the file named "abc123".
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39732799
Padas

thanks for responding..

Here is my html code for the page that has the form on it

<HTML>

<style>
body { background:url(images/ilm.jpg) repeat-x; } 
a{color:#DC143C;float:right;margin-right:20px;}
#bottomright{float:right;bottom:0;position:fixed;width:100%;}
</style>
		<title>Tom's Music Site</title>

	<body>

                 <font color=#DC143C>
		<h1>This site is down for maintenance</h1>

                <form>
                <input type="button" value="Show All MP3 Songs" onClick="window.location.href='music.asp'">
                </form> 

<form name="input" action="music.asp" method="get">
 Song, Artist, Album: <input type="text" name="song">
 <input type="submit" value="Submit">
 </form> 

                <p><!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%m/%d/%Y" startspan -->12/12/2013<!--webbot bot="Timestamp" endspan i-checksum="12626" -->
                </p>
                <div id="bottomright">             
                    <form>
                    <input type="button" value="Contact US" onClick="window.location.href='mailto:webmaster@mydom.com?subject=User requesting info'">
                    </form>               
                    <form> 
                    <input type="button" value="Sign Our Guest Book" onClick="window.location.href='GuestBook.htm'">
                    </form> 
                </div>


</html>

Open in new window


How do I pass the input from this to the asp code?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39732842
This piece of code submits to music.asp.
<form name="input" action="music.asp" method="get">
 Song, Artist, Album: <input type="text" name="song">
 <input type="submit" value="Submit">
 </form> 

Open in new window

In my example where I have search=request.form("search") you would just want to change that to  In my example where I have
search=request.form("song")

Open in new window


When somebody submits the form with, "Born To Run" then the music.asp page where you have search=request.form("song"), the variable search will be, "Born To Run".

What you might need to do is set all the text to upper or lower case.  

search=LCase(request.form("song")) 'set to lower case

if instr(search,LCase(item.name))>0 then ' this will compare everything in lower case.

If your file names have underscores for spaces like, "Born_To_Run" then you can account for that like:

search=search=replace(LCase(request.form("song"))," ","_")
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39733088
Padas

Thanks for all the responses

Just got to my computer now that I am home.

search=request.form("song")

 for each item in folder.Files
       If UCase(fs.GetExtensionName(item.name)) = "MP3" or UCase(fs.GetExtensionName(item.name)) = "M4A" or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
         if instr(search,item.name)>0 then
         
                        url = MapURL(item.path)
                        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
         end if

      end if
    next 

Open in new window



Where to I put this inside what I already have for music.asp?

All the songs are in mixed case they do have underscores dashes ( )


Here is the code I updated can you check it for me?

<% ListFolderContents(Server.MapPath("/mp3/mp3musicalbums")) %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.

search=request.form("song")     
Response.Write("<ul>")
    for each item in folder.Files
       If UCase(fs.GetExtensionName(item.name)) = "MP3" or UCase(fs.GetExtensionName(item.name)) = "M4A" or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
          if instr(song,item.name) >0 then
          
          url = MapURL(item.path)
          Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
          
          end if
          
       end if
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     url = "/mp3/" & Right(path, Len(path) - 9)
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window


How does it look?

Where can we put the upper case underscore checking code?

I placed the code on line check it out  at www.tomsmp3.com

I just checked it and it only lists all the artist names which happen to be the folder names too take a look.
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39733299
>it only lists all the artist names which happen to be the folder names
This is because of your code is looking through the folder names.  If you want to search on something linked, you would need a database.

Note, in the code below, I added the variable "search" when calling the function ListFolder.  Please try and go through the code so you understand the logic.

<% 
search=request.form("song") 
ListFolderContents(Server.MapPath("/mp3/mp3musicalbums"),search)

 %>
<% sub ListFolderContents(path)
     dim fs, folder, file, item, url
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
    'Display the target folder and info.
     Response.Write("<h2>"& folder.Name &"</h2>")
     'Display a list of sub folders.
     for each item in folder.SubFolders
                ListFolderContents(item.Path)
     next
     'Display a list of files.

    
Response.Write("<ul>")
    for each item in folder.Files
       If UCase(fs.GetExtensionName(item.name)) = "MP3" or UCase(fs.GetExtensionName(item.name)) = "M4A" or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
          if instr(song,item.name) >0 then
          
          url = MapURL(item.path)
          Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
          
          end if
          
       end if
    next
        Response.Write("</ul>")
   end sub
   function MapURL(path)
     dim rootPath, url
     'Convert a physical file path to a URL for hypertext links.
     url = "/mp3/" & Right(path, Len(path) - 9)
     MapURL = Replace(url, "\", "/")
end function  %>

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39733723
Padas,

Thanks for the info

I replaced my code with the one above.

Now I get HTTP 500 error

Check it out.



So you think I should build a SQL database?
Would that make my searching easier?
What about installing a search engine on my site? Would that help searching the folder structure?

I run SQL 2008 here so that can be done.

My thought process was If I can search thru the folders and list all
Why couldn't I just limit the listing by adding the name .

Is the search checking for the entire file name or just the words we type in the search ?

examples Born To Run real file name is
01 - Born To Run.mp3
or
01 - Bruce Springsteen - Born To Run.mp3
or
01 - Born To Run - Bruce Springsteen - Born To Run.mp3

I am thing to rename all of the file to Number 3
Track , Album Name , Artist, Song



Do you think a SQL database would be better than this method?

I was hoping I could do this with just ASP Coding.


Your thoughts
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39733854
The 500 error does not help.  You have to turn on asp errors http://www.iis.net/learn/application-frameworks/running-classic-asp-applications-on-iis-7-and-iis-8/classic-asp-script-error-messages-no-longer-shown-in-web-browser-by-default or they may be on but you have to surf on the server.

I think having some type of database would help.  This could be a simple text, csv, json, xml file or an sql db or an array.

Let's say you have 2 songs,  "Cadillac Ranch" by Bruce Springsteen released in 1980 and stored in c:\songs\1.mp3 and "The Load-Out" by Jackson Brown released in 1977 and stored in c:\songs\2.mp3.  You can create a delimited file like:

songs="Cadillac Ranch[c]Bruce[c]Springsteen[c]1980[c]songs/1.mp3[r]The Load Out[c]Jackson[c]Brown[c]1977[c]songs/2.mp3"

In the line above, the column delimiter is [c] and the row delimiter is [r].  This means you can create an array by doing the following:

arrSongs=split(songs,"[r]") ' now you have 2 rows.
for each song in arrSongs
   arrDetail=split(song,"[c]") ' now you have the detail in rows
       title=arrDetail(0)
       firstname=arrDetail(1)
       lastname=arrDetail(2)
       year=arrDetail(3)
       location=arrDetail(4)

      Response.Write "The title "&title&", by "&firstname&" "&lastname&" was released in "&year&".  <a href=""&location&"" ">Click here to download.</a>"

next

Open in new window


I would still maintain an sql database but use asp code to write all of this to a file and that way you don't have to access the db.  However, this works ok for a short list.  If you have 1000 titles, it may still be better to use sql server or another data source. The best way will depend on your sites usage.
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39733894
Padas,

Took your advise and yes I agree a SQL database would be a better option.

I was thinking about that myself for sometime.

I created a sql database

I found this

http://technet.microsoft.com/en-US/library/ms345151(v=SQL.90).aspx

Which helped on the design

But it is for SQL 2005 I run SQL 2008  but basically the same..

I will continue with that and see what happens
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39733927
When you are creating the schema, I would add a field for search tags or an alternate name.  Jackson Brown "The Load Out" is a good example.  somebody might search, "Stay" or  "Load out"    Maybe a field for ratings?
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39733945
AS per the article I posted

I create three tables

dbo.artists
dbo.recordings
dbo.tracks

Which table should I add that in?

Currently in dbo.tracks

trackID PK
TrackTitle varchar 50
RecordingID int
TrackSequence tinyint
TrackFileName nvarchar max


Going to import some data for testing in a while

I have to work up my connection string and asp code to access the SQL DB

I have another web site doing a similar thing.

Examples are the best.

Should I close this issue and open another one for SQL for you?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39733972
dbo.artists
Would include id, first_name, last_name

dbo.recordings
would include id, album name, year, location, description, alternate_name, album_rating, publisher

dbo.tracks
Would include id, songname, location_on_your_server

I would also add a cross reference
dbo.artists_songs with fields for id, track_id, artist_id

This would allow you to enter in multiple artists per track.  Although you could also create a text field in dbo.tracks called artists and that can contain a delimited list of artist id's.  I know it does not get you to third normalization but it is common practice for web databases.  The field may contain data like:  1,25,1008.  

Then when you want to display a list of artists for that track you can use the split function and split on a comma.  As you are entering data, if you have checkbox's and check multiple checkbox's you end up with a comma delimited list.

>Should I close this issue and open another one for SQL for you?
If your basic question is answered, then yes, close this out and get into more detail.

On your next question, you may simply want to ask for opinions on how to set up your schema showing your example.  For the question topics, you don't need to use asp, just use sql, mssql, mysql or databases. You are just getting ideas on how to set up search fields.  Once that is done you can concentrate on asp/vb and mssql.
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
ID: 39736034
Creating a SQL database great idea.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now