Reading Directories

I need to code a function that will read a directory that contains a variable number of pictures and load them into an array.
function getArrayPhotosNames();

Open in new window

dhilpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

8riaNCommented:
What programming languages do you have available on the web server? Do you have a particular language you need to use, or a choice?
0
dhilpertAuthor Commented:
This will be running on my local computer. The language must be Javascript.
0
8riaNCommented:
It's straight up not possible (in any normal context.)

Web browsers runs in a special security context. They do not give access to the local file system except in special cases like the <input type="FileUpload"> form control and things like signed java applets which can request special permissions for themselves from the user.  Thus there are no specific javascript functions you can run in a browser which will list the contents of a directory.

There are workarounds, like if you have a web server running on your machine, you could make the directory visible at 127.0.0.1/somename and make it browsable in IIS or Tomcat or whatever, use HTTPRequestObject to get the dir list, then parse the resulting text to find the filenames.  This is a horrible solution, though.

What you really need is a web server (like IIS/Personal Web Server on or the other of which comes with all windows versions after '98{I think} or Apache Tomcat http://tomcat.apache.org/ ) and a real server-side scripting language, like PhP ( http://www.php.net/downloads.php#v5 - windows binaries if you're on windows,) for example - it's free and easy to learn what you'd need to know.

If none of these work, tell me what you're trying to do and maybe we can think of something else.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

dhilpertAuthor Commented:
What I am doing is taking a web class at the university of maryland and here is the specs from the assignment.
 Your project must define and use the functions below:

    * main
    * getArrayPhotosNames - This function has the following prototype: function getArrayPhotosNames(); This function will read the folder name, common name, start photo number, and end photo number and will return an array with the names of the photos that belong to the specified number range. Each photo's name consists of the folder name, followed by the common name, the photo's number and the ".jpg" extension. You can assume only .jpg files will be provided. Notice you don't need to add "/" (it is assumed the folder name will provide it). After reading the start and end photo numbers the function must check that the end number is larger than or equal to the start number. If that is not the case the message "Invalid Numbers" must be displayed via the alert function.
0
8riaNCommented:
That's a crazy assignment, and VERY badly worded...

I think it's supposed to be a way to make fully specified paths to files stored using a known naming convention, like /images/series1_pic01.jpg.../images/series1_pic21.jpg - and you're supposed to make up the path from the inputs, assuming no holes. So if you wanted the about array, you'd call the function like this:
getArrayPhotots("/images/","series1_pic",1,21)

and you should alert an error if they try to pass numbers like 21,1 instead
0
dhilpertAuthor Commented:
could you elaborate a bit more on the coding. There is a fixed directory provided with the assignment which is umcb. It contains all of the pictures. also we are to provide an alternet set of pictures in another directory,
Thanks
0
8riaNCommented:
Wait, is this server side javascript?

As in your code is inside tags like this:

<SERVER>
function getArrayPhotosNames();
...
</SERVER>

In that case, you DO have access to the filesystem through things like the server object and the file class, but I've never heard of anybody actually using server-side javascript, so I have no idea how to get a directory - I found some info on getting files here:
http://docs.sun.com/source/816-6411-10/misc.htm#1017481
But nothing about listing directories.
0
dhilpertAuthor Commented:
No but he hinted at a sever side solution by mentioning that we could install Apache on our computers.
Would this be a possible solution?
Dave
0
8riaNCommented:
Possible, maybe, desireable, no way.  If it were my assignment I'd look at the picture names and try to write something that assumes they are standardized, like output from a digital camera.  Then mention that javascript is a dumb language to read directories with.  Can you give me some of the filenames of the pics in your directory?
0
dhilpertAuthor Commented:
college1.jpg thru colleg16.jpg in 1 directory plus another initial.jpg in another directory
thanks
Dave H.
0
8riaNCommented:
yah, I'd just make a function you can pass the path, and college and 4 and 6 and get path/college4.jpg, path/college5.jpg, path/college6.jpg back in an array - and are you sure you can't have initial1.jpg instead in that other dir?
0
dhilpertAuthor Commented:
I'm not sure what you are suggesting. How do I obtain the number of entries in the directory. An no I cannot move the other jpg into another directory in fact the screens we are supposed to build start out with entering the directory name so we can pull the number of entries and display the pictures 1 thru whatever.
Thanks
Dave
0
8riaNCommented:
It makes no sense to try that with javascript - If the web server will show the directory in browse mode you could parse the response text to pull out the filenames, but I have a hard time believing anybody would give an assignment like that, it's just a stupid way to handle a problem that has much better solutions available.

if you post the entire text of the assignment and all the support stuff I could try to figure out what you're being asked, as I suspect you are being asked to do something simpler than what you are attempting.

Otherwise, I think you should take your questions back to the instructor - specifically, are you supposed to read the directory off the disk to decide if the files you need exist, or do you just pass back names constructed according to the formula in total ignorance of whether the files actually exist on disk. I suspect the latter.
0
dhilpertAuthor Commented:
here is the link to the assignment.
http://www.cs.umd.edu/class/summer2009/cmsc122/assignments/a3/assignment3.html
Thanks for your help
Dave Hilpert
0
8riaNCommented:
Yah, ok.

You definately don't need to read the directory.  The assignment is to create a slideshow viewer that displays a subset of pictures from a known set of files.  I imagine it's this line that's causing you consternation:

 Your project must work with folders of pictures different from the one (i.e., umcp) we have provided.

I think you're 100% safe to just implement the show such that it displays from any subdirectory of the webroot and any files as long as they all start with the same string and end with a number with nothing in between, as in pic1.jpg, pic2.jpg

So just ignore the contents of the directory, make the array of names, and trust the user not to enter slide numbers that don't exist and you should be fine.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhilpertAuthor Commented:
Excellent response but it would be nice if some on had some sort of plug in to read a directory directly.
0
8riaNCommented:
There are all kinds of solutions like that - just not client-side javascript solutions - and that's deliberate for security reasons.  Languages like PhP/JSP/ASP/ColdFusion/Perl/Python do this stuff in their sleep.
0
dhilpertAuthor Commented:
It would be nice if some one could post a solution for this problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.