Exporting code/copying code/ code management

new version

  • with a line of **************** between the single pde-files and
  • the main file (pde with setup and draw that has the same name as the folder) is listed first.
  • Starts notepad with the resulting file.

Chrisir

// JOIN all pde in a folder

// user selects folder

//**************************************************************
// alter these values:
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 
//**************************************************************

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

String folderGlobal = "";  

final String stars = "********************************************************************************";

int linesInTotal=0; 

// result 
String[] resultingStringArray = {}; 

String FileWithFolderName=""; 

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

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

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

  case stateWaitForFolder:
    // waiting until folder has been selected
    background(111);
    text("Please choose a folder. Hit Cancel to abort.", 
      33, 33);    

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

  case stateDone:
    background(111);
    text("DONE: " + folderGlobal, 
      33, 33);
    showButtons();
    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) {
  //  
  // println (folderLocal);

  String[] strList;

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

  resultingStringArray = new String[0]; 
  resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;
  resultingStringArray = (String[]) append ( resultingStringArray, "// "+stars ) ;
  resultingStringArray = (String[]) append ( resultingStringArray, "//         joined pde-file of folder " + folderLocal ) ;
  resultingStringArray = (String[]) append ( resultingStringArray, "// "+stars ) ;
  resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;

  // first handle only the file which name is the name of the folder (in processing that's the main file)
  findFileWithFolderName(folderLocal);

  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()) {
        // is a file 
        if ( checkFileName(f1.getName()) && !FileWithFolderName.equals(f1.getName()) ) {
          // is pde 

          // write intro for that file / tab 
          resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;
          resultingStringArray = (String[]) append ( resultingStringArray, "// "+stars ) ;
          resultingStringArray = (String[]) append ( resultingStringArray, "// tab: " + f1.getName() ) ;
          resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;

          // write content
          String[] temp1 = loadStrings(f1.getAbsolutePath());
          linesInTotal += temp1.length; 
          resultingStringArray = (String[]) concat ( resultingStringArray, temp1 ) ;
        }
      }
    } // for

    resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;
    resultingStringArray = (String[]) append ( resultingStringArray, "// End of joined file. "+stars ) ;
    resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;
    //println("");
    println("Lines in total: "
      +linesInTotal);
    String fileNameTarget="result"+timeStamp()+".txt";
    saveStrings(fileNameTarget, 
      resultingStringArray);

    // open  
    openInNotepad(sketchPath("")+fileNameTarget); 

    state = stateDone; // next state
  }//if
}//func

void findFileWithFolderName(String folderLocal) {

  // first handle only the file which name is the name of the folder (in processing that's the main file)

  String[] strList;

  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()) {
        // is a file 
        if ( checkFileName(f1.getName()) &&  f1.getName().equals(f.getName()+ALLOWED_ENDING) ) {
          // is pde 

          println("findFileWithFolderName says "
            + f1.getName());

          // write intro for that file / tab
          resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;
          resultingStringArray = (String[]) append ( resultingStringArray, "// "+stars ) ;
          resultingStringArray = (String[]) append ( resultingStringArray, "// tab: " + f1.getName() +" main file (file name is folder name)") ;
          resultingStringArray = (String[]) append ( resultingStringArray, "" ) ;

          // write content
          String[] temp1 = loadStrings(f1.getAbsolutePath());
          linesInTotal = temp1.length; 
          resultingStringArray = (String[]) concat ( resultingStringArray, temp1 ) ;

          // store name of the FileWithFolderName globally and leave here
          FileWithFolderName = f1.getName(); 
          return;
        }
      }
    } // for
  }
}

boolean checkFileName(String fileName) {
  // depending on CHECK_ONLY_FILES_CONTAINING it returns true or it returns whether the file name contains a file ending ALLOWED_ENDING
  if (CHECK_ONLY_FILES_CONTAINING)
    return fileName.contains(ALLOWED_ENDING); 
  else 
  return true;
}//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=""; 
      FileWithFolderName=""; 
      linesInTotal=0; 
      resultingStringArray = null;
      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 

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

String timeStamp() {
  return     todaysDate () 
    + "_" 
    + timeNow () ;
}

String todaysDate () {

  int d = day();    // Values from 1 - 31
  int m = month();  // Values from 1 - 12
  int y = year();   // 2003, 2004, 2005, etc.

  String Result = 
    nf(y, 2)+
    nf(m, 2)+
    nf(d, 2); 
  return( Result );
}

String timeNow () {

  int h = hour();    // Values from 1 - 23
  int m = minute();  // Values from 1 - 60
  int s = second();   // 1-60

  String Result = nf(h, 2) + "" + 
    nf(m, 2) + "" + 
    nf(s, 2); 
  return( Result );
}

// ---------------------------------------------------------
// open in notepad

void openInNotepad(String currentFile) {
  // open in notepad

  if (currentFile.equals("")) {
    //return; // leave here (not necessary)
  }

  try
  {
    ProcessBuilder pb = new ProcessBuilder("notepad.exe", currentFile);
    Process p = pb.start();
  }
  catch ( Exception /* IOException, URISyntaxException */ e )
  {
    e.printStackTrace();
  }//catch
}
//
2 Likes