faqts : Computers : Programming : Languages : JavaScript : Forms

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 8 people (75%) answered Yes
Recently 6 of 8 people (75%) answered Yes

Entry

How can I read the filenames of files in a folder on a webserver and pass them to a Javascript Array

Jul 22nd, 2002 05:56
Jean-Bernard Valentaten, Nick Hearn,


You will need a server-side language like PHP, ASP, JSP, Perl or any 
other CGI-skript. I'll write the following example using ASP. You may 
need to adapt it a little bit.
<%
Function getFileNames(folderName)
  Dim fso, fc, f1, s
  Set fso = CreateObject("Scripting.FileSystemObject")
  s = ""
  If (fso.FolderExists(folderName)) Then
    Set fc = fso.GetFolder(folderName).Files
    For Each f1 in fc
      s = s & "'" & f1.name & "'" & ","
    Next
    If (Len(s) > 0) Then
      s = Left(s, Len(s) - 1)
    End If
    Set fc = Nothing
  end if
  Set fso = Nothing
  getFileNames = s
End Function
%>
<script language="JavaScript">
<!--
  var fileArray = new Array(<%=getFileNames("yourFolder")%>);
//-->
</script>
That will do the trick. Of course there are ways to do it with js 
alone, but I would not recommend that, since it's a lot of work (you 
need to write a stringparser etc.) and will not always work!!
HTH,
Jean