File lister: path in variable

Hi,
I need to check if a file already exist in a folder.
I use "file-lister" from node-red-contrib-fs, and it works well, but only if I give it the folder, for example: Start Folder = C:/Users/me/flows/
This folder is already in a flow variable, so I want to use something like:
Start Folder = flow.myFolder...but it doesn't work.
Can someone help me with this?

Try using a change node just before the file lister node. Then you can set msg.path to flow.myFolder and pass it to the file lister node.

Ok, I've put the change node and set msg.path before the file lister node.
How should I configure the file lister node?
I've tried to set Start Folder to "nothing" / msg / msg.path / flow.myFolder but nothing works except the hard-coded C:/Users/me/flows/

At the bottom of the help text for the node it tells you how to define the path in a message:
" To override the configured options, pass in a msg with msg.payload like: {"start":"/my/folder","pattern":"*.json","folders":"!context"} Any missing options will be picked up from the configured node."

So a Change node configured something like this should do it
image

Thanks, but the change node configuration is not the problem, I've an issue with the file lister configuration. I think it's just a syntax issue. What should I do in the "Start Folder"?
It works with an hard-coded C:/Users/Me/flows/, and also with an environment variable ${MyFolder}
but I don't know how to use a flow variable nor msg.path

image

Read my post again, it is not msg.path that you need to use. The path must be passed in the payload, which you can do using a Change node as I suggested.

Example flow

[{"id":"9dbd46e7994b500c","type":"change","z":"997da33a0beedade","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.start","pt":"msg","to":"myFolder","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":980,"wires":[["6df90bec1c9243ad"]]},{"id":"c1bcb8e684d8e758","type":"inject","z":"997da33a0beedade","name":"Get file list","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":980,"wires":[["9dbd46e7994b500c"]]},{"id":"6df90bec1c9243ad","type":"fs-file-lister","z":"997da33a0beedade","name":"","start":"/","pattern":"*.*","folders":"*","hidden":true,"lstype":"files","path":true,"single":false,"depth":0,"stat":false,"showWarnings":true,"x":540,"y":980,"wires":[["43d268a322a00bb8"]]},{"id":"43d268a322a00bb8","type":"debug","z":"997da33a0beedade","name":"debug 2473","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":980,"wires":[]}]

That does not look to me like the fs list node. This is what I see when I open it.

Please export your node and paste it in a reply

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

I assumed it is the one out of node-red-contrib-fs, as stated in the first post, which does have a Start Folder field.

My bad! I have both node-red-contrib-fs and node-red-contrib-filesystem installed and th 'filesystem' nodes show as
Screenshot 2023-12-22 at 7.24.13 AM and I was looking at fs list not fs - file - lister

Here is a simplified version of my code.
The file lister is the one from node-red-contrib-fs

[
    {
        "id": "d557afe07f574c0f",
        "type": "tab",
        "label": "Flux 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "3a8b9d456b1084e3",
        "type": "inject",
        "z": "d557afe07f574c0f",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 270,
        "y": 140,
        "wires": [
            [
                "9d764aca51045d52"
            ]
        ]
    },
    {
        "id": "9d764aca51045d52",
        "type": "function",
        "z": "d557afe07f574c0f",
        "name": "function 9",
        "func": "var folder = \"C:/Users/Me/flows/\" \n\nvar file = \"myfile.csv\"\n\nmsg.fname = file\nmsg.filename = folder + file\n\nmsg.filecontent = msg.payload;\nmsg.payload = { \"pattern\": msg.fname };\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 460,
        "y": 140,
        "wires": [
            [
                "73e73b91f8cd8675"
            ]
        ]
    },
    {
        "id": "73e73b91f8cd8675",
        "type": "fs-file-lister",
        "z": "d557afe07f574c0f",
        "name": "",
        "start": "C:/Users/Me/flows/",
        "pattern": "",
        "folders": "*",
        "hidden": true,
        "lstype": "files",
        "path": true,
        "single": true,
        "depth": 0,
        "stat": true,
        "showWarnings": false,
        "x": 620,
        "y": 140,
        "wires": [
            [
                "71388be84dc2fb5f"
            ]
        ]
    },
    {
        "id": "71388be84dc2fb5f",
        "type": "switch",
        "z": "d557afe07f574c0f",
        "name": "Test si fichier existe",
        "property": "$count(msg.payload)",
        "propertyType": "jsonata",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 810,
        "y": 140,
        "wires": [
            [
                "42d026e6a3f1aab3"
            ],
            [
                "920657f2b92d9946"
            ]
        ]
    },
    {
        "id": "d93f27d25ef29295",
        "type": "csv",
        "z": "d557afe07f574c0f",
        "name": "Génère fichier + en-tête",
        "sep": ",",
        "hdrin": "",
        "hdrout": "all",
        "multi": "one",
        "ret": "\\r",
        "temp": "Time, Antenne, Data",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 1260,
        "y": 120,
        "wires": [
            [
                "ab86f6a722296074"
            ]
        ]
    },
    {
        "id": "4d3fa0c630e9e96b",
        "type": "csv",
        "z": "d557afe07f574c0f",
        "name": "Ajoute ligne au fichier",
        "sep": ",",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "\\n",
        "temp": "Time, Antenne, Data",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 1260,
        "y": 160,
        "wires": [
            [
                "ab86f6a722296074"
            ]
        ]
    },
    {
        "id": "42d026e6a3f1aab3",
        "type": "change",
        "z": "d557afe07f574c0f",
        "name": "Get file content",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filecontent",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 120,
        "wires": [
            [
                "d93f27d25ef29295"
            ]
        ]
    },
    {
        "id": "920657f2b92d9946",
        "type": "change",
        "z": "d557afe07f574c0f",
        "name": "Get file content",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filecontent",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1030,
        "y": 160,
        "wires": [
            [
                "4d3fa0c630e9e96b"
            ]
        ]
    },
    {
        "id": "ab86f6a722296074",
        "type": "file",
        "z": "d557afe07f574c0f",
        "name": "",
        "filename": "filename",
        "filenameType": "msg",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 1500,
        "y": 140,
        "wires": [
            []
        ]
    }
]

If you read the help for the dnode you will see

To override the configured options, pass in a msg with msg.payload like: {"start":"/my/folder","pattern":"*.json","folders":"!context"} Any missing options will be picked up from the configured node.

Your function node you have this:

msg.payload = { "pattern": msg.fname };

which is not what the node wants

That's it!
I was missing {"start":"/my/folder"}.
With this it's alright:

var folder = flow.get("myFolder")
msg.payload = {"start":folder, "pattern": msg.fname };

Thanks guys!

1 Like

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