How can I write CSV file and change filename with name list in array

I am very new to Node-RED and javascript. My first project is to send query command from CSV files to MSSQL, then get the value after querying and write as CSV files by naming the file as same as the file sending to query to MSSQL. But I can't write the file. It is always replaced by the query command CSV files and filenames are all the same I want to write it in other folder. I try to use for loop to get filenames in array, but it only returns the first iteration. Could you help me solving this problem? Thanks for advance.
(Sorry for my bad English)

messageImage_1663928273819

[
    {
        "id": "93592be3ddf665ea",
        "type": "inject",
        "z": "d8dfe1f7f653b5cd",
        "name": "",
        "props": [
            {
                "p": "path",
                "v": "C:\\Users\\ADMIN\\Desktop\\Study\\data",
                "vt": "str"
            },
            {
                "p": "filename",
                "v": "C:\\Users\\ADMIN\\Desktop\\Study\\test",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 130,
        "y": 180,
        "wires": [
            [
                "073037739906ca9c"
            ]
        ]
    },
    {
        "id": "073037739906ca9c",
        "type": "fs-file-lister",
        "z": "d8dfe1f7f653b5cd",
        "name": "",
        "start": "C:\\Users\\ADMIN\\Desktop\\Study\\test",
        "pattern": "*.*",
        "folders": "*",
        "hidden": true,
        "lstype": "files",
        "path": true,
        "single": false,
        "depth": 0,
        "stat": false,
        "showWarnings": true,
        "x": 320,
        "y": 180,
        "wires": [
            [
                "8d31249a7e33fab5",
                "e99c1b10323c28ab",
                "e2cce093fce5abd0"
            ]
        ]
    },
    {
        "id": "8d31249a7e33fab5",
        "type": "debug",
        "z": "d8dfe1f7f653b5cd",
        "name": "debug 28",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 560,
        "y": 100,
        "wires": []
    },
    {
        "id": "e99c1b10323c28ab",
        "type": "file in",
        "z": "d8dfe1f7f653b5cd",
        "name": "",
        "filename": "payload",
        "filenameType": "msg",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 700,
        "y": 180,
        "wires": [
            [
                "a8e8ab5c1a31d1d0",
                "5ba17e06eb46c524"
            ]
        ]
    },
    {
        "id": "5ba17e06eb46c524",
        "type": "MSSQL",
        "z": "d8dfe1f7f653b5cd",
        "mssqlCN": "a51e405c.10f64",
        "name": "MSSQL",
        "outField": "payload",
        "returnType": 0,
        "throwErrors": "0",
        "query": "{{{msg.payload}}}",
        "modeOpt": "",
        "modeOptType": "query",
        "queryOpt": "payload",
        "queryOptType": "msg",
        "paramsOpt": "queryParams",
        "paramsOptType": "msg",
        "rows": "rows",
        "rowsType": "msg",
        "params": [],
        "x": 920,
        "y": 180,
        "wires": [
            [
                "2dc44d451a0f77e2",
                "e1c77c31f51d4dba"
            ]
        ]
    },
    {
        "id": "a8e8ab5c1a31d1d0",
        "type": "debug",
        "z": "d8dfe1f7f653b5cd",
        "name": "debug 30",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 780,
        "y": 100,
        "wires": []
    },
    {
        "id": "2dc44d451a0f77e2",
        "type": "debug",
        "z": "d8dfe1f7f653b5cd",
        "name": "debug 31",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 100,
        "wires": []
    },
    {
        "id": "e1c77c31f51d4dba",
        "type": "csv",
        "z": "d8dfe1f7f653b5cd",
        "name": "",
        "sep": ":",
        "hdrin": false,
        "hdrout": "all",
        "multi": "mult",
        "ret": "\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": "",
        "include_null_values": "",
        "x": 1130,
        "y": 180,
        "wires": [
            [
                "b035bf563b701391",
                "08b294de8df862f6"
            ]
        ]
    },
    {
        "id": "b035bf563b701391",
        "type": "debug",
        "z": "d8dfe1f7f653b5cd",
        "name": "debug 32",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1140,
        "y": 100,
        "wires": []
    },
    {
        "id": "5885e309fb4a8b80",
        "type": "debug",
        "z": "d8dfe1f7f653b5cd",
        "name": "debug 33",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 900,
        "y": 340,
        "wires": []
    },
    {
        "id": "08b294de8df862f6",
        "type": "function",
        "z": "d8dfe1f7f653b5cd",
        "name": "Set filename and add data",
        "func": "msg.topic = \"Data\"\nmsg.filename = msg.files\nmsg.payload = Buffer.from(msg.payload);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 810,
        "y": 260,
        "wires": [
            [
                "5885e309fb4a8b80",
                "bc99ac28912c4fe2"
            ]
        ]
    },
    {
        "id": "e2cce093fce5abd0",
        "type": "fs-ops-dir",
        "z": "d8dfe1f7f653b5cd",
        "name": "",
        "path": "C:\\Users\\ADMIN\\Desktop\\Study\\test",
        "pathType": "str",
        "filter": "*",
        "filterType": "str",
        "dir": "file",
        "dirType": "msg",
        "x": 160,
        "y": 260,
        "wires": [
            [
                "799ee4ee35734b4b"
            ]
        ]
    },
    {
        "id": "c01879a8b1cda076",
        "type": "debug",
        "z": "d8dfe1f7f653b5cd",
        "name": "debug 37",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "local",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 320,
        "wires": []
    },
    {
        "id": "799ee4ee35734b4b",
        "type": "function",
        "z": "d8dfe1f7f653b5cd",
        "name": "function 10",
        "func": "for (let i = 0; i < msg.file.length; i++) {\n    msg.local = msg.file[i];\n    return msg;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 330,
        "y": 260,
        "wires": [
            [
                "c01879a8b1cda076"
            ]
        ]
    },
    {
        "id": "bc99ac28912c4fe2",
        "type": "function",
        "z": "d8dfe1f7f653b5cd",
        "name": "Set location",
        "func": "for (let i = 0; i < msg.files.length; i++){\n     msg.localfiles = \"/Users/ADMIN/Desktop/Study/\" + msg.files[i];\n     }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1070,
        "y": 260,
        "wires": [
            [
                "4ca63cedd4efa379"
            ]
        ]
    },
    {
        "id": "4ca63cedd4efa379",
        "type": "file",
        "z": "d8dfe1f7f653b5cd",
        "name": "",
        "filename": "localfiles",
        "filenameType": "msg",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 1240,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "a51e405c.10f64",
        "type": "MSSQL-CN",
        "tdsVersion": "7_4",
        "name": "My SQL Server",
        "server": "10.202.164.67",
        "port": "1433",
        "encyption": false,
        "trustServerCertificate": false,
        "database": "BikeStores",
        "useUTC": false,
        "connectTimeout": "15000",
        "requestTimeout": "15000",
        "cancelTimeout": "5000",
        "pool": "5",
        "parseJSON": true,
        "enableArithAbort": true
    }
]

try this solution with necessary modification

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