0

You can see the folder structure here:

Fruit

Apple - index.html

Orange - index.html

Banana - index.html

There is a root folder Fruit and three sub-folders.

I want to put the names Orange and Banana in Apple/index.html.

Is there any possible way to get the sibling folder names with javascript?

Please help!

Tony Wu
  • 1,040
  • 18
  • 28
  • You can't do that in javascript as it is client sided, or are you using node.js without tagging it? You can do this in PHP with `scandir` but you haven't specified your server language. – online Thomas Nov 23 '15 at 14:45
  • I Think that there is no way to read sibling folders, you should write your own script for doing that... try to use fs.readdir – Hitmands Nov 23 '15 at 14:46
  • Unless you add the structure somewhere in your code, no, not clientside. But why do you ask? You usually know your own dir structure in advance. – Shilly Nov 23 '15 at 14:47
  • @Shilly What is the best way to store the sibling folder names? Is it the best way if I use array in javascript? – Tony Wu Nov 23 '15 at 14:49
  • @Thomas I am actually new to creating responsive websites. How can I get the folder names with node.js? – Tony Wu Nov 23 '15 at 14:50
  • Probably, or an object if you want to store more data about each page. But still, why would you need it? Are you dynamically creating links without knowing in advance where the links will go to? – Shilly Nov 23 '15 at 14:51
  • @Shilly Just imagine if I have thousands of sub-folders, I have to copy their names one by one....That's crazy.. – Tony Wu Nov 23 '15 at 14:57
  • Then create that list of names with some specialized tool (or plain command prompt). Is this related to an actual website? Since dynamically creating all those links could be very costly if it has to run every time a site is visited. On topic: I'd probably have the operating system print out all the names, copy/paste to a text file, replace whitespace by a comma, add a '[' and ']', you got your array of names. – Shilly Nov 23 '15 at 15:02
  • @TonyWu Node.js is javascript that runs serversided. You can roughly say it replaces PHP for example in a website. If you fully understand that means that it's not just a library for you javascript problem: http://stackoverflow.com/questions/2727167/getting-all-filenames-in-a-directory-with-node-js – online Thomas Nov 23 '15 at 15:20

1 Answers1

0

Is there any possible way to get the sibling folder names with javascript?

No. its not possible to get filesystem information with javascript

get a list of all folders in directory

You could however load these names in first with PHP (Out of your database for example) or have them stored into a JSON (Javascript Object) file you have saved on your root

Quick solution: (Have not tested personaly)

<?php    
    $filesAndDirectories = scandir("..");
    foreach($filesAndDirectories as $fileOrDirectory){
        if(is_dir($fileOrDirectory)){
            echo "<a href='http://www.siteNameHere.com/". $fileOrDirectory ."/index.php'>". $fileOrDirectory ."</a>";
        }
    }

?>
Community
  • 1
  • 1
Joost
  • 761
  • 7
  • 18