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 
//
4 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

3 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.

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

3 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

Did you ever had to write 9 nested for-loops?

for (int i0 = 0; i0 < 2; i0++) { 
    for (int i1 = 0; i1 < 2; i1++) { 
      for (int i2 = 0; i2 < 2; i2++) { 
         ....
            ......

I wrote another Sketch to write these 9 lines with for-loops for me, so I don’t have to do it.

You can copy paste the auto-generated code from the direct window in processing:


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 ( "}");
  }
  println ("");

  exit();
} // func 
//

Similarly, I was needing a list of 3D-PVectors that point in all 3D-directions.

I received help from the forum here.

 PVector[] pvList = 
    {  
    // new PVector ( 0,  0,  0),   // This can be removed
    new PVector ( 0, 0, 1), // forward
    new PVector ( 0, 0, -1), // reverse
    new PVector ( 0, 1, 0), 
    new PVector ( 0, 1, 1), 
    new PVector ( 0, 1, -1), 
     .....
     ......

So I wrote a Sketch to generate the list.

You have to copy the result list from the direct window of the processing IDE and then replace 2 with -1 by the processing Find-function, ctrl-f:

size(300, 300); 

println("PVector[] pvList1 = "); 
println("    {"  );

for (int i0 = 0; i0 < 3; i0++) { 
  for (int i1 = 0; i1 < 3; i1++) { 
    for (int i2 = 0; i2 < 3; i2++) { 
      println(
        "        new PVector ( "  
        + str(i0)+", "
        +str(i1)+", "
        +str(i2)
        +" ),");
    }//for
  }
}

println("};");

The List

Here is the result:


PVector[] pvList = 
    {  
    // new PVector ( 0,  0,  0),   // This can be removed
    new PVector ( 0, 0, 1), // forward
    new PVector ( 0, 0, -1), // reverse
    new PVector ( 0, 1, 0), 
    new PVector ( 0, 1, 1), 
    new PVector ( 0, 1, -1), 
    new PVector ( 0, -1, 0), 
    new PVector ( 0, -1, 1), 
    new PVector ( 0, -1, -1), 

    new PVector ( 1, 0, 0), 
    new PVector ( 1, 0, 1), 
    new PVector ( 1, 0, -1), 
    new PVector ( 1, 1, 0), 
    new PVector ( 1, 1, 1), 
    new PVector ( 1, 1, -1), 
    new PVector ( 1, -1, 0), 
    new PVector ( 1, -1, 1), 
    new PVector ( 1, -1, -1), 

    new PVector ( -1, 0, 0), 
    new PVector ( -1, 0, 1), 
    new PVector ( -1, 0, -1), 
    new PVector ( -1, 1, 0), 
    new PVector ( -1, 1, 1), 
    new PVector ( -1, 1, -1), 
    new PVector ( -1, -1, 0), 
    new PVector ( -1, -1, 1), 
    new PVector ( -1, -1, -1), 
  };

3D-graphic

And a 3D-graphic to demonstrate the use of the list.

The lines pointing outwards from the center are the PVectors:


float angle1; 

// auto-generated list of 3D PVectors for all possible directions in 3D (right, left, up, diagonal right downwards....)
PVector[] pvList1 = 
  {
  //  new PVector ( 0, 0, 0 ), 
  new PVector ( 0, 0, 1 ), 
  new PVector ( 0, 0, -1 ), 
  new PVector ( 0, 1, 0 ), 
  new PVector ( 0, 1, 1 ), 
  new PVector ( 0, 1, -1 ), 
  new PVector ( 0, -1, 0 ), 
  new PVector ( 0, -1, 1 ), 
  new PVector ( 0, -1, -1 ), 
  new PVector ( 1, 0, 0 ), 
  new PVector ( 1, 0, 1 ), 
  new PVector ( 1, 0, -1 ), 
  new PVector ( 1, 1, 0 ), 
  new PVector ( 1, 1, 1 ), 
  new PVector ( 1, 1, -1 ), 
  new PVector ( 1, -1, 0 ), 
  new PVector ( 1, -1, 1 ), 
  new PVector ( 1, -1, -1 ), 
  new PVector ( -1, 0, 0 ), 
  new PVector ( -1, 0, 1 ), 
  new PVector ( -1, 0, -1 ), 
  new PVector ( -1, 1, 0 ), 
  new PVector ( -1, 1, 1 ), 
  new PVector ( -1, 1, -1 ), 
  new PVector ( -1, -1, 0 ), 
  new PVector ( -1, -1, 1 ), 
  new PVector ( -1, -1, -1 ) 
};

void setup() {
  // init
  size(800, 600, P3D);
} // func 

void draw() {
  // runs on and on 
  background(0);
  lights();

  stroke(255, 0, 0);
  strokeWeight(8);

  pushMatrix();
  translate(width/2, height/2, 0);
  rotateX(-0.2992);
  rotateY(angle1); 

  int i=0; 
  for (PVector pv : pvList1) {
    float amt = map(i, 
      0, pvList1.length, 
      0, 1);
    stroke(lerpColor(color(255), color(255, 0, 0), amt));
    line(
      //0, 0, 0, 
      pv.x*8, pv.y*8, pv.z*8, 
      pv.x*88, pv.y*88, pv.z*88);
    i++;
  }//for
  popMatrix();
  //

  stroke(100); 
  //  noFill();
  fill(255, 0, 0);

  pushMatrix();
  translate(100, 100, 0);
  rotateZ(angle1);
  if (!keyPressed)
    angle1+=.04; 
  box(60);
  popMatrix();

  pushMatrix();
  translate(400, 100, 0); 
  rotateX(angle1);
  fill(0, 0, 255);
  box(60);
  popMatrix();

  pushMatrix();
  translate(700, 100, 0);
  rotateY(angle1);
  box(60);
  popMatrix();

  pushMatrix();
  translate(700, 300, -330);
  noStroke(); 
  sphere(50);
  popMatrix();
} // func 
//
2 Likes

I made another Tool or Meta Tool

Here you can grab (using the mouse) a color from an existing image and get the color code

Chrisir

PImage sourceImage; 
color colorGlobal  =  color(209.0, 139.0, 70.0); 
String colorText = ""; 

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

void setup() {
  size(1470, 770);

  sourceImage=loadImage("chess.jpg");  // whatever 
}

void draw() {
  background(111); 

  // show image 
  image(sourceImage, 0, 0);

  // show control rect 
  fill(colorGlobal);
  stroke(0); 
  rect(sourceImage.width+0, 110, 
    60, 60);

  // show help
  fill(0); 
  text("This Sketch lets you receive a color from a given image.\nClick with the mouse a color in the image.\n"
    +colorText, 
    sourceImage.width+40, 210);
}

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

void mousePressed() {
  color coloFromMouse = 
    sourceImage.get(mouseX, mouseY);

  colorText = " = color( " 
    + int(red(coloFromMouse))
    +", "
    +int(green(coloFromMouse))
    +", "+
    int(blue(coloFromMouse))
    +" );"; 
  println(colorText);
  colorGlobal = color( red(coloFromMouse), green(coloFromMouse), blue(coloFromMouse) );
}
//

1 Like

If you need to write 9 nested for-loops you’ve got bigger problems than simply typing them out!:sweat_smile: