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

**URL:** <https://discourse.nodered.org/t/how-can-i-write-csv-file-and-change-filename-with-name-list-in-array/68127>\
**Category:** General\
**Created:** [23 September 2022 10:22 UTC](https://discourse.nodered.org/t/how-can-i-write-csv-file-and-change-filename-with-name-list-in-array/68127 "2022-09-23T10:22:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wcrk](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wcrk](https://discourse.nodered.org/u/wcrk)\
**Post date:** [23 September 2022 10:22 UTC](https://discourse.nodered.org/t/how-can-i-write-csv-file-and-change-filename-with-name-list-in-array/68127/1 "2022-09-23T10:22:06Z")

</div>

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_1663925632503](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2502560c889f340279bed926bebec11b4edc13a.jpeg)

![messageImage_1663928273819](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46588b201d2f8cc91ec75e1260b03e7d59a99de4.jpeg)

```auto
[
    {
        "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
    }
]

```

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [23 September 2022 11:24 UTC](https://discourse.nodered.org/t/how-can-i-write-csv-file-and-change-filename-with-name-list-in-array/68127/2 "2022-09-23T11:24:03Z")

</div>

try this solution with necessary modification

> [@Generate a PDF-file with variable filename](https://discourse.nodered.org/t/generate-a-pdf-file-with-variable-filename/64228/2):
>
> remove the hard coded filename from the file node & in your function add something like... // your code const reportDate = new Date().toISOString().replace(/[TG]/g,'\_').replace(/:/g,'-').substring(0,19); const reportName = 'mydata' msg.filename = `c/reports/report-${reportName}-${reportDate}.pdf` return msg;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 November 2022 11:24 UTC](https://discourse.nodered.org/t/how-can-i-write-csv-file-and-change-filename-with-name-list-in-array/68127/3 "2022-11-22T11:24:05Z")

</div>

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