Extract certain characters from String

I need to extract certain characters from file name string from Dropbox. File in my Dropbox has the following format:

lettersanddigits - string.extension

I have successfully download the file with n8n and now I need to extract the first part of the filename to use in workflow. Filenames will be different in other files but keep the same format.

This is the part of the Dropbox JSON response where I can get the filename:

"name": "A02934024X - String.png",

I need help in order to extract the first part of filename.

Thank you very much for your help.

There are a few ways you could do this, are the files always going to be pngs and is there a chance the string could contain a . in it?

You can use regex to match between 2 characters so in your example it would be anything between - and . but you could also use the string split method.

Thank you for your reply. Strings will never contains . in filename.

We need the first part of filename, from Start to -, in the example this part: A02934024X

Where can I find the string split method?

Thank you.

Ah ok so it is just the A02934024X that you would be after, That is handy. So one way you could do it would be to use variable_name.substr(0, variable_name.indexOf(' -')); where variable_name is your $json.name or whatever it might be.

If that first part is always 10 characters you could also do variable_name.substr(0,10) there is also the regex route but we can save that for later.

It isn’t pretty but the workflow below shows the first 2 in action using set nodes hopefully it helps.

Sample workflow
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "name",
              "value": "A02934024X - String.png"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        430,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "extractedText",
              "value": "={{$json[\"name\"].substr(0,10)}}"
            },
            {
              "name": "extractedText2",
              "value": "={{$json[\"name\"].substr(0, $json[\"name\"].indexOf(' -'))}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        630,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}```
1 Like

Thank you very much for your help.

I will try it and update this post.

Thank you for your help.

It is working ine.

2 Likes