# Is the a "load all text files in a directory" (\*.txt) function in processing?

**URL:** <https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507>\
**Category:** Coding Questions\
**Created:** [May 5, 2020, 2:16pm UTC](https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507 "2020-05-05T14:16:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [May 5, 2020, 2:16pm UTC](https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507/1 "2020-05-05T14:16:58Z")

</div>

Hello All,

I have a more general question. I have a data analysis program I have been working on that takes in .txt files using loadStrings(). Before I run the program I am manually reformatting the names of the .txt files so loadStrings() can point to a specific filename. I would like to program some code so all .txt files are loaded in without needing to have a specific file location.

Really simple example:

“This is the full file name and I only want to get JUST THIS PIECE of information out of it”.txt

I have about 70 .txt files with names like the one above and I would eventually like to write some code so Processing can extra information from the filename itself. Is there a way I could load all text files, like a \*.txt command, from a particular directory?

Thank you for the help.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2020, 2:32pm UTC](https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507/2 "2020-05-05T14:32:41Z")

</div>

here is a Sketch

results are println’ed

Chrisir

```auto

// https://discourse.processing.org/t/tool-for-you-search-file-name/10517

// user selects folder

/*
It searches a directory and all subfolders recursively:
 
 you can alter certain values.
 
 It does only check the file name, not the content!!!!!!!!!!!!!!!!!!!!
 
 At the moment it searches: ee, non-case-sensitive, NON recursively, only .txt-files
 
 */

// **************************************************************
// alter these values:
String searchPhrase="ee"; // search this 
boolean ALL_UPPER_CASE = true; // not case-sensitive 
boolean CHECK_ONLY_FILES_CONTAINING = true; // we can search for a certain file type only e.g. ".pde"
String ALLOWED_ENDING = ".txt"; // e.g. ".pde" when CHECK_ONLY_FILES_CONTAINING = true 
boolean SEARCH_RECURSIVE=false; // search sub-folders
// **************************************************************

//
//states 
final int stateWaitForFolder = 0; // consts  
final int stateDone = 1;
final int stateBreak = 2;  
int state = stateWaitForFolder; // current

String folderGlobal = "";  

String result = "Searching : "
  +searchPhrase 
  + ":\n"; 

boolean firstTime=true; 

void setup() {
  size (1100, 700); 
  background(111);
  selectFolder("Select a folder (Cancel to abort):", 
    "folderSelected");

  if (ALL_UPPER_CASE)
    searchPhrase=searchPhrase.toUpperCase();
}

void draw() {
  // 
  switch (state) {

  case stateWaitForFolder:

    // waiting until folder has been selected

    background(111);
    text("Please choose a folder. All sub-folders in the folder will be searched (or not, it depends). Hit Cancel to abort.", 
      33, 33);    

    // THE WAIT IS OVER 
    if (!folderGlobal.equals("")) {
      searchFolder( folderGlobal, 0 ); 
      //
    } // if 
    break;

  case stateDone:
    background(111);
    text("DONE: " + folderGlobal, 
      33, 33);
    showButtons();
    if (firstTime) {
      println (result+". <<<");
      firstTime=false;
    }
    break;

  case stateBreak:
    background(111);
    text("Window was closed or the user hit cancel.", 33, 33); 
    showButtons(); 
    break; 

  default:
    // error
    println ("error 91"); 
    exit();
    break;
    //
  } // switch
} // func 

// -----------------------------------------------------------------------------

void searchFolder(String folderLocal, int depth) {
  // recursive 
  // println (folderLocal);
  String[] strList; 

  // https://docs.oracle.com/javase/7/docs/api/java/io/File.html 

  File f = dataFile(folderLocal); 
  strList = f.list();

  if (strList!=null) {
    // println ("files in total: " + strList.length);
    int i = 0;
    for (File f1 : f.listFiles()) {
      if (!f1.isFile()) {
        // println ( spaceSigns(depth) + "\"" + f1.getName() + "\"," );
        if (f1.isDirectory()) {
          // println("call "+depth);
          // recursive 
          if (SEARCH_RECURSIVE)
            searchFolder(f1.getAbsolutePath(), depth+1);
        }
      }//if
      else {
        // is a file 
        if ( checkFileName(f1.getName()) ) {
          if ( getFileName(f1.getName()).contains(searchPhrase)) {
            result+= f1.getName() + " in " + folderLocal + "\n";
          }
        }
      }
    } // for
    // println("");
    state = stateDone; // next state
  }//if
  else {
    println("strList is empty");
  }
}//func

String spaceSigns(int depth) {
  // makes indents 
  String newS="";
  for (int i=0; i<depth*4; i++)
    newS+=" ";
  return newS;
}//func

boolean checkFileName(String fileName) {
  // depending on CHECK_ONLY_FILES_CONTAINING it returns true or it returns whether the file name contains an file ending ALLOWED_ENDING
  if (CHECK_ONLY_FILES_CONTAINING)
    return fileName.contains(ALLOWED_ENDING); 
  else 
  return true;
}//func 

String getFileName(String fileName) {
  // acts depending on ALL_UPPER_CASE
  if (ALL_UPPER_CASE) 
    return fileName.toUpperCase(); 
  else 
  return fileName;
}// func

// -----------------------------------------------------------------
// file handling 

void folderSelected(File selection) {
  // the 'callback' function.
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
    state=stateBreak;
  } else {
    println("User selected " + selection.getAbsolutePath());
    folderGlobal = selection.getAbsolutePath();
  } // else
}//func 

// ----------------------------------------------------------------
// Input 

void mousePressed() {
  if (state==stateDone||state==stateBreak) {
    if (dist(mouseX, mouseY, 133, 133) < 66) {
      // reset / restart
      folderGlobal=""; 
      selectFolder("Select a folder to rename files (Cancel to abort):", 
        "folderSelected");
      state=stateWaitForFolder;
    }//if
    else if (dist(mouseX, mouseY, 233, 133) < 66) {
      exit();
    }//else if
  }//if
}//func 

//-------------------------------------------------------------------
//Tools

void showButtons() {
  // set mode for text and rect 
  textAlign(CENTER, CENTER);
  rectMode(CENTER);

  // rects 
  noFill();  
  rect(133-5, 133+3, 84, 23);
  rect(233-1, 133+3, 84, 23);

  // text 
  fill(255); 
  text ( "Next folder", 133, 133);
  text ( "Quit", 233, 133);

  // reset 
  textAlign(LEFT);
  rectMode(CORNER); // The default mode is rectMode(CORNER)
}//func 
//

```

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [May 5, 2020, 2:34pm UTC](https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507/3 "2020-05-05T14:34:37Z")

</div>

Take a look at the [File class](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html). Contrary to it’s name it can also hold information about a folder/directory.

The `listFiles()` method returns an array of files that are in that folder. Here’s a quick example:

```auto
void setup() {
  printAllFileNames();
}

void printAllFileNames() {
  // savePath("") returns the path to the data-folder of the sketch
  File dataFolder = new File(savePath(""));

  // This is called an "enhanced for-loop". It is basically the equivalent of saying:
  // File[] files = dataFolder.listFiles();
  // for(int i = 0; i < files.length; i++) {
  // File file = files[i];
  // ...
  // }
  for (File file : dataFolder.listFiles()) {
    if(file.getName().endsWith(".txt")) {
      println(file.getName());
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [May 9, 2020, 12:34pm UTC](https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507/4 "2020-05-09T12:34:24Z")

</div>

Thank you very much Chrisir, honestly this is more than I was hoping for. I have also been trying to get a really basic user interface going and this code will help a lot.

Thanks again

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [May 9, 2020, 12:36pm UTC](https://discourse.processing.org/t/is-the-a-load-all-text-files-in-a-directory-txt-function-in-processing/20507/5 "2020-05-09T12:36:50Z")

</div>

Thank you Schred, this is very helpful. I thought there would be a function that can just pull this information that already exists in Processing but I was having trouble finding it. Much obliged.
