Link to home
Start Free TrialLog in
Avatar of Member_2_6492660_1
Member_2_6492660_1Flag for United States of America

asked on

ASP Code help needed

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

Avatar of Big Monty
Big Monty
Flag of United States of America image

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

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

Avatar of Member_2_6492660_1

ASKER

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?
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

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
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
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works great thanks

now to testing on all browsers and smart devices.