JavaScript - Question...

MrNutz

Banned
Oct 18, 2001
851
0
0
I've been googling away with no avail... How can I count the number of files in a directory using JavaScript? :confused:

thanx :p
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
what is this for? web? windows shell script? i don't think you can do that in browsers normally if that's what you are thinking.
 

MrNutz

Banned
Oct 18, 2001
851
0
0
I'm creating a dynamic slideshow script that rotates through a set of images. I just need to know how many images are in the image folder. They are named image_###.

(BTW, there are only images in the folder)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You can't. Client-side scripts have no access to information on the server. The only way that you could possibly do it is to allow directory listings on the server, download the directory listing via your javascript, then parse out the results via the DOM or a bunch of regular expressions.
 

MrNutz

Banned
Oct 18, 2001
851
0
0
Is there anyway I could run a script that would just check if the file is there. - Basically a loop counter that doesn't actually preload each image.

Also, I found this. But I'm guessing I would need to directory listing on to use the snipit of code that counts the number of files...?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
What you should really do is have all this calculated on the server with a server side language, so that when the page is loaded, the javascript will already know how many files are on the directory.
 

MrNutz

Banned
Oct 18, 2001
851
0
0
I found this page that describes something like that, but I don't know how to implement server side code.

Can I create some type of script file that I just call? And can I tell the script to only give me the number of files in a certain directory?
 

OzzieGT

Senior member
Oct 9, 1999
506
4
81
The JS cannot access anything on the server nor can it call any scripts on the server except through XML or HTTP requests, which requires server side code. So either you will need to store the information in an XML file on the server or use some server-side script like PHP to put the information somewhere in the web page (in a hidden div element perhaps).
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
ok i saw your link. if your server supports perl, you can directly use that approach from that page. all you need to do is copy that Perl program and put it on your server and make it executable (with FTP or some configuration panel). might also need to put it in a cgi directory. just check your hosting service's guides/faq etc for information
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
if you have the perl script in /slide_show

then you can modify the <script> tag from that page to

<script
src="/cgi-bin/script.cgi?NumberOfFiles&images"
type="text/javascript"
language="JavaScript">
</script>

the rest should be the same
 

Chebago

Senior member
Apr 10, 2004
575
0
0
marcyes.com
I wrote this function in php, it will give you an array of all the files in a given directory...it could be easily modified to return the size of the array instead...
 

MrNutz

Banned
Oct 18, 2001
851
0
0
Originally posted by: dighn
if you have the perl script in /slide_show

then you can modify the <script> tag from that page to

<script
src="/cgi-bin/script.cgi?NumberOfFiles&images"
type="text/javascript"
language="JavaScript">
</script>

the rest should be the same

Thanks for the help.

I guess my real question is that if the perl script is in the /slide_show/ directory, then would the src="/cgi-bin/script.cgi?NumberOfFiles&images" tag still point to the /cgi-bin directory?
 

MrNutz

Banned
Oct 18, 2001
851
0
0
Originally posted by: Chebago
I wrote this function in php, it will give you an array of all the files in a given directory...it could be easily modified to return the size of the array instead...
Thanks - How can I call this function from the js code?
 

Chebago

Senior member
Apr 10, 2004
575
0
0
marcyes.com
I might be confused on what exactly you are trying to do, one thing would be to just call my function and then have it take a random picture and display it...is your slideshow one that you want to do in the background, such as switching a header image every now and again, I tried your links, but they didn't work for me, so I am kind of confused why you want to do this in javascript. If it is just for showing a group of images like family pictures, my function should work and then just refresh the page with a new url every 5 seconds or something...
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: MrNutz
Originally posted by: dighn
if you have the perl script in /slide_show

then you can modify the <script> tag from that page to

<script
src="/cgi-bin/script.cgi?NumberOfFiles&images"
type="text/javascript"
language="JavaScript">
</script>

the rest should be the same

Thanks for the help.

I guess my real question is that if the perl script is in the /slide_show/ directory, then would the src="/cgi-bin/script.cgi?NumberOfFiles&images" tag still point to the /cgi-bin directory?

oops then it would be src="script.cgi?NumberOfFiles&images" if the perl script is in the same directory as the calling HTML
 

MrNutz

Banned
Oct 18, 2001
851
0
0
Originally posted by: Chebago
I might be confused on what exactly you are trying to do, one thing would be to just call my function and then have it take a random picture and display it...is your slideshow one that you want to do in the background, such as switching a header image every now and again, I tried your links, but they didn't work for me, so I am kind of confused why you want to do this in javascript. If it is just for showing a group of images like family pictures, my function should work and then just refresh the page with a new url every 5 seconds or something...
My slide show actually cycles through x number of images automatically or manually depending on the user's desire. It does this by increasing the image file's number in the image file name. (i.e. image_001.jpg, image_002,...,image_055,etc.)

So basically I just need to know how many files are in the image folder and that will tell me the highest image number / last filename. I'm starting to really like the php approach b/c that is more independent than the perl approach which can be different from server to server.
 

Chebago

Senior member
Apr 10, 2004
575
0
0
marcyes.com
allright, the attached code should do it...bear in mind that it is not optimized and might contain errors, I think it is well enough commented that you should be able to understand everything that it is doing.

hopefully this will get you pointed in the right direction, good luck and let me know if you need any help or don't understand what I have done...
 

MrNutz

Banned
Oct 18, 2001
851
0
0
Thanks Chebago. :D

I cleaned it up some so that it just returns the number of image files in the specified directory. (At least I think that it will do that :) )

Now if I could just figure out how to assign that value to my javascript variable...

Would this work? :

<script language="javascript">
function num_of_images()
{
var num_files = "ss_count.php";
}
</script>
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: MrNutz
Originally posted by: Chebago
I might be confused on what exactly you are trying to do, one thing would be to just call my function and then have it take a random picture and display it...is your slideshow one that you want to do in the background, such as switching a header image every now and again, I tried your links, but they didn't work for me, so I am kind of confused why you want to do this in javascript. If it is just for showing a group of images like family pictures, my function should work and then just refresh the page with a new url every 5 seconds or something...
My slide show actually cycles through x number of images automatically or manually depending on the user's desire. It does this by increasing the image file's number in the image file name. (i.e. image_001.jpg, image_002,...,image_055,etc.)

So basically I just need to know how many files are in the image folder and that will tell me the highest image number / last filename. I'm starting to really like the php approach b/c that is more independent than the perl approach which can be different from server to server.

If perl is installed on a server, then it will tell you the names of files in a directory. I don't knoww hat you think is "dependent" about that. Perl is installed on nearly every unix/linux machine.
 

MrNutz

Banned
Oct 18, 2001
851
0
0
Sorry I didn't mean it like that. I meant that the perl installation directory can change from server to server. I also read that some servers require the .cgi files to be put in the cgi-bin directory.

That's why I like the php method b/c I don't have to worry if I've got those variable set up right...

But, in any case, I am leaning back toward the perl method b/c I don't see a way that I can assign the php count value to my javascript variable. :p