How to get folder name from string?

Describe the problem/error/question

How do I get the folder name in a string? for example my string would look like this:
C:\Users\Administrator\Desktop\Manga\Batch\Beware the Villainess! (Digital) (1r0n)\Beware the Villainess! v01 (2024) (Digital) (1r0n).cbz I want to get the folder name so the output should be Beware the Villainess! (Digital) (1r0n)
I tried using the javascript split function but it doesn’t seem to be valid syntax in n8n

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • **n8n version:1.89.2
  • **Database (default: SQLite):default
  • **n8n EXECUTIONS_PROCESS setting (default: own, main):default
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):npm
  • **Operating system:Windows Server 2022

Hi @Ruriko

Didn’t know .at() was an option. :grin:
I always use .slice(-1)[0] and that does work.
Can also use .pop() and such. No idea why .at() isn’t working.

Correction: it is working with .at()
The \ is a special character though so it is probably not working with that, you can use \ to escape it. try \\ that should work

I tried the slice method but it doesn’t give folder name

Please look at the rest of my post. You need to escape the character

1 Like

Try like this

const path = "C:\\Users\\Administrator\\Desktop\\Manga\\Batch\\Beware the Villainess! (Digital) (1r0n)\\Beware the Villainess! v01 (2024) (Digital) (1r0n).cbz";

// Split the path into parts
const parts = path.split("\\"); 

// Get the second-last item (folder name)
const folderName = parts[parts.length - 2];

console.log(folderName);

In case you don’t have this working yet, here’s a working version of what @BramKn suggested (re: dealing with the escaped back-slash).

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.