Hello All,
I have a more general question. I have a data analysis program I have been working on that takes in .txt files using loadStrings(). Before I run the program I am manually reformatting the names of the .txt files so loadStrings() can point to a specific filename. I would like to program some code so all .txt files are loaded in without needing to have a specific file location.
Really simple example:
“This is the full file name and I only want to get JUST THIS PIECE of information out of it”.txt
I have about 70 .txt files with names like the one above and I would eventually like to write some code so Processing can extra information from the filename itself. Is there a way I could load all text files, like a *.txt command, from a particular directory?
Thank you for the help.
1 Like
here is a Sketch
results are println’ed
Chrisir
// https://discourse.processing.org/t/tool-for-you-search-file-name/10517
// user selects folder
/*
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: ee, non-case-sensitive, NON recursively, only .txt-files
*/
//**************************************************************
// alter these values:
String searchPhrase="ee"; // 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 = ".txt"; // e.g. ".pde" when CHECK_ONLY_FILES_CONTAINING = true
boolean SEARCH_RECURSIVE=false; // 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 (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 searched (or not, it depends). 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);
// recursive
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
else {
println("strList is empty");
}
}//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
//
2 Likes
Take a look at the File class. Contrary to it’s name it can also hold information about a folder/directory.
The listFiles()
method returns an array of files that are in that folder. Here’s a quick example:
void setup() {
printAllFileNames();
}
void printAllFileNames() {
// savePath("") returns the path to the data-folder of the sketch
File dataFolder = new File(savePath(""));
// This is called an "enhanced for-loop". It is basically the equivalent of saying:
// File[] files = dataFolder.listFiles();
// for(int i = 0; i < files.length; i++) {
// File file = files[i];
// ...
// }
for (File file : dataFolder.listFiles()) {
if(file.getName().endsWith(".txt")) {
println(file.getName());
}
}
}
1 Like
Thank you very much Chrisir, honestly this is more than I was hoping for. I have also been trying to get a really basic user interface going and this code will help a lot.
Thanks again
1 Like
Thank you Schred, this is very helpful. I thought there would be a function that can just pull this information that already exists in Processing but I was having trouble finding it. Much obliged.
1 Like