Getting the files in a dir

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noorul Ameen

    Getting the files in a dir

    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
  • Grant Wagner

    #2
    Re: Getting the files in a dir

    Noorul Ameen wrote:
    [color=blue]
    > In a HTML page I want to get all the files in a selected dirs. Is
    > that any way to do this thru javascript.[/color]

    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.list Directory() : null);
    if (directoryList && (len = directoryList.l ength()) > 0) {
    %>

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

    <%
    }
    %>

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • Robert

      #3
      Re: Getting the files in a dir

      In article <bd3564a0.04091 42341.20415008@ posting.google. com>,
      noorul.ameen@gm ail.com (Noorul Ameen) wrote:
      [color=blue]
      > In a HTML page I want to get all the files in a selected dirs. Is
      > that any way to do this thru javascript.[/color]

      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 parentDirectory Found = false;
      var tagNamePrior = "";
      var tagNameCurrent = "";

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

      function findDirectoryIt ems(n)
      {

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

      var theParent = n.parentNode;

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

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

      if (n.data == "Parent Directory")
      {
      parentDirectory Found = 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;

      findDirectoryIt ems(m);
      } return;
      }

      function displayDirector yItems()
      {
      alert("In displayDirector yItems, now find directory items.");
      findDirectoryIt ems(window.fram es["webdirecto ry"].document.body) ;
      directoryList.s ort();
      fileList.sort() ;
      var stringDirectory List = directoryList.j oin();
      var stringFileList = fileList.join() ;
      alert("stringDi rectoryList = " + stringDirectory List);
      alert("stringFi leList = " + stringFileList) ;
      }

      </script>

      </HEAD>

      <BODY onload='
      if (parentDirector yFound == false)
      {
      alert("Iframe onload did not run. Try now.");
      displayDirector yItems();
      };'>

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

      <!--

      - Add the style tag below to hide this iframe.

      style="visibili ty: 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="webdirect ory"
      height=450
      width=600
      src="http://www.apache.org/dist/httpd/"
      onload="display DirectoryItems( );">
      </iframe>



      </BODY>

      </HTML>


      Robert

      Comment

      • J. J. Cale

        #4
        Re: Getting the files in a dir


        "Noorul Ameen" <noorul.ameen@g mail.com> wrote in message
        news:bd3564a0.0 409142341.20415 008@posting.goo gle.com...[color=blue]
        > 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.[/color]

        If you want to read files on the client machine you can do it in IE with the
        fileSystemObjec t
        google with this 'msdn fso' or try the link below

        ml/FSOoriFileSyste mObject.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.[color=blue]
        >
        > Help will be highly appreciated. Thanks in advance.[/color]

        Hope this helps
        Jimbo[color=blue]
        >
        > Regards,
        > Ameen T[/color]


        Comment

        • Andrew Thompson

          #5
          Re: Getting the files in a dir

          On Thu, 16 Sep 2004 03:53:01 GMT, Robert wrote:
          [color=blue]
          > - Add the style tag below to hide this iframe.
          >
          > style="visibili ty: hidden; width: 1px; height: 1px;"[/color]

          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

          Comment

          • Dr John Stockton

            #6
            Re: Getting the files in a dir

            JRS: In article <rccharles-0C7ED7.23590615 092004@news1.we st.earthlink.n
            et>, dated Thu, 16 Sep 2004 03:53:01, seen in news:comp.lang. javascript,
            Robert <rccharles@my-deja.com> posted :
            [color=blue]
            >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.[/color]

            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"

            [color=blue]
            > if (parentDirector yFound == true &&[/color]

            == 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            Working...