# Having problems with split for directories

**URL:** https://discourse.processing.org/t/having-problems-with-split-for-directories/46799
**Category:** Coding Questions
**Created:** [July 26, 2025, 4:54pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799 "2025-07-26T16:54:01Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 26, 2025, 4:54pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/1 "2025-07-26T16:54:01Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 26, 2025, 5:03pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/2 "2025-07-26T17:03:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 26, 2025, 5:17pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/3 "2025-07-26T17:17:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 26, 2025, 5:21pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/4 "2025-07-26T17:21:06Z")

</div>

> [@Grumpy\_Mike](#):
>
> `String[] dirictories split(filepath, char(/));`

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

> **[= (assign) / Reference](https://processing.org/reference/assign.html)**
>
> Assigns a value to a variable. The "=" sign does not mean "equals", but is used to place data within a variable. The "=" operator is formally called the assignment operator. There are many different t…

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()**](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName()).

There’s also method [**list()**](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#list()), which grabs all file and folder names.

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 26, 2025, 5:38pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/5 "2025-07-26T17:38:55Z")

</div>

> [@GoToLoop](#):
>
> [**getName()**](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName())

When I try this in :-

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

```

I get the message

> The function getName() does not exist.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 26, 2025, 5:42pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/6 "2025-07-26T17:42:33Z")

</div>

```auto
// 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());

```

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 26, 2025, 5:53pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/7 "2025-07-26T17:53:38Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 26, 2025, 5:56pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/8 "2025-07-26T17:56:13Z")

</div>

This worked for me and no splits LOL

```auto
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]);
  }
}

```

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 26, 2025, 6:01pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/9 "2025-07-26T18:01:12Z")

</div>

Thanks  
Very close but no coconut.

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 27, 2025, 1:03am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/10 "2025-07-27T01:03:49Z")

</div>

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:

```auto
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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/d/6d6fe1387b9b7dc1914a4676488f6408bcad102f.png)

- References:
- [split() / Reference / Processing.org](https://processing.org/reference/split_.html)
- [String / Reference / Processing.org](https://processing.org/reference/String.html)

Gemini simple response request:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/b/3b14dab62bb0f6909466513cdc444753119276f4.png)

`:)`

This works too:

```auto
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:

- [https://stackoverflow.com/questions/10336293/splitting-filenames-using-system-file-separator-symbol](https://stackoverflow.com/questions/10336293/splitting-filenames-using-system-file-separator-symbol)
- [String (Java SE 17 & JDK 17)](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html)

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 27, 2025, 4:44am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/11 "2025-07-27T04:44:14Z")

</div>

> [@glv](#):
>
> This works:

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/optimized/3X/2/d/2d42f887a72b2f266ad46943b4931dc9d4e4a673_2_690x77.png)

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

```auto
    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.

---

<div class="post-metadata">

### Author: ![stefterv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stefterv/32/21678_2.png) [@stefterv](https://discourse.processing.org/u/stefterv)
#### Post date: [July 27, 2025, 6:36am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/12 "2025-07-27T06:36:24Z")

</div>

```processing

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?

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [July 27, 2025, 7:17am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/13 "2025-07-27T07:17:37Z")

</div>

> [@stefterv](#):
>
> Would this work?

Yes it would 😀 😀 😀

Thanks a lot.

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

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [July 27, 2025, 10:27am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/14 "2025-07-27T10:27:56Z")

</div>

> [@Grumpy\_Mike](#):
>
> Very close but no coconut.
> 
> The window is now renamed as the first part of the directories not the last one.

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

```auto
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]);
  }
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 27, 2025, 10:57am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/15 "2025-07-27T10:57:10Z")

</div>

> [@Grumpy\_Mike](#):
>
> It seems like the problem is in setting the screen title. This function requires a string and not an array.

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:

- [Backslash vs Forward Slash: Key Differences in Grammar and File Paths – WP Maintenance](https://wpmaintenancemode.com/backslash-vs-forward-slash-key-differences-in-grammar-and-file-paths/)
- [JavaFX GIF Player Discussion](https://discourse.processing.org/t/javafx-gif-player-discussion/46547)

This is _working_ here:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/9/49a452a6694c0af68d4cbafbc8ec100bfd3a8c40.png)

```auto
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.

`:)`

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 27, 2025, 10:57am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/16 "2025-07-27T10:57:17Z")

</div>

> [@Grumpy\_Mike](#):
>
> It is odd that the others didn’t work, I will have to study it closely.

Well, I did mention that parameter _selection_ is of type [File](https://docs.Oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html). 🙄  
And thus, it has many methods, including **getName()**: 🗃

> **[File (Java SE 21 & JDK 21)](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName())**
>
> declaration: module: java.base, package: java.io, class: File

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [August 2, 2025, 5:20am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/17 "2025-08-02T05:20:58Z")

</div>

Thanks guys for the extra suggestions.

I was surprised by how simple the marked solution was. This is a small part of a much bigger project in getting my home built CNC router to work with more modern version, that is one available to run now.

I am working through the code one error at a time, and asking about specific sections when I get stuck.

Thanks again for all your inputs to my problem.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [August 2, 2025, 8:22am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/18 "2025-08-02T08:22:19Z")

</div>

> [@Grumpy\_Mike](#):
>
> I am trying to put the last directory as the title to Processing window

I am confused because the marked solution shows the file name in the title which is not what you asked for.

---

<div class="post-metadata">

### Author: ![Grumpy\_Mike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/grumpy_mike/32/14787_2.png) [@Grumpy\_Mike](https://discourse.processing.org/u/Grumpy_Mike)
#### Post date: [August 2, 2025, 6:09pm UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/19 "2025-08-02T18:09:00Z")

</div>

> [@quark](#):
>
> because the marked solution shows the file name in the title which is not what you asked for.

But the file name is the last part of the full path name, and so is exactly what I wanted.

I thought this could be done by using the split function on the absolute path name, but the marked solution did exactly this without using a split function.

I hope this explains the solution I accepted, and why I selected it.

Thanks again for your contribution to this thread.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [August 3, 2025, 8:40am UTC](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799/20 "2025-08-03T08:40:00Z")

</div>

> [@Grumpy\_Mike](#):
>
> the file name is the last part of the full path name

true but it is **not**

> [@Grumpy\_Mike](#):
>
> the last directory

which you asked for in your first post hence my confusion. Never mind c’est la vie 😄

[Next page](https://discourse.processing.org/t/having-problems-with-split-for-directories/46799.md?page=2)
