Solved

ASP Code help needed

Posted on 2013-12-11
8
397 Views
Last Modified: 2013-12-11
My code is below and works great

Runs on a windows 2003 server using IIS 6 ASP

I would just like to show the following file type only

.MP3 .M4A and .WMA

Is there a way to do that currently is lists all files in the folders.

<% ListFolderContents(Server.MapPath("/mp3/mp3 music albums")) %>
<% 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
       url = MapURL(item.path)
        Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
    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
Comment
Question by:Thomas Grassi
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
you'll need to use the GetExtension method.  try something like this:

<% ListFolderContents(Server.MapPath("/mp3/mp3 music albums")) %>
<% 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" 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

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
I can think of several ways to do this.  You could use item.GetExtensionName and check for something good.  Here is another function to add to your code.

function goodExt(t)
  ext= right(t,3)
  if instr(g,ext)>0 then
       goodExt="yes"
       else
       goodExt="no"
  end if
end function

Open in new window

Next modify this part of your code.
 for each item in folder.Files
       url = MapURL(item.path)
       if goodExt(item.Name)="yes" then
             Response.Write("<li><a href=""" & url & """>" & item.Name & "</a></li>")
      end if
    next

Open in new window

0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Thanks for responding

Will give a try.

How about the other two extensions .m4a and .wma can they be added on the same command?

Is there a place where I can read up on this command?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
I forgot something in my code.. very important



function goodExt(t)
  ext= right(t,3)
  g="MP3 M4A WMA"
  if instr(g,ext)>0 then
       goodExt="yes"
       else
       goodExt="no"
  end if
end function

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
you can read up on it here:

http://msdn.microsoft.com/en-us/library/x0fxha2a%28v=VS.85%29.aspx

you can check for multiple extensions, just add an OR close to your if statement:

If UCase(fs.GetExtensionName(item.name)) = "MP3" Then or UCase(fs.GetExtensionName(item.name)) = "MP4" Then
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
try the code with multiple extensions got this

Microsoft VBScript compilation  error '800a0400'

Expected statement

/music_19.asp, line 15
If UCase(fs.GetExtensionName(item.name)) = "MP3" Then or UCase(fs.GetExtensionName(item.name)) = "M4A" Then or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
------------------------------------------------------^

The ^ is under the OR

This is ASP classic  music_19.asp does that matter?

The original one with just the .mp3 worked only .mp3 files appeared.

Now I need the other two .m4a
0
 
LVL 32

Accepted Solution

by:
Big Monty earned 500 total points
Comment Utility
change it to:

If UCase(fs.GetExtensionName(item.name)) = "MP3" or UCase(fs.GetExtensionName(item.name)) = "M4A" or UCase(fs.GetExtensionName(item.name)) = "WMA" Then
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
Comment Utility
Works great thanks

now to testing on all browsers and smart devices.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

763 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

8 Experts available now in Live!

Get 1:1 Help Now