Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a server side script to read filenames

Status
Not open for further replies.

jamesp81

Programmer
Mar 3, 2006
19
US
I need a server-side javascript that can read filenames in a directory on the same server, and return an array of strings that are the names of the files in the directory I specify.

I can't seem to find method or function to read the names of all the files in a directory in javascript. Does anyone know what I should use?

Thanks in advance.
 
Since you're using server side javascript I assume that you're coding in ASP. Here's an example to list all files in the current web directory. If you want to list files from a different directory then change the string: "."

Code:
<%@ LANGUAGE="JavaScript" %>
<%

var objDirFSO = Server.CreateObject("scripting.filesystemobject");
var objCurrentDir = objDirFSO.GetFolder(Server.MapPath("."));

for (fileList = new Enumerator(objCurrentDir.Files); !fileList.atEnd(); fileList.moveNext()) { 
   Response.Write(fileList.item() + "<br />");
} 

%>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Here's a function I've been using for quite a while in JScript ASP that works pretty well. It also does a simple bubble sort at the end of the function to put the file names in alphabetical order. I'm sure it could be optimized some, but haven't bothered because it does what I want and need.

Code:
//get directory listing with "filespec" for desired file name pattern to match
//if pattern must be in a specific position in the file name,
//"position" is the zero-based index in the filename to check for that
function getdir(foldername, filespec, position)
{
var filesys=Server.CreateObject('Scripting.FileSystemObject');

if (arguments.length < 2)
  {
  filespec='';
  }

if (arguments.length < 3)
  {
  position = -1;
  }

if (isNaN(position))
  {
  position = -1;
  }

var dir = new Array(), di=0;

var folder = filesys.GetFolder(foldername);

var filenames = new Enumerator(folder.Files);
filenames.moveFirst()
while (!filenames.atEnd())
  {
  var onefile = filenames.item().name;
  if (filespec.length==0 || onefile.toLowerCase().indexOf(filespec.toLowerCase()) > -1)
    {
    dir[di]=onefile;
    di++;
    }
  filenames.moveNext();
  }

if (position > -1)
  {
  var filtered = new Array(), fi=0;
  for (var di=0;di<dir.length; di++)
    {
    if (dir[di].toLowerCase().indexOf(filespec.toLowerCase()) == position)
      {
      filtered[fi++]=dir[di];
      }
    }
  dir = filtered;
  }

for (var di1=0;di1<dir.length-1;di1++)
  {
  for (var di2=di1+1;di2<dir.length;di2++)
    {
    if (dir[di1].toLowerCase() > dir[di2].toLowerCase())
      {
      var tdir = dir[di1];
      dir[di1] = dir[di2];
      dir[di2] = tdir;
      }
    }
  }

return dir;
}

Lee
 
This does not seem to have worked, probably because we are, in fact, not using ASP. All I'm doing is using notepad to write my javascript and upload it to our site.

The Server.CreateObject('Scripting.FileSystemObject') looks suspiciously like VBScript to me, which I assume is what ASP uses. I need something purely javascript, no ASP, that retrieves the filenames in a directory on the server that the script itself resides on.
 
I already have a javascript in place that creates a 'Printer Friendly' page for most of the pages on our site, so I know for a fact that javascript will run on it.
 
Javascript is a client-side language.
If your server support JScript then that will run server-side but that is just scripting support via IIS which is in fact ASP using JScript rather than VBScript.

So either you ARE running ASP or your 'Printer Friendly' script is an include file stored on your server that runs as client-side code to do the printer friendly formatting on the client, not on the server.

Think about it, would you want someone to be able to run code on their own computer that will list all of the files and their locations on your web server? Client-side code just does not have the permissions to do that because it runs from an unknown/untrusted source.


Stamp out, eliminate and abolish redundancy!
 
So let me get this right...

Your server does support server-side scripting (in JavaScript), but it's not ASP-based.

What is it if not ASP?

Are you sure you're not confusing a client-side JavaScript that happens to be served by your server?

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If your server does not support ASP then look into PHP or CGI.

Stamp out, eliminate and abolish redundancy!
 
So either you ARE running ASP or your 'Printer Friendly' script is an include file stored on your server that runs as client-side code to do the printer friendly formatting on the client, not on the server.

Having examined it, I think this is probably what's going on.

Sorry for all the noob questions. Despite what it looks like, I actually am a programmer by profession. It's just that I'm a Borland Delphi programmer. Web programming never has been my thing, but the company wants this done, and I'm the guy on hand.
 
Printer Friendly Pages" can be done completely with CSS, which requires nothing from the server hosting the page.

You need to be a bit more descriptive of what server side technology you have available to you. If you're just writing an HTML file and putting javascript in the page encapsulated in <script> tags, then that is not server side code - it's client side. The browser downloads the HTML file from the server, and then runs the code in the <script> tags on the user's local machine.

The example I gave above is in fact not VBScript, we know this for a few reasons:
[ol]
[li]I explicitly set my server side language to javascript in the first line (better known as JScript when used server side)[/li]
[li]There are semicolons at the end of each line (VBScript does not do this)[/li]
[li]There are brackets encapsulating the code for the for loop (VBScript does not do this)[/li]
[li]There is no corresponding end tag for the for loop[/li]
[li]The variables are defined using a "var" statement, VBScript uses "dim"[/li]
[/ol]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
What is the server they are running? Linux or Windows?
Your server may support ASP but the folders you are in do not have execution enabled for scripts. Server-side script files may need to be located under the script folders like
Or from a network perspective something like:
\\myserver\inetpub\scripts.

IIS used to come default configured to not allow scripts to execute from the HTML folders and you would have to place the server-side scripts in the scripts folder and reference them there. That may still be the case or at least is the case for your server.

If you are on a linux server then you almost certainly have CGI and/or PHP support and you should be able to get code to return the filenames in an array from a related forum.
I use ASP to return a Javascript array for some of my apps, it's not hard to do it just involves server-side code.

Stamp out, eliminate and abolish redundancy!
 
Our server is Linux. I found out that it's running PHP, so it looks like I'm doing this one in PHP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top