By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

Getting the files in a dir

P: n/a
Dear Folks,

In a HTML page I want to get all the files in a selected dirs. Is
that any way to do this thru javascript.

Help will be highly appreciated. Thanks in advance.

Regards,
Ameen T
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Noorul Ameen wrote:
In a HTML page I want to get all the files in a selected dirs. Is
that any way to do this thru javascript.


If you mean, is there a way to read the list of files in a directory
from an HTTP server using JavaScript alone. The answer is no.
Client-side JavaScript will always need help from the server or some
other component.

The server will have to generate a list of files using some server-side
script that client-side JavaScript can parse, you could then load the
URL in a hidden frame or use the XML HTTP Request object to retrieve it;
or
The server will have to produce a file listing when presented with a URL
that does not contain an index.html/default.htm type file, then load
that URL in a hidden frame or use the XML HTTP Request object to
retrieve it; or
You will have to use a Java applet to retrieve the information you want:
<url: http://www.galasoft-lb.ch/myjava/Web...Demo/Demo.html />

Usually if you have server-side processing available, the easiest thing
to do is have it generate the file list as prebuilt client-side
JavaScript. Then the client-side JavaScript does not need to do any
heavy lifting, it simply reads the Array it already has available:

<%
// server-side JavaScript
var fileSystem = new BVI_FileSystem();
var directoryList = (fileSystem ? fileSystem.listDirectory() : null);
if (directoryList && (len = directoryList.length()) > 0) {
%>

<script type="text/javascript">
<% for (var i = 0; i < len; i++) { %>
var clientSideDirectoryList = [
<%= (i > 0 ? ",\n" : "") + directoryList.get(i) %>
];
<% } %>
</script>

<%
}
%>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #2

P: n/a
In article <bd**************************@posting.google.com >,
no**********@gmail.com (Noorul Ameen) wrote:
In a HTML page I want to get all the files in a selected dirs. Is
that any way to do this thru javascript.


Some people want to display a random graphic image on a page. They
could place all the graphic images in a directory without an index.html
file and use the code below to get the list of graphic images to pick
from.

I ran into a few problems in writing this code.

1) How do you tell when the iframe is loaded. Not all web browsers
support the onload attribute on an iframe.

2) Netscape 7.1 doesn't allow you to look in a page outside of your
domain. For Netscape 7.1, you need to make the directory a subdirectory
in the folder where you have your html file.

3) I have only test on a limited number of directories.

I tested this on MacOS 10.2.6 with Netscape 7.1 and IE 5.2.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Read host directory</TITLE>

<SCRIPT type="text/javascript">

var parentDirectoryFound = false;
var tagNamePrior = "";
var tagNameCurrent = "";

var directoryList = [ ];
var fileList = [ ];

function findDirectoryItems(n)
{

if (n.nodeType == 3 &&
n.parentNode.tagName != "SCRIPT") // Is n a Text object?
{

var theParent = n.parentNode;

if (parentDirectoryFound == true &&
tagNamePrior =="PRE" &&
tagNameCurrent == "A")
{

// Is it a directroy or a file?
if (n.data.lastIndexOf("/") >= 0)
{
directoryList[directoryList.length] = n.data;
}
else
{
fileList[fileList.length] = n.data;
}
}

if (n.data == "Parent Directory")
{
parentDirectoryFound = true;
}

return;
}

// Otherwise, n may have children
// Note the recursive call
var numChars = 0;
for (var m = n.firstChild; m != null; m = m.nextSibling)
{
tagNamePrior = tagNameCurrent;
tagNameCurrent = n.tagName;

findDirectoryItems(m);
} return;
}

function displayDirectoryItems()
{
alert("In displayDirectoryItems, now find directory items.");
findDirectoryItems(window.frames["webdirectory"].document.body);
directoryList.sort();
fileList.sort();
var stringDirectoryList = directoryList.join();
var stringFileList = fileList.join();
alert("stringDirectoryList = " + stringDirectoryList);
alert("stringFileList = " + stringFileList);
}

</script>

</HEAD>

<BODY onload='
if (parentDirectoryFound == false)
{
alert("Iframe onload did not run. Try now.");
displayDirectoryItems();
};'>

<p><br>Let's see if we can find the directory items.</p>

<!--

- Add the style tag below to hide this iframe.

style="visibility: hidden; width: 1px; height: 1px;"

- Best to put the iframe at the end of the document, since
it does take a little real space on the page.

- The onload in an iframe seems to be a recent addition to html.

-->

<iframe
name="webdirectory"
height=450
width=600
src="http://www.apache.org/dist/httpd/"
onload="displayDirectoryItems();">
</iframe>

</BODY>

</HTML>
Robert
Jul 23 '05 #3

P: n/a

"Noorul Ameen" <no**********@gmail.com> wrote in message
news:bd**************************@posting.google.c om...
Dear Folks,

In a HTML page I want to get all the files in a selected dirs. Is
that any way to do this thru javascript.
If you want to read files on the client machine you can do it in IE with the
fileSystemObject
google with this 'msdn fso' or try the link below
http://msdn.microsoft.com/library/de...us/script56/ht
ml/FSOoriFileSystemObject.asp

If you want to read files from the server you need server side javascript or
some kind of cgi script to handle a request.
If you can code in c I would suggest using PHP for cross platform since many
of its internal functions are taken from the c syntax or you could write an
executable to run on Winduhs Servers. If you know VB ASP is a viable
solution for IE.
Help will be highly appreciated. Thanks in advance.
Hope this helps
Jimbo
Regards,
Ameen T

Jul 23 '05 #4

P: n/a
On Thu, 16 Sep 2004 03:53:01 GMT, Robert wrote:
- Add the style tag below to hide this iframe.

style="visibility: hidden; width: 1px; height: 1px;"


Try..
style="display: none;"

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05 #5

P: n/a
JRS: In article <rc*****************************@news1.west.earthl ink.n
et>, dated Thu, 16 Sep 2004 03:53:01, seen in news:comp.lang.javascript,
Robert <rc*******@my-deja.com> posted :
Some people want to display a random graphic image on a page. They
could place all the graphic images in a directory without an index.html
file and use the code below to get the list of graphic images to pick
from.
Number the files consecutively from X0.gif to, say, X43.gif; count them
(44); then in the code use
var ImageName = "X" + Random(44) + ".gif"

if (parentDirectoryFound == true &&


== true is superfluous & betrays an insufficient
understanding of Boolean logic.
If you've not read the newsgroup FAQ, do so first.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.