1

I am currently on a Chromebook using HTML, CSS, JS on repl.it, and I can't find a way to check the files in a folder.

I have a folder called objects containing a bunch of JSON files. I want the object dataQueue to hold every object, and I have got that working. But I don't want to have to specifically type out each file to make it run the function I made called getFile for every single file, as is done below in the code.

I am also trying to do this without any libraries (so I can understand everything), and also I want it to be able to run both as a site on the computer and on repl.it.

This is my current code:

//defined the dataQueue object where the object files 
//will be stored
var dataQueue={};
//a function to get files by just inputting their 
//filename and what i want to call them with in the future
function getFile(theFile,index){
  //sets to a new request
  var txtFile = new XMLHttpRequest();
  //works with any file type, gets the specified file
  txtFile.open("GET", theFile, true);
  //I don't know what this does
  txtFile.send(null);
  //runs when the file is recieved
  txtFile.onreadystatechange = function(){
    //test to make sure it is ready
    if(txtFile.readyState==4){
      //add object file to dataQueue object
      dataQueue[index]=JSON.parse(txtFile.response);
    }
  }
}

//get each file individually
getFile("objects/ship1.json","ship1");
getFile("objects/box1.json","box1");
getFile("objects/bigplatform1.json","platform1");
getFile("objects/bigbox1.json","bigbox1");

I want to be able to just add another JSON file to the objects folder and have it put in the dataQueue object without needing to add another getFile function.

I think a solution would be if I could somehow get a list of strings containing all of the filenames in the Objects folder, but I have tried finding a way to, and I couldn't find anything.

The question: How do I get each file name in a specific folder in a website's files?

nima
  • 7,796
  • 12
  • 36
  • 53
  • Well, simply ... ***YOU DON'T**, if I get your question right. Are you trying to develop on (for) the server or on (for) the client (chromebook) ? Are you trying in Chromebook to iterate over folders/files in your Google Drive, Chromebook local storage, or your website directory ? Think about this way, if a website could read your files/folders it would be a huge security risk (e.g. you go read the news and they read all your files on your disk) – azbarcea Sep 16 '21 at 13:24
  • I mean the websites files, as in like a websites files can be organised into scripts, images, CSS, and such, like at this link: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/Dealing_with_files, but I have a folder called "objects" that I want the contents of to have their names to be listed off as strings in a list. – Kitty Craft0 Sep 16 '21 at 13:26
  • The person who develops the website knows all these files. If you need an array of them, you can keep the whole folder/files hierarchy as an array in Javascript – azbarcea Sep 16 '21 at 13:28
  • If you just want to retrieve it using an AJAX call: https://stackoverflow.com/questions/34179986/how-to-retrieve-data-from-json-file-using-jquery-and-ajax – azbarcea Sep 16 '21 at 13:29
  • I am making the website here: https://replit.com/@KittyCraft0/3D-3-6#engine/2getobject.js and I want to be able to get all of the object JSONs in the "objects" folder to be put in a list of strings containing the filenames in it. I would like it to be automatic. Is there a way to have your said whole folder/files hierarchy as an array in Javascript to be automatically created? – Kitty Craft0 Sep 16 '21 at 13:30
  • Prefferably, I would like a list to be automatically created containing the filenames as strings. An example for this example would be ```["ship1.json","box1.json","bigplatform1.json","bigbox1.json"]``` – Kitty Craft0 Sep 16 '21 at 13:32
  • maybe not a good thing to do but you could make a json file with a list of all the files that are on that drive. you'd have to manually maintain that json file but then you could fetch that file and know from the content what files are inside the folder. – Jarne Kompier Sep 16 '21 at 13:52
  • @KittyCraft0: the conclusion is that it is not recommended and is technically not possible automatically, without some artificial process. The reason being is that the website code runs in the client brower and the files are on the server. One cannot iterate over files/folders from a server remotely. In case you want to know more about what tools can actually iterate over remote *files/folder* (in this process are always called resources and never files/folders), you can research about Website Crawlers (what Google actually is: a Crawler and and Indexer). – azbarcea Sep 16 '21 at 23:35
  • @azbarcea Ohh, I was under the impression for some reason that all files were sent to the browser, and then the code told it what to read. I knew that that wasn't how that worked, hence why I made that sort of loading thing in the first place, so maybe the website could load first and then all of the object JSONs could come after. I didn't put two and two together and figure that it doesn't even have the files, I was asking for them, and the website itself didn't know what files to call. But then is there a way to call a folder and all of its contents? That would be just as usefull. – Kitty Craft0 Sep 17 '21 at 13:01
  • Yes, the only think you care about is the main `index.html` and in that you have the ` – azbarcea Sep 20 '21 at 20:50

0 Answers0