# File Listing Order

**URL:** <https://discourse.processing.org/t/file-listing-order/7148>\
**Category:** Coding Questions\
**Created:** [January 4, 2019, 4:43am UTC](https://discourse.processing.org/t/file-listing-order/7148 "2019-01-04T04:43:52Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 4, 2019, 9:01am UTC](https://discourse.processing.org/t/file-listing-order/7148/6 "2019-01-04T09:01:04Z")

</div>

some rearrangement

```auto
// https://discourse.processing.org/t/file-listing-order/7148
// @philspitler @Architector_4

ArrayList<File> myfiles;
String path = "/home/pi/projects/processing/sketchbook";
boolean piconly = true;

void setup() {
  println("\nListing info about all files in a directory and all subdirectories \n _filter_ piconly: "+piconly);
  myfiles = listFilesRecursive(path,piconly);
  for (File afile : myfiles) println("File: "+afile ); // afile.getAbsolutePath());
}

void draw() {
}

// Function to get a list of all files in a directory and all subdirectories // with piconly filter
ArrayList<File> listFilesRecursive(String dir,boolean piconly) {
  ArrayList<File> fileList = new ArrayList<File>(); 
  recurseDir(fileList, dir, piconly); // ( file array , root_directory , pictures only )
  return fileList;
}

// Recursive function to traverse subdirectories
void recurseDir(ArrayList<File> a, String dir, boolean piconly) {
  boolean showdirs = false;
  File file = new File(dir);
  if (file.isDirectory()) {
    if ( showdirs ) a.add(file); // only if you want to include directories in the list
    File[] subfiles = file.listFiles();
    java.util.Arrays.sort(subfiles); //Sorts all files by name
    for (int i = 0; i < subfiles.length; i++) recurseDir(a, subfiles[i].getAbsolutePath(), piconly ); // Call this function on all files in this directory
  } else {
    String fileName = file.getName();
    if ( piconly ) {
    if ( //fileName.endsWith(".other-random-formats")||
      fileName.endsWith(".gif") ||
      fileName.endsWith(".jpg") ||
      fileName.endsWith(".jpeg") ||
      fileName.endsWith(".tga") ||
      fileName.endsWith(".png") ||
      fileName.endsWith(".svg") 
      ) a.add(file); // if picture for piconly
    } else { a.add(file); } // all
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/file-listing-order/7148)._
