Determining number of files in folder using javascript

P

prashant

hello,
I am looking to determine the number of image files in a
folder so that I could create a dynamic table accordingly. For this i
need a method to identify the number of files in a folder. Is there
any way i could estimate this so that i could determine the size of an
array to create my tables

regards
Prashant
 
S

stannyc

hello,
          I am looking to determine the number of image files ina
folder so that I could create a dynamic table accordingly. For this i
need a method to identify the number of files in a folder. Is there
any way i could estimate this so that i could determine the size of an
array to create my tables

regards
Prashant

In Javascript, you never have to dimension an array before you use
it. You can just do something like this:

var imgs = some code that grabs the images
var imgArray = []; / create an empty array

for (var t=0; t=imgs[t]; t++)
imgArray.push(t);

and your array will be populated
 
M

My Pet Programmer

(e-mail address removed) said:
In Javascript, you never have to dimension an array before you use
it. You can just do something like this:

var imgs = some code that grabs the images
var imgArray = []; / create an empty array

for (var t=0; t=imgs[t]; t++)
imgArray.push(t);

and your array will be populated

Hey, thanks for that. I was assigning new elements with:

imgArray[imgArray.length] = t;

I knew it was inefficient, just never thought to use push, I suppose. I
feel sully now.

Thanks a ton!

~A!
 
D

Dr J R Stockton

In comp.lang.javascript message said:
prashant said the following on 12/23/2007 11:18 AM:

Client-side JS can't do that.
...

Depends on what client-side is thought to mean. Javascript running in
Windows under WSH should be able to do what is needed, and an HTA may
too.

For that environment, microsoft.public.scripting.jscript should be
better.
 
P

prashant

hello,
I am looking to determine the number of image files in a
folder so that I could create a dynamic table accordingly. For this i
need a method to identify the number of files in a folder. Is there
any way i could estimate this so that i could determine the size of an
array to create my tables
regards
Prashant

In Javascript, you never have to dimension an array before you use
it. You can just do something like this:

var imgs = some code that grabs the images
var imgArray = []; / create an empty array

for (var t=0; t=imgs[t]; t++)
imgArray.push(t);

and your array will be populated

Thanks for the idea...
But could you give me an instance how I could get each image file from
a folder ?
 
A

Anthony Levensalor

prashant said:
Thanks for the idea...
But could you give me an instance how I could get each image file from
a folder ?

Use a server-side scripting language to write the values of the files in
a given folder. What you're asking is not meant to be accomplished via
Javascript.

~A!
 
E

Evertjan.

Anthony Levensalor wrote on 07 jan 2008 in comp.lang.javascript:
prashant said:


Use a server-side scripting language to write the values of the files in
a given folder. What you're asking is not meant to be accomplished via
Javascript.

But for Javascript as a serverside scripting language.
 
T

Thomas 'PointedEars' Lahn

Anthony said:
prashant said:

Use a server-side scripting language to write the values of the
files in a given folder.

I don't think this statement makes any sense.
What you're asking is not meant to be accomplished via Javascript.

I don't think so. Furthermore, your statement as it is implies that
JavaScript (and ECMAScript implementations in general) are scripting
languages that can be used only on the client, whereas the opposite
is true.


PointedEars
 
T

Thomas 'PointedEars' Lahn

prashant said:
But could you give me an instance how I could get each image file from a
folder ?

Probably. Where is the script and where are the image files located?


PointedEars
 
A

Anthony Levensalor

Thomas 'PointedEars' Lahn said:
I don't think so. Furthermore, your statement as it is implies that
JavaScript (and ECMAScript implementations in general) are scripting
languages that can be used only on the client, whereas the opposite
is true.

Such an implication was not my intent. In almost all of the questions
asked here, however, Javascript is being used on the client side, and I
was operating under the assumption that the OP was using it that way. If
I caused any confusion, then I apologize.

~A!
 
A

Anthony Levensalor

Randy Webb said:
It is the default assumption, unless stated otherwise, that any question
referring to script is referring to client side scripting. Don't let
Thomas' pedantic behavior distract you.

I'm a little gun-shy as it is about posting, so that helps quite a bit.
Thanks, Randy.
 
P

prashant

Probably. Where is the script and where are the imagefileslocated?

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

The images should be located on a server. The javascript would be on
the client side. As Ive seen from the replies, this should be more
easily accomplished by a server side scripting. But if you have a
simple way to get the images from a folder on the server, please let
me know.

regards
prashant
 
B

Bart Van der Donck

prashant said:
          I am looking to determine the number of image files ina
folder so that I could create a dynamic table accordingly. For this i
need a method to identify the number of files in a folder. Is there
any way i could estimate this so that i could determine the size of an
array to create my tables

There are possibilities if the server allows to list the files in the
directory and if the default index-file is absent.

I made a demo for you:
http://www.dotinternet.be/temp/test.htm

The directory is:
http://www.dotinternet.be/temp/images/

The trick is to find some filter to make sure which text can be
considered safe enough to correspond to one image. In this case, I've
chosen for 'ALT=""'.

I don't say that this demo is the preferred way. You will have a
shorter and more solid solution when doing the job in your favourite
server-side scripting language.

--------------------------------------------------------------------
Start Code
--------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Number of images</title>
<script type="text/javascript">
var xmlhttp;
function load(url)
{
xmlhttp=null;
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
var nr = xmlhttp.responseText.split('ALT="[IMG]"');
alert( (nr.length-1) + ' images');
}
else
{
alert("Problem retrieving resource");
}
}
}
</script>
</head>
<body onload="load('images/?' + new Date().getTime());">
<p>&nbsp;</p>
</body>
</html>

--------------------------------------------------------------------
End Code
--------------------------------------------------------------------

In another post you wrote that script and directory can reside on
different domains. The demo above will not work then because
XMLHttpRequest is subject to the Same Origin Policy. You can use
www.ajax-cross-domain.com to overcome this problem.

Hope this helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top