Having problems with split for directories

Using Processing 4.2, I am trying to put the last directory as the title to Processing window. From a file I have opened. To this end I am trying to use the split function. Unfortunately I can’t get it right, I have tried several different combinations but nothing seems to do the trick. This is my code, I hope some one can spot where I am going wrong.

String[] txtFile;

void setup() {
  size(400,430);
  selectInput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    String filepath = selection.getAbsolutePath();
    println("User selected " + filepath); 
    
    String[] dirictories split(filepath, char(/)); // not able to get this to work
     
    surface.setTitle(filepath);  //  put the last directory as the title of the window - not working
    // load file here
    txtFile = loadStrings(filepath);    
  }
}

The code I am opening is actually a Gcode file but I am using just the generic name of text file at the moment.
Thanks
Mike

Try replacing
String[] dirictories = split(filepath, char(/));
Note: I have added the = operator which is missing from your listing

with

String[] dirictories = filepath.split('/');

If this doesn’t work then show us the value of filepath and the elements in the dirictories array.

2 Likes

Thanks.
I tried the replacement and got a Syntax Error - Missing “;” I have been getting these a lot when clearly there is a ; at the end of the message. I also get the message:-

The function "split() expects parameters like: “split(String)”

The value of file path is taken from line 12 of the code where:-
String filepath = selection.getAbsolutePath();

I am getting this filepath fine, it is just that it shows all of the path and I am trying to get just the last directory.

It seems you’ve forgotten the assignment operator =:
String[] directories = split(filepath, '/');

If your intention is to get the filename itself, the parameter selection is of datatype File; and there’s a method just for that called getName().

There’s also method list(), which grabs all file and folder names.

1 Like

When I try this in :-

String filepath = selection.getAbsolutePath();
    println("User selected " + filepath); 
    println(getName());
 

I get the message

The function getName() does not exist.

// A dummy file to get us started
File file = new File(dataPath( "temp.txt"));
// Show absolute path
println(file.getAbsolutePath());
// Show filename
println(file.getName());
1 Like

Thanks but things are getting messy now. This is the state of my code at the moment:-

String[] txtFile;
File file = new File(dataPath( "temp.txt"));

void setup() {
  size(400,430);
  selectInput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    String filepath = selection.getAbsolutePath();
    println("User selected " + filepath); 
    println(file.getName());
    //String[] dirictories =  filepath.split('/'); // not able to get this to work
     
    surface.setTitle(filepath);  //  put the last directory as the title of the window - not working
    // load file here
    txtFile = loadStrings(filepath);    
  }
}

But the code still needs to be run to get into the else stage of the “fileSelected” part of the function.

When it is I get a whole list of stuff in the console window.

objc[21752]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fffabaad3d8) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x1318cdf50). One of the two will be used. Which one is undefined.
User selected /Users/mikecook/Documents/An_example_Cut.gcode
temp.txt

Sorry but I thing we are going backwards at the moment.

This worked for me and no splits LOL

String[] txtFile;

void setup() {
  size(640, 430);
  selectInput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    String filepath = selection.getAbsolutePath();
    println("User selected " + filepath);
    String sep = "/"; // OS specific
    int idx = filepath.lastIndexOf(sep);
    String path = idx <= 0 ? "-" : filepath.substring(0, idx);
    surface.setTitle(path);  //  put the last directory as the title of the window - not working
    // load file here
    txtFile = loadStrings(filepath);
    for (int i = 0; i < txtFile.length; i++)
      println(txtFile[i]);
  }
}

Thanks
Very close but no coconut.

The window is now renamed as the first part of the directories not the last one.

Hello @Grumpy_Mike,

I am using Processing 4.4.4 and Windows 10.
I use Processing 4.3.4 for error checking since it is broken for later versions.

This works:

String[] txtFile;

File file = new File(dataPath( "gcode.txt"));

void setup() {
  size(400,430);
  selectInput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    String filepath = selection.getAbsolutePath();
    println("User selected " + filepath); 
    println(file.getName());
    
    println(filepath);
    // Either of these work:
    String[] directories =  filepath.split("\\\\"); // This works!
    //String[] directories =  split(filepath,'\\'); // Processing split
    printArray(directories);
     
    surface.setTitle(filepath);  //  put the last directory as the title of the window - not working
    // load file here
    txtFile = loadStrings(filepath);    
  }
}

Output:

Gemini simple response request:

:)

This works too:

import java.util.regex.Pattern;

// Alternative: use Pattern.quote(File.separator)
String pattern = Pattern.quote(System.getProperty("file.separator"));
String[] directories = filepath.split(pattern);

References:

Thanks, but sadly not for me.

The code as posted produces this output:-

objc[21954]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fffabaad3d8) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x12f3a3f50). One of the two will be used. Which one is undefined.
User selected /Users/mikecook/Documents/An_example_Cut.gcode
gcode.txt
/Users/mikecook/Documents/An_example_Cut.gcode
[0] “/Users/mikecook/Documents/An_example_Cut.gcode”

And the window gets named as:-
Window Title

instead of just “An_example_Cut.gcode”

Maybe the difference is that I am on a Mac, macOS Mojave Version 10.14.6, I am reluctant to upgrade the Processing 4.2 at the moment because a lot of my stuff in in this version.

I haven’t explored your “also works” suggestions. That is my next stage.

Thanks again for your input.

Edit
// Either of these work: sorry no they don’t.

Using the reggex utility plus changes to the setting the title of the window to

    surface.setTitle(file.getName());

Goes a bit too far and the window title is just
“gcode.txt” and not “An_example_Cut.gcode”

It seems like the problem is in setting the screen title. This function requires a string and not an array.


String[] txtFile;

void setup() {
  size(400,430);
  selectInput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
     
    surface.setTitle(selection.getName());  //  put the last directory as the title of the window - not working
    // load file here
    txtFile = loadStrings(selection.getAbsolutePath());    
  }
}

Would this work?

2 Likes

Yes it would :grinning_face: :grinning_face: :grinning_face:

Thanks a lot.

It is odd that the others didn’t work, I will have to study it closely.

My mistake missed that little gem. This works for me.

String[] txtFile;

void setup() {
  size(640, 430);
  selectInput("Select a file to process:", "fileSelected");
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    String filepath = selection.getAbsolutePath();
    println("User selected " + filepath);
    String sep = "/"; // OS specific
    int idx = filepath.lastIndexOf(sep);
    String name = idx < 0 ? filepath : filepath.substring(idx + 1);
    String path = idx <= 0 ? "-" : filepath.substring(0, idx);
    idx = path.lastIndexOf(sep);
    String dir = idx <= 0 ? "-" : path.substring(idx);
    surface.setTitle(dir);  //  use last directory as the title of the window
    
    // All done so show all the file and path data collected
    println("Absolute path  : " +  filepath);
    println("Full dir path  : " +  path);
    println("Top directory  : " +  dir);
    println("File name      : " +  name);
    println("----------------------------------------------------------------");
    // Show contents of text file here
    txtFile = loadStrings(filepath);
    for (int i = 0; i < txtFile.length; i++)
      println(txtFile[i]);
  }
}
2 Likes

Hello,

My code runs with out errors on W10 and Processing 4.4.4 and can extract the file name (see array contents) but was not used to set the title.

A bit of work and this can be used to set the title.

My focus was on your approach with your last code example on my system.
I see there are alternative solutions!

I may very well be the issues with the slashes which can be OS dependent:

This is working here:

String filename = directories[directories.length-1]; 
surface.setTitle(filename); // Sets title to the name of file

Working means no errors and sets title for example provided (with additional edits) on my Windows 10 and Processing 4.4.4 setup.

It may still need work!

Lots to read in this topic and I will likely explore this in depth later.

:)

Well, I did mention that parameter selection is of type File. :roll_eyes:
And thus, it has many methods, including getName(): :card_file_box:

2 Likes