Link to home
Start Free TrialLog in
Avatar of dhilpert
dhilpert

asked on

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

Avatar of 8riaN
8riaN

What programming languages do you have available on the web server? Do you have a particular language you need to use, or a choice?
Avatar of dhilpert

ASKER

This will be running on my local computer. The language must be Javascript.
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.
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.
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
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
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.
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
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?
college1.jpg thru colleg16.jpg in 1 directory plus another initial.jpg in another directory
thanks
Dave H.
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?
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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of 8riaN
8riaN

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent response but it would be nice if some on had some sort of plug in to read a directory directly.
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.
It would be nice if some one could post a solution for this problem