# dataPath("") only works in "data" folder

**URL:** https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310
**Category:** Coding Questions
**Created:** [June 27, 2018, 4:18pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310 "2018-06-27T16:18:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![grumo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumo/32/604_2.png) [@grumo](https://discourse.processing.org/u/grumo)
#### Post date: [June 27, 2018, 4:18pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310/1 "2018-06-27T16:18:37Z")

</div>

I use this script for reading and filtering files by extension in a folder. The thing is that it only works when reading “/data” if I do “/data/something” or “/something” it won’t work. Is driving me crazy. I’m working in OS X. Any help will be greatly appreciated.

Thanks!

///////

String[] files(){  
// The data path of the folder to look in (write your own)  
java.io.File folder = new java.io.File(dataPath(""));

// let’s set a filter (which returns true if file’s extension is .mp4)  
java.io.FilenameFilter pngFilter = new java.io.FilenameFilter() {  
public boolean accept(File dir, String name) {  
return name.toLowerCase().endsWith(".mp4");  
}  
};

// list the files in the data folder, passing the filter as parameter  
String[] filenames = folder.list(pngFilter);  
return(filenames);  
}

---

<div class="post-metadata">

### Author: ![Bird](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bird/32/118_2.png) [@Bird](https://discourse.processing.org/u/Bird)
#### Post date: [June 27, 2018, 4:58pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310/2 "2018-06-27T16:58:16Z")

</div>

dataPath("") specifically returns the data folder in the sketch, so that is what the function you have there will do. What is it you are doing to try to point to a different folder?

---

<div class="post-metadata">

### Author: ![grumo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumo/32/604_2.png) [@grumo](https://discourse.processing.org/u/grumo)
#### Post date: [June 27, 2018, 5:12pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310/3 "2018-06-27T17:12:02Z")

</div>

It works as dataPath("") (reading the data folder) but using dataPath(“something”) won’t work…

---

<div class="post-metadata">

### Author: ![REAS](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reas/32/16_2.png) [@REAS](https://discourse.processing.org/u/REAS)
#### Post date: [June 27, 2018, 5:21pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310/4 "2018-06-27T17:21:24Z")

</div>

The “DirectoryList” example, distributed with Processing, might help.

```auto
/**
 * Listing files in directories and subdirectories
 * by Daniel Shiffman.  
 * 
 * This example has three functions:<br />
 * 1) List the names of files in a directory<br />
 * 2) List the names along with metadata (size, lastModified)<br /> 
 * of files in a directory<br />
 * 3) List the names along with metadata (size, lastModified)<br />
 * of files in a directory and all subdirectories (using recursion) 
 */

import java.util.Date;

void setup() {

  // Using just the path of this sketch to demonstrate,
  // but you can list any directory you like.
  String path = sketchPath();

  println("Listing all filenames in a directory: ");
  String[] filenames = listFileNames(path);
  printArray(filenames);

  println("\nListing info about all files in a directory: ");
  File[] files = listFiles(path);
  for (int i = 0; i < files.length; i++) {
    File f = files[i];    
    println("Name: " + f.getName());
    println("Is directory: " + f.isDirectory());
    println("Size: " + f.length());
    String lastModified = new Date(f.lastModified()).toString();
    println("Last Modified: " + lastModified);
    println("-----------------------");
  }

  println("\nListing info about all files in a directory and all subdirectories: ");
  ArrayList<File> allFiles = listFilesRecursive(path);

  for (File f : allFiles) {
    println("Name: " + f.getName());
    println("Full path: " + f.getAbsolutePath());
    println("Is directory: " + f.isDirectory());
    println("Size: " + f.length());
    String lastModified = new Date(f.lastModified()).toString();
    println("Last Modified: " + lastModified);
    println("-----------------------");
  }

  noLoop();
}

// Nothing is drawn in this program and the draw() doesn't loop because
// of the noLoop() in setup()
void draw() {
}

// This function returns all the files in a directory as an array of Strings  
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
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;
  }
}

// Function to get a list of all files in a directory and all subdirectories
ArrayList<File> listFilesRecursive(String dir) {
  ArrayList<File> fileList = new ArrayList<File>(); 
  recurseDir(fileList, dir);
  return fileList;
}

// Recursive function to traverse subdirectories
void recurseDir(ArrayList<File> a, String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    // If you want to include directories in the list
    a.add(file);  
    File[] subfiles = file.listFiles();
    for (int i = 0; i < subfiles.length; i++) {
      // Call this function on all files in this directory
      recurseDir(a, subfiles[i].getAbsolutePath());
    }
  } else {
    a.add(file);
  }
}

```

---

<div class="post-metadata">

### Author: ![grumo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumo/32/604_2.png) [@grumo](https://discourse.processing.org/u/grumo)
#### Post date: [June 27, 2018, 6:02pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310/5 "2018-06-27T18:02:03Z")

</div>

Thanks Casey!

Any ideas for the filtering by file extension? --I need only the .mp4 files listed but I can’t find any parameter --I should just read the array and choose the matches?

---

<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: [June 27, 2018, 6:13pm UTC](https://discourse.processing.org/t/datapath-only-works-in-data-folder/1310/6 "2018-06-27T18:13:14Z")

</div>

> **[How to check if a file is exist in data folder by code?](https://Discourse.Processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/7)**
>
> Yes! Just pass an empty string "" to dataPath(): dataFolderPath = this.dataPath('') + '/' print dataFolderPath … or to dataFile(): dir = this.dataFile('') dataFolderPath = dir.path + '/' print dataFolderPath However, if you need all files within...
