Hello all,
I made a sketch where you can select a folder and it makes a json file with all folders recursively as children and sub-children.
Could somebody have a look and tell me whether that’s the proper way to do it?
for example, the children are just named 0,1,2…?
Thanks a lot!
Regards, Chrisir
// user selects folder, all sub-folders are listed recursively in a json object
//**************************************************************
//// alter these values:
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 = "";
DateTimeTool dateTimeTool=new DateTimeTool();
JSONObject targetFolder;
void setup() {
size (1100, 700);
//targetFolder = new JSONObject();
background(111);
selectFolder("Select a folder to search sub-folders (Cancel to abort):",
"folderSelected");
}
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("")) {
targetFolder = searchFolder( folderGlobal, 0 );
saveJSONObject(targetFolder, "data/list"+dateTimeTool.dateTimeStampShort()+".json");
//
} // if
break;
case stateDone:
background(111);
text("DONE: " + folderGlobal + " (saved list to file).",
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
// -----------------------------------------------------------------------------
JSONObject searchFolder(String folderLocal, int depth) {
// recursive
String[] strList;
// https://docs.oracle.com/javase/7/docs/api/java/io/File.html
// http://jsonviewer.stack.hu/
// https://stackoverflow.com/questions/11194287/convert-a-directory-structure-in-the-filesystem-to-json-with-node-js
File f = dataFile(folderLocal);
JSONObject myFolder = new JSONObject();
myFolder.setString("path", folderLocal);
myFolder.setString("name", f.getName());
myFolder.setString("type", "folder");
// children
JSONArray values;
values = new JSONArray();
strList = f.list();
if (strList!=null) {
int i=0;
for (File f1 : f.listFiles()) {
if (f1.isDirectory()) {
JSONObject childFolder = new JSONObject();
if (SEARCH_RECURSIVE)
childFolder = searchFolder(f1.getAbsolutePath(), depth+1);
values.setJSONObject(i, childFolder);
i++;
} //if
} // for
myFolder.setJSONArray( "children", values );
state = stateDone; // next state
} //if
return myFolder;
} //func
String spaceSigns(int depth) {
// makes indents
String newS="";
for (int i=0; i<depth*4; i++) {
newS+=" ";
}
return newS;
}//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 list folders (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
//
// ============================================================================
// minor class to get a date-time-stamp from.
class DateTimeTool {
String dateTimeStampShort() {
// short version
return getDate() + "_" + getTime();
}
String dateTimeStampLong() {
// long version
return getDateLong()
+ " at "
+ getTimeLong();
}
//-----
String getTime() {
return leadingZeros(hour())
+ leadingZeros(minute())
+ leadingZeros(second());
}
String getTimeLong() {
return leadingZeros(hour())
+":"+ leadingZeros(minute())
+":"+ leadingZeros(second());
}
String getDate() {
return leadingZeros(year())
+ leadingZeros(month())
+ leadingZeros(day());
}
String getDateLong() {
return leadingZeros(year())
+"/"+ leadingZeros(month())
+"/"+ leadingZeros(day());
}
//-----
String leadingZeros(int a) {
String Buffer;
Buffer=nf(a, 2);
return(Buffer);
}
//
}//class
//
the sketch saves:
{
"path": "C:\\Users\\Christoph\\Documents\\pTest",
"children": [
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\autumn",
"children": [],
"name": "autumn",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\spring",
"children": [],
"name": "spring",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\summer",
"children": [
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\summer\\Surf2",
"children": [],
"name": "Surf2",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\summer\\Surfing1",
"children": [],
"name": "Surfing1",
"type": "folder"
}
],
"name": "summer",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\winter",
"children": [
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\winter\\Ski",
"children": [],
"name": "Ski",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\winter\\Ski2",
"children": [],
"name": "Ski2",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\winter\\Snowboard",
"children": [
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\winter\\Snowboard\\1",
"children": [],
"name": "1",
"type": "folder"
},
{
"path": "C:\\Users\\Christoph\\Documents\\pTest\\winter\\Snowboard\\2",
"children": [],
"name": "2",
"type": "folder"
}
],
"name": "Snowboard",
"type": "folder"
}
],
"name": "winter",
"type": "folder"
}
],
"name": "pTest",
"type": "folder"
}