# Read all files in a directory

**URL:** https://discourse.processing.org/t/read-all-files-in-a-directory/39285
**Category:** Beginners
**Created:** [October 15, 2022, 6:49pm UTC](https://discourse.processing.org/t/read-all-files-in-a-directory/39285 "2022-10-15T18:49:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![5x9x7x2x7x9](https://avatars.discourse-cdn.com/v4/letter/5/9de0a6/32.png) [@5x9x7x2x7x9](https://discourse.processing.org/u/5x9x7x2x7x9)
#### Post date: [October 15, 2022, 6:49pm UTC](https://discourse.processing.org/t/read-all-files-in-a-directory/39285/1 "2022-10-15T18:49:29Z")

</div>

How would I read all files in a directory?  
For example, I would have a bunch of images in a folder and would want to load them in.  
Or would have a bunch of levels as .txt files and would load them?

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [October 15, 2022, 7:26pm UTC](https://discourse.processing.org/t/read-all-files-in-a-directory/39285/2 "2022-10-15T19:26:30Z")

</div>

This code is based on an exerp of a library I am developing:

```auto
ArrayList<File> files=new ArrayList<File>();
void visitFile(File f) {
  try {
    if (f.isDirectory()) {
      final File[] children=f.listFiles();
      if (children!=null) {
        for (File child : children) visitFile(child);
      }
    } else files.add(f);
  }
  catch(Exception e) {
  }
}

```

This code takes a file Object and checks if it is a directory. If that is the case it will check the contents of the folder.  
You can easily create a File Object by invoking it’s constructor using the _ **absolute** _ path of the directory. You can get it from a relative Path using `sketchPath(String)`.

This doc might be useful to you:  
[File (Java Platform SE 7 ) (oracle.com)](https://docs.oracle.com/javase/7/docs/api/java/io/File.html)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 15, 2022, 7:42pm UTC](https://discourse.processing.org/t/read-all-files-in-a-directory/39285/3 "2022-10-15T19:42:28Z")

</div>

> [@5x9x7x2x7x9](#):
>
> How would I read all files in a directory?

> [@File Listing Order](https://discourse.processing.org/t/file-listing-order/7148/41):
>
> [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object[])](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object%5B%5D)) static final String PICS\_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp"; static final float FPS = .25; PImage[] images; void setup() { size(1200, 600); frameRate(FPS); imageMode(CENTER); final File dir = dataFile(""); println(dir); String[] imagePaths = {}; int imagesFound = 0; if (dir.isDirectory()) { imagePaths = listPaths(dir.getPath(), "files", "recursi…
