# How can I count the number of files in a directory?

**URL:** <https://discourse.processing.org/t/how-can-i-count-the-number-of-files-in-a-directory/28377>\
**Category:** Beginners\
**Created:** [March 9, 2021, 11:29pm UTC](https://discourse.processing.org/t/how-can-i-count-the-number-of-files-in-a-directory/28377 "2021-03-09T23:29:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LiterallyANoodle](https://avatars.discourse-cdn.com/v4/letter/l/df705f/32.png) [@LiterallyANoodle](https://discourse.processing.org/u/LiterallyANoodle)\
**Post date:** [March 9, 2021, 11:29pm UTC](https://discourse.processing.org/t/how-can-i-count-the-number-of-files-in-a-directory/28377/1 "2021-03-09T23:29:14Z")

</div>

I’m working on a little side-project where it is necessary for me to preload several images as an array before the rest of the program runs. When this program is run in context, I won’t have any way of knowing how many images are being fed into the array, so I setup the loading in a for loop.

The problem is, I do not know how many images could be in the folder, so I need a way of counting the number of files in the directory. I also won’t be able to know the names of the images, so the code needs to figure out the names of the files.

My current code only can handle a set number of images using a simple naming convention:

```auto
// create variables to hold images and songs
var img = [];

// preload the necessary assets
function preload() {

	// preload all the images as an array
	for (i = 0; i < 4; i++) {
		img[i] = loadImage('assets/poss/poss' + i + '.png');
    }

}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [March 9, 2021, 11:33pm UTC](https://discourse.processing.org/t/how-can-i-count-the-number-of-files-in-a-directory/28377/2 "2021-03-09T23:33:43Z")

</div>

```auto
public File[] listFiles(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    File[] files = file.listFiles();
    return files;
  } else {
    // If it's not a directory
    return null;
  }
};

```

[https://processing.org/examples/directorylist.html](https://processing.org/examples/directorylist.html)

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 10, 2021, 10:51am UTC](https://discourse.processing.org/t/how-can-i-count-the-number-of-files-in-a-directory/28377/3 "2021-03-10T10:51:10Z")

</div>

I think the original question is in p5.js while this solution is for processing. Unfortunately p5.js is “front-end” which does not have access to folders. You may need to use tools like node.js or electron to read local files, or create a metadata in advance, let’s say a json file that has a list of files in the folder, so that the p5.js script first loads the json and then look for all the files from the list.
