Tool for you: Search file name

#1

Hello all,

today I was searching an old sketch, so I wrote a search sketch I want to share.

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: Lissajous, non-case-sensitive, recursively, only .pde-files

Chrisir

// user selects folder, all sub-folders are searched

//**************************************************************
// alter these values:
String searchPhrase="Lissajous";  // 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 = ".pde";   // e.g. ".pde" when CHECK_ONLY_FILES_CONTAINING = true 
boolean SEARCH_RECURSIVE=true;    // 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 to search sub-folders (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 listed. 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);
          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
}//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 
//
1 Like