# How to get a folder fileList?

**URL:** <https://discourse.processing.org/t/how-to-get-a-folder-filelist/913>\
**Category:** p5.js\
**Created:** [June 13, 2018, 8:10am UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913 "2018-06-13T08:10:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![erkosone](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erkosone](https://discourse.processing.org/u/erkosone)\
**Post date:** [June 13, 2018, 8:10am UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/1 "2018-06-13T08:10:07Z")

</div>

Hi!

I try to get a folder filelist for load images in batch with function… but don´t work…

Any suggestion?

```auto
function getDirectories(path) {
    var fs = require('fs');
    var files = fs.readdirSync(path);
    console.log(files[0]);
}

```

I´m noob on javaScript, i dont know require().

---

<div class="post-metadata">

**Author:** ![lmccart](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lmccart/32/12_2.png) [@lmccart](https://discourse.processing.org/u/lmccart)\
**Post date:** [June 13, 2018, 3:28pm UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/2 "2018-06-13T15:28:05Z")

</div>

p5.js is a frontend library. That is, it just controls what you can see and interact with in the browser, and doesn’t have access directly to the filesystem. The code you posted is for use with backend code, like a [node.js](https://medium.freecodecamp.org/what-exactly-is-node-js-ae36e97449f5) server. It is possible for frontend code and backend code to communicate with each other, but you won’t be able to run this code directly from your p5.js sketch. More on frontend vs backend code [here](http://blog.teamtreehouse.com/i-dont-speak-your-language-frontend-vs-backend).

To answer your question directly, from the p5.js sketch itself, there is not a way to list the files in your filesystem or folder.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 13, 2018, 11:26pm UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/3 "2018-06-13T23:26:50Z")

</div>

If you are working in the back end, then let us know. For demonstration purposes (I haven’t tried this) you should be able to run a local host in your computer, then you use js to access/inquire your local server to get this info for you. As I said, this is good for demonstration purposes but it might need to be reconsider based on your final goal.

For local servers, check [this](https://github.com/processing/p5.js/wiki/Local-server) link.

Kf

---

<div class="post-metadata">

**Author:** ![bmoren](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bmoren/32/68_2.png) [@bmoren](https://discourse.processing.org/u/bmoren)\
**Post date:** [June 14, 2018, 3:48am UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/4 "2018-06-14T03:48:47Z")

</div>

depending on what you are trying to do there are some ways to kind of get around having to do this on the back end with having a user select a file with a file picker before using the file in JS. This way it’s on user choice and not penetrating the secure walls of the browser: [https://developer.mozilla.org/en-US/docs/Web/API/File/Using\_files\_from\_web\_applications](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications)

---

<div class="post-metadata">

**Author:** ![erkosone](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erkosone](https://discourse.processing.org/u/erkosone)\
**Post date:** [June 14, 2018, 5:05am UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/5 "2018-06-14T05:05:48Z")

</div>

Hello, first of all thanks for the help, what I’m trying to do is a function that loads a folder of images.

The idea is not to directly use the loadImage function (file), it is better to use the new function loadImages (  
path), where a location relative to the script is passed as parameter, for example “/ data / images /” and the function is responsible for generating a list of files and loading them into an image matrix.

An example of this has already been created for java processing:

```auto
PImage[] loadImages(String nameFolder) {
    java.io.File folder = new java.io.File(dataPath("")+"/"+nameFolder);
    String[] filenames = folder.list();
    PImage[] img = new PImage[filenames.length];
    for (int i = 0; i < filenames.length; i++) {
        img[i] = loadImage(dataPath("") + "/" + nameFolder + "/" + filenames[i]);
    }
    return img;
}

```

And this is the same thing for Android mode:

i

```auto
mport android.content.Context;
import android.app.Activity;
import android.content.res.AssetManager;
//------------------------------------------------------------
PImage[] loadImages(String folderName_){
  Activity act= this.getActivity();
  AssetManager assetManager = act.getAssets();
  PImage img_[] = null;
  try{
    String[] imgPath = assetManager.list(folderName_);
    img_ = new PImage[imgPath.length];
     for(int i=0; i<imgPath.length; i++){
     img_[i] = loadImage(folderName_ + "/" + imgPath[i]);
    }
  }
  catch(IOException ex){
       System.out.println (ex.toString());   
  }
  return img_;
}

```

and now I’m trying to do the same in p5.

---

<div class="post-metadata">

**Author:** ![bmoren](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bmoren/32/68_2.png) [@bmoren](https://discourse.processing.org/u/bmoren)\
**Post date:** [June 14, 2018, 1:41pm UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/6 "2018-06-14T13:41:19Z")

</div>

> [@erkosone](#):
>
> The idea is not to directly use the loadImage function (file), it is better to use the new function loadImages (  
> path)

If you are unable to use the file picker in the HTML5 spec, you’ll have to use node.js and fs.readdir which run on the backend and can have access to the filesystem. check out @lmccart’s post above for all the details.

---

<div class="post-metadata">

**Author:** ![erkosone](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erkosone](https://discourse.processing.org/u/erkosone)\
**Post date:** [June 24, 2018, 5:16am UTC](https://discourse.processing.org/t/how-to-get-a-folder-filelist/913/7 "2018-06-24T05:16:38Z")

</div>

Thank´s! i see the info with calm.
