# How to list files and parse them

**URL:** <https://discourse.nodered.org/t/how-to-list-files-and-parse-them/36083>\
**Category:** General\
**Created:** [17 November 2020 17:59 UTC](https://discourse.nodered.org/t/how-to-list-files-and-parse-them/36083 "2020-11-17T17:59:49Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [20 November 2020 20:02 UTC](https://discourse.nodered.org/t/how-to-list-files-and-parse-them/36083/21 "2020-11-20T20:02:17Z")

</div>

Hi, now I have two problems:

```auto
1) var publicVariables = {
    Data_START: "00:00:00", //this generate an error, I assume because it's not recognized as an hour
    Lotto_START: "0",
    Scheda_START: "0"
}
global.set("publicVariables", publicVariables);
return msg;

```

1. No Output of payload with Calcola\_Tempo calculated with:

```auto
var Calcola_Tempo = (msg.payload.Ora - publicVariables.Data_START);

```

This is the flow

```auto
[{"id":"98a22fb9.cb5a","type":"file in","z":"b1118599.3545f8","name":"Apri LOG DVC","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":340,"y":280,"wires":[["a25570de.29aed"]]},{"id":"ab3cd84c.703898","type":"change","z":"b1118599.3545f8","name":"C:\\LOG_QUADRA\\Storico","rules":[{"t":"set","p":"path","pt":"msg","to":"\\\\10.0.0.4\\c$\\LOG_QUADRA\\","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":220,"wires":[["91d240da.015f8"]]},{"id":"91d240da.015f8","type":"fs-ops-dir","z":"b1118599.3545f8","name":"","path":"path","pathType":"msg","filter":"*.log","filterType":"str","dir":"files","dirType":"msg","x":480,"y":220,"wires":[["2c369e2e.840132"]]},{"id":"2c369e2e.840132","type":"change","z":"b1118599.3545f8","name":"copy files to payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"files","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":220,"wires":[["12df3723.658b69"]]},{"id":"7266539f.9340cc","type":"function","z":"b1118599.3545f8","name":"make filename","func":"msg.file = msg.payload;\nmsg.filename = msg.path + \"\\\\\" + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":140,"y":280,"wires":[["98a22fb9.cb5a"]]},{"id":"12df3723.658b69","type":"split","z":"b1118599.3545f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":890,"y":220,"wires":[["7266539f.9340cc"]]},{"id":"a25570de.29aed","type":"csv","z":"b1118599.3545f8","name":"Split Fields","sep":";","hdrin":"","hdrout":false,"multi":"one","ret":"\\n","temp":"Giorno, Ora, Stato, Programma, Lotto, Scheda","skip":"0","strings":false,"x":530,"y":280,"wires":[["71d7613e.abfca"]]},{"id":"5194020f.43fdfc","type":"MSSQL","z":"b1118599.3545f8","mssqlCN":"e91a56e2.a1e9f8","name":"DVC to SQL","query":"insert into Output_Produzione (\n Centro_di_Lavoro, \n data, \n n_ord_prod, \n n_operazione, \n articolo, \n VarINT1,\n VarCHAR1,\n Tempo)\nVALUES (\n 1109,\n '{{{payload.DateTime}}}',\n '{{{payload.LottoRight5}}}',\n '{{{payload.LottoLeft4}}}',\n '{{{payload.Programma}}}',\n '{{{payload.Scheda}}}',\n '{{{payload.Stato}}}',\n '{{{payload.Tempo}}}')","outField":"payload","returnType":"0","throwErrors":"0","x":990,"y":280,"wires":[[]]},{"id":"71d7613e.abfca","type":"function","z":"b1118599.3545f8","name":"NEW transform DVC","func":"//copy of original payload for debug output\nmsg.origPayload = msg.payload;\n\n//make a copy of the row data... in some flows doesn't work ... WHY????\n//let row = {...msg.payload};\n\nif (msg.payload.Stato == \"START\"){\n //Store Data and Lotto of the \"START\" Record to calculate delta time with the \"END\" record\n global.set(publicVariables.Data_START, msg.payload.Ora);\n global.set(publicVariables.Lotto_START, msg.payload.Lotto);\n global.set(publicVariables.Scheda_START, msg.payload.Scheda);\n\n}\nelse {\n \n//If any condition is false, the record is skipped\n if (msg.payload.Stato == \"END\"){\n //recall public variables\n var publicVariables = global.get(\"publicVariables\");\n\n //Check if START lotto end END Lotto are the same\n if (msg.payload.Lotto == publicVariables.Lotto_START){\n //calculate delta time using the global variable Data_START\n var Calcola_Tempo = (msg.payload.Ora - publicVariables.Data_START);\n //calculate Quantity using the global variable Scheda_START\n var Calcola_Qta = (msg.payload.Scheda - publicVariables.Scheda_START);\n\n if (msg.payload.Lotto !== undefined){\n var WO = msg.payload.Lotto.substr(5,6);\n var OP = msg.payload.Lotto.substr(0,4);\n\n if (msg.payload.Programma !== undefined){\n var Prog = 'AP11'+ msg.payload.Programma;\n \n if (msg.payload.Scheda !== undefined){\n var Sch = msg.payload.Scheda;\n \n //solo se tutti gli IF sono andati a buon fine\n //preparo il messaggio\n \n var strG = msg.payload.Giorno;\n var strO = msg.payload.Ora;\n //split Data dd/mm/yyyy hh:mm:ss\n var D = strG.substr(6,4)+'/'+strG.substr(3,2)+'/'+strG.substr(0,2)+' '+strO;\n\n msg.payload = {\n a: 1109, //not sure what this is about?\n //DateTime: formatDT(row.Giorno, row.Ora), //Call a function to make the correct DATETIME string\n DateTime: D,\n //LottoRight5 : lottoParts[1], //characters right of the '/' \n //LottoLeft4: lottoParts[0], //characters left of the '/' \n LottoRight5 : WO,\n LottoLeft4 : OP,\n Stato: Status, //add Stato to the payload\n Programma: Prog, //add Programma to the payload\n Scheda: Sch, //add Scheda to the payload\n Tempo : Calcola_Tempo,\n Qta : Calcola_Qta\n }\n }\n \n }\n }\n }\n }\n\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":760,"y":280,"wires":[["2915102.0f697f"]]},{"id":"b64b201.b7b0ae","type":"inject","z":"b1118599.3545f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":140,"wires":[["69156b95.e64ca4"]]},{"id":"2915102.0f697f","type":"debug","z":"b1118599.3545f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":340,"wires":[]},{"id":"69156b95.e64ca4","type":"function","z":"b1118599.3545f8","name":"Declare Global Variables","func":"var publicVariables = {\n Data_START: \"00:00:00\",\n Lotto_START: \"0\",\n Scheda_START: \"0\"\n}\nglobal.set(\"publicVariables\", publicVariables);\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":140,"wires":[["ab3cd84c.703898"]]},{"id":"e91a56e2.a1e9f8","type":"MSSQL-CN","z":"","tdsVersion":"7_4","name":"Scambio_NAV","server":"srv-dc","port":"1433","encyption":true,"database":"Scambio_NAV","useUTC":true,"connectTimeout":"90000","requestTimeout":"90000","cancelTimeout":"90000","pool":"5","parseJSON":false}]
```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 November 2020 21:23 UTC](https://discourse.nodered.org/t/how-to-list-files-and-parse-them/36083/22 "2020-11-20T21:23:29Z")

</div>

> [@Simone](#):
>
> `Data_START: "00:00:00", //this generate an error, I assume because it's not recognized as an hour`

You haven't told us where the error is generated or what the error says.

> [@Simone](#):
>
> No Output of payload with Calcola\_Tempo calculated with:

Have a look at the node-red docs page [Writing Functions](https://nodered.org/docs/user-guide/writing-functions) which will show you how you can do simple debugging by inserting `node.warn()` at appropriate points in the function so you can work out what is going wrong.

---

<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:** [4 December 2020 21:23 UTC](https://discourse.nodered.org/t/how-to-list-files-and-parse-them/36083/23 "2020-12-04T21:23:35Z")

</div>

Closed on 4 December 2020 21:23 UTC

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

[Previous page](https://discourse.nodered.org/t/how-to-list-files-and-parse-them/36083.md?page=1)
