FTP List and Download specific files

Hello.

I have a FTP node that lists the files inside of a directory.

I want from that list to pass the data to another FTP node to download them but only the data of specific files.

For example here is a json from FTP List

[
{
"type": "-",
"name": "mk0410.dat",
"size": 702,
"modifyTime": "2021-10-04T13:07:13.000Z",
"accessTime": "2021-10-04T13:07:20.000Z",
"rights": {
"user": "rw",
"group": "r",
"other": "r"
},
"owner": 0,
"group": 0,
"path": "/root/testemail/mk0410.dat"
},
{
"type": "-",
"name": "mk0410.csv.txt",
"size": 740,
"modifyTime": "2021-10-04T13:07:13.000Z",
"accessTime": "2021-10-04T13:07:20.000Z",
"rights": {
"user": "rw",
"group": "r",
"other": "r"
},
"owner": 0,
"group": 0,
"path": "/root/testemail/mk0410.csv.txt"
},
{
"type": "-",
"name": "dim0410.csv.txt",
"size": 740,
"modifyTime": "2021-10-04T13:07:13.000Z",
"accessTime": "2021-10-04T13:07:20.000Z",
"rights": {
"user": "rw",
"group": "r",
"other": "r"
},
"owner": 0,
"group": 0,
"path": "/root/testemail/dim0410.csv.txt"
},
{
"type": "-",
"name": "mk0410.txt",
"size": 2431,
"modifyTime": "2021-10-04T13:07:13.000Z",
"accessTime": "2021-10-04T13:07:20.000Z",
"rights": {
"user": "rw",
"group": "r",
"other": "r"
},
"owner": 0,
"group": 0,
"path": "/root/testemail/mk0410.txt"
}
]

I want to download only the files that are .csv.txt

Can someone show me how to create a function to get only the .csv.txt files and pass it to FTP Download node?

Hey @Mulen,

Something like this would do it…

image

So we do an FTP list then run an If on the filename and if it ends with .csv.txt we download it, The snippet below should help.

{
  "nodes": [
    {
      "parameters": {
        "operation": "list",
        "path": "/files/"
      },
      "name": "FTP",
      "type": "n8n-nodes-base.ftp",
      "typeVersion": 1,
      "position": [
        430,
        730
      ],
      "credentials": {
        "ftp": "FTP"
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"name\"]}}",
              "operation": "endsWith",
              "value2": ".csv.txt"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        620,
        730
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        620,
        890
      ]
    },
    {
      "parameters": {
        "path": "={{$json[\"path\"]}}"
      },
      "name": "FTP1",
      "type": "n8n-nodes-base.ftp",
      "typeVersion": 1,
      "position": [
        830,
        710
      ],
      "credentials": {
        "ftp": "FTP"
      }
    }
  ],
  "connections": {
    "FTP": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "FTP1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
3 Likes

You are amazing.

Thank you very much!

1 Like