Node-RED: Infinite path appending during recursive FTP directory traversal

I am using Node-RED (v2.1.4) and node.js v14.17.0 to monitor an FTP server (FileZilla) where a Dahua IP camera uploads snapshots. By default, the camera creates a deeply nested directory structure:

DEVICE_ID / YYYY-MM-DD / pic_001 / HH / filename.jpg.

The standard ftp list node only returns a flat list of the current directory. To find the images, I built a recursive loop to enter each subfolder. However, my logic is failing, and the folder path is being built incorrectly in an infinite loop.

The Setup

  • FTP Server: FileZilla Server 1.12.1.
  • Node-RED Node: node-red-contrib-ftp.
  • Flow Logic: An Inject node triggers every 5 seconds, starts at a base path, uses ftp list, and a function node determines if subfolders exist to loop back.

The Error:
When the recursive flow runs, my debug sidebar shows that the currentFolder variable is being corrupted. It continuously appends the same folder name to itself instead of moving down to the next date-stamped subfolder:

currentFolder: "/9K092FDPAGC88BD/9K092FDPAGC88BD/9K092FDPAGC88BD/...".

Because of this, the foldersToScan array never empties, and the flow never reaches the actual image files located at the bottom of the tree.

The Question

How can I modify my function node to correctly identify subfolders and build the path without this self-referencing recursion?

Welcome to the forum @manel :mx_claus:

Good grief that's some old versions you are running!

Node-red is on v4, soon to go to v5, and node.js v20, soon to go to v24.

If you can, it would be very wise to upgrade before trying to resolve the issue, it may already be fixed

Thank you! Regarding the upgrade: I am worried If I upgrade from my current versions to teh newest ones, will this affect my other existing projects that are currently working correctly?

is there a risk of 'breaking changes' that would require me to rewrite my old code or reconfigure my nodes?

Yes. Always a risk. As you say it’s a function you have written that is failing then I wouldn’t upgrade. Maybe share the function so folk here can take a look instead.


here are both my flow and the errors .
the code of the function process files and folders:

let items = msg.payload;
let currentFolder = msg.currentFolder;
let allFiles = msg.allFiles || [];
let foldersToScan = msg.foldersToScan || [];

if (!Array.isArray(items)) {
    return msg;
}

items.forEach(item => {

    if (!item.name) return;

    // Dahua returns relative paths already → do NOT duplicate
    let itemPath;

    if (item.name.startsWith("/")) {
        itemPath = item.name;
    } else if (currentFolder === "/") {
        itemPath = "/" + item.name;
    } else {
        itemPath = currentFolder + "/" + item.name;
    }

    // Normalize path
    itemPath = itemPath.replace(/\/+/g, "/");

    // Prevent infinite recursion
    if (itemPath === currentFolder) return;

    if (item.type === "d" || item.type === "directory") {

        if (!foldersToScan.includes(itemPath)) {
            foldersToScan.push(itemPath);
        }

    } else if (item.type === "-" || item.type === "file") {

        if (/\.(jpg|jpeg|png)$/i.test(item.name)) {
            allFiles.push({
                name: item.name,
                path: itemPath,
                date: item.date || item.modifyTime,
                size: item.size
            });
        }
    }
});

msg.allFiles = allFiles;
msg.foldersToScan = foldersToScan;

return msg;