Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Determining number of files in folder using javascript

7 views
Skip to first unread message

prashant

unread,
Dec 23, 2007, 11:18:20 AM12/23/07
to
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

Randy Webb

unread,
Dec 23, 2007, 3:33:07 PM12/23/07
to
prashant said the following on 12/23/2007 11:18 AM:

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

Client-side JS can't do that. It is trivial in any server side language
though.

> Is there any way i could estimate this so that i could determine
> the size of an array to create my tables

Huh? Nothing in that makes any sense.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

sta...@gmail.com

unread,
Dec 23, 2007, 4:40:25 PM12/23/07
to

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

My Pet Programmer

unread,
Dec 23, 2007, 4:54:52 PM12/23/07
to
sta...@gmail.com 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!

My Pet Programmer

unread,
Dec 23, 2007, 5:11:59 PM12/23/07
to

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


Um, silly. Not sully.

Dr J R Stockton

unread,
Dec 24, 2007, 8:57:03 AM12/24/07
to
In comp.lang.javascript message <wcWdnZZ_5ca...@giganews.com>,
Sun, 23 Dec 2007 15:33:07, Randy Webb <HikksNo...@aol.com> posted:

>prashant said the following on 12/23/2007 11:18 AM:

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

--
(c) John Stockton, Surrey, UK. replyYYWW merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.

prashant

unread,
Jan 7, 2008, 10:15:32 AM1/7/08
to
On Dec 23 2007, 10:40 pm, "stan...@gmail.com" <stan...@gmail.com>
wrote:

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

Anthony Levensalor

unread,
Jan 7, 2008, 10:31:28 AM1/7/08
to
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!


--
anthony at my pet programmer dot com

Evertjan.

unread,
Jan 7, 2008, 10:40:23 AM1/7/08
to
Anthony Levensalor wrote on 07 jan 2008 in comp.lang.javascript:

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

But for Javascript as a serverside scripting language.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Thomas 'PointedEars' Lahn

unread,
Jan 7, 2008, 1:19:01 PM1/7/08
to
Anthony Levensalor wrote:
> 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.

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

Thomas 'PointedEars' Lahn

unread,
Jan 7, 2008, 1:21:25 PM1/7/08
to
prashant wrote:
> 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?

Anthony Levensalor

unread,
Jan 7, 2008, 2:38:54 PM1/7/08
to
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.

Randy Webb

unread,
Jan 7, 2008, 4:51:18 PM1/7/08
to
Anthony Levensalor said the following on 1/7/2008 2:38 PM:

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

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.

Anthony Levensalor

unread,
Jan 7, 2008, 5:34:16 PM1/7/08
to
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.

prashant

unread,
Jan 15, 2008, 2:58:47 PM1/15/08
to
On Jan 7, 7:21 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

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

Bart Van der Donck

unread,
Jan 16, 2008, 10:11:28 AM1/16/08
to
prashant wrote:

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="[IMG]"'.

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,

--
Bart

0 new messages