Solved

Reading Directories

Posted on 2009-07-02
18
312 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:dhilpert
  • 9
  • 9
18 Comments
 
LVL 5

Expert Comment

by:8riaN
ID: 24765018
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
 

Author Comment

by:dhilpert
ID: 24765133
This will be running on my local computer. The language must be Javascript.
0
 
LVL 5

Expert Comment

by:8riaN
ID: 24765269
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
 

Author Comment

by:dhilpert
ID: 24765610
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
 
LVL 5

Expert Comment

by:8riaN
ID: 24765841
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
 

Author Comment

by:dhilpert
ID: 24765937
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
 
LVL 5

Expert Comment

by:8riaN
ID: 24766188
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
 

Author Comment

by:dhilpert
ID: 24766376
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
 
LVL 5

Expert Comment

by:8riaN
ID: 24766674
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
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 

Author Comment

by:dhilpert
ID: 24768318
college1.jpg thru colleg16.jpg in 1 directory plus another initial.jpg in another directory
thanks
Dave H.
0
 
LVL 5

Expert Comment

by:8riaN
ID: 24768373
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
 

Author Comment

by:dhilpert
ID: 24768683
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
 
LVL 5

Expert Comment

by:8riaN
ID: 24768786
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
 

Author Comment

by:dhilpert
ID: 24769044
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
 
LVL 5

Accepted Solution

by:
8riaN earned 500 total points
ID: 24769133
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
 

Author Closing Comment

by:dhilpert
ID: 31599255
Excellent response but it would be nice if some on had some sort of plug in to read a directory directly.
0
 
LVL 5

Expert Comment

by:8riaN
ID: 24819962
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
 

Author Comment

by:dhilpert
ID: 24828739
It would be nice if some one could post a solution for this problem
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now