Tools for you: Search file name and others

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 
//
3 Likes

Another Tool (Meta Tool to work with other Sketches)

here is a sketch that joins pde files and saves as txt file

Useful when you want to post a multiple tab project in the forum.

see Exporting code/copying code/ code management

Chrisir

2 Likes

@Chrisir If you want sophisticated code management you could do a lot worse than use atom. I use it all the time with my ruby-processing projects. You can easily set it up to run scripts for you as I have with my JRubyArt plugin.

2 Likes

Another Tool (Meta Tool)

it generates for-loops, when you need several nested for loops:



void setup() {

  size(800, 600);

  int numberOfDesiredForLoops = 9; 

  for (int i = 0; i < numberOfDesiredForLoops; i++) {
    println ( " for (int i"+i+" = 0; i"+i+" < 2; i"+i+"++) { " );
  }

  println ("");
  for (int i = 0; i < numberOfDesiredForLoops; i++) {
    print ( "i"+i+",");
  }
  println ("");
  println ("");

  for (int i = 0; i < numberOfDesiredForLoops; i++) {
    println ( "} // for ");
  }
  println ("");

  exit();
} // func 
//
2 Likes

Another Tool (Metatool)

it converts lists of data without “” to with “”

// This tool (meta tool) takes csv data from a text file named "text.txt"
// and converts it into a code segment defining the content of the text file 
// as a String array in your code.
//
// Copy the String array from the Direct Window of this processing Sketch into your sketch. 
//
// Example usage: You have a Level Editor for a game. The level editor just gives out data like 
// 0,0.0,0.0,0.0,400.0,2.0,400.0,-16570625 in its direct window.
// 
// Save the data (ctrl-a, ctrl-c) and put it into a text file (ctrl-v). 
// Use this tool to convert the text file.
// Paste the result into the Game to show the new level you made in the level editor. 

// See example data below.

// (easier when you improve your export tool or when you use loadStrings in your target sketch)

size(155, 155); 

String[] list=loadStrings("text.txt");

println("String[] list=\n{"); 

for (String s1 : list) {
  println("\""
    +s1
    +"\","
    );
}//for 

println("};");


/*

 Start Content of text.txt
 
 //Export (id, pos vector in 3D, size vector in 3D, color)------------------------------
 0,0.0,0.0,0.0,400.0,2.0,400.0,-16570625
 1,-400.0,0.0,0.0,400.0,10.0,400.0,-65536
 2,-400.0,0.0,-400.0,400.0,10.0,400.0,-1
 3,0.0,0.0,-400.0,400.0,10.0,400.0,-16570625
 
 etc.
 
 RESULT: -------------------------------------- 
 
 String[] list=
 {
 "0,0.0,0.0,0.0,400.0,2.0,400.0,-16570625",
 "1,-400.0,0.0,0.0,400.0,10.0,400.0,-65536",
 "2,-400.0,0.0,-400.0,400.0,10.0,400.0,-1",
 "4,0.0,0.0,-400.0,400.0,10.0,400.0,-16570625"
 };
 
 
 */

Haha, this man is truly a coder. This is truly something you begin to notice.

2 Likes

similar to the search tool above, but this new tool does a full text search INSIDE the pde files (and does not search the file name except for .pde)

// very slow !! Checks full text content of each pde file !!! (and not really the file name)
// function fileHasSearchText() is important here
// 
// user selects folder, all sub-folders are searched

//**************************************************************
// alter these values:
String searchPhrase= "Join3DArray"; // "CHECK_ONLY_FILES_CONTAINING";  // "Lissajous";  // search this 
// this should be false!!!!! Because the full text of the file is NOT turned UPPER CASE 
boolean ALL_UPPER_CASE = false; // not case-sensitive OR case-sensitive
// This says: We only check files with ending .pde 
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 is true 
boolean SEARCH_RECURSIVE = true;    // search sub-folders
//**************************************************************

// following values not for users to change (only for programmers / if you know what you are doing)
//states 
final int stateWaitForFolder = 0;  // consts  
final int stateDone          = 1;
final int stateBreak         = 2;  
int state = stateWaitForFolder;  // current

String folderGlobal = "";  

// This String is filled with the search results 
String result = "Searching full text: "
  + 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. Depending on the flags all sub-folders in the folder will be searched (full text search). Hit Cancel to abort.", 
      33, 33);    

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

  case stateDone:
    background(111);
    text("DONE: " 
      + folderGlobal
      + ". See direct window for results. ", 
      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 

// -----------------------------------------------------------------------------
// Further core functions 

void searchFolder(String folderLocal, int depth) {
  // recursive function going over folder content
  // 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);  // recursive
        }//if
      }//if
      else {
        // It is a file 
        if ( checkFileName(f1.getName()) ) {
          // old version for check of the file name
          //  if ( getFileName(f1.getName()).contains(searchPhrase)) {
          // }
          // NEW version for check of the file full text content 
          if (fileHasSearchText(f1.getPath())) {
            result+= f1.getName() + " in " + folderLocal + "\n";
          }
        }
      }//else
    } // for
    // println("");
    state = stateDone; // next state
  }//if
}//func

boolean fileHasSearchText(String name_) {
  // Is the seatch text in the full text?  
  // println (name_); 
  String[] text1 = loadStrings(name_);
  String text2 = join (text1, "#"); 

  return
    text2.contains ( searchPhrase );
}

String spaceSignsOLD(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 criteria CHECK_ONLY_FILES_CONTAINING is true 
  if (CHECK_ONLY_FILES_CONTAINING) {
    // we check and return result
    return fileName.contains(ALLOWED_ENDING);
  } else { 
    // else we return true always 
    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() {
  // check Buttons
  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 ( "Choose folder", 133, 133);
  text ( "Quit", 233, 133);

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