How to pass the array to the loop

Hello Everyone,

I'm completely new to node-red.
I am trying to create a flow in which i have to loop while condition and
within loop i need to add sub flows based on value of loop.
※sub flows are the same handling.

Input

  • A zip file that inside contains many text files

Output

  • The result file contains the processed content of each txt file in the input ZIP file, such as comparison, calculation and editing.

I'm thinking of the draf like this:

[{"id":"a5510213.ab69c","type":"subflow","name":"Subflow 1","info":"","in":[],"out":[]},{"id":"43e653d8.4eb50c","type":"function","z":"a5510213.ab69c","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":190,"y":80,"wires":[[]]},{"id":"18febc57.82b2a4","type":"switch","z":"a5510213.ab69c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":220,"y":120,"wires":[[]]},{"id":"603ee840.f02c08","type":"template","z":"3b501765.6a7958","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","x":510,"y":520,"wires":[["17301876.45fad8"]]},{"id":"17301876.45fad8","type":"switch","z":"3b501765.6a7958","name":"i<4","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"4","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":790,"y":520,"wires":[["781cc82b.bc4508"],["bc8de519.bf7fb8"]]},{"id":"de118552.608c38","type":"function","z":"3b501765.6a7958","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":380,"wires":[["17301876.45fad8"]]},{"id":"912db2b0.8768b","type":"debug","z":"3b501765.6a7958","name":"msg end","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"y":560,"wires":[]},{"id":"ae6ab5c2.fee078","type":"debug","z":"3b501765.6a7958","name":"msg output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1270,"y":480,"wires":[]},{"id":"1f381e41.ded552","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":390,"y":520,"wires":[["603ee840.f02c08"]]},{"id":"21b81be3.dd31f4","type":"comment","z":"3b501765.6a7958","name":"↓ output 4 times","info":"","x":1280,"y":440,"wires":[]},{"id":"ab22648b.1eff08","type":"comment","z":"3b501765.6a7958","name":"conditional for while ↑","info":"","x":760,"y":560,"wires":[]},{"id":"e138d0d7.649ce","type":"comment","z":"3b501765.6a7958","name":"↓ init value i to increment","info":"","x":570,"y":480,"wires":[]},{"id":"d10b2b36.526988","type":"comment","z":"3b501765.6a7958","name":"↓ count up i","info":"","x":970,"y":340,"wires":[]},{"id":"bc8de519.bf7fb8","type":"template","z":"3b501765.6a7958","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"End of loop","x":1020,"y":560,"wires":[["912db2b0.8768b"]]},{"id":"781cc82b.bc4508","type":"template","z":"3b501765.6a7958","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Value i = {{i}}","x":1020,"y":480,"wires":[["ae6ab5c2.fee078","de118552.608c38"]]},{"id":"d4664aee.ff01c8","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":310,"y":740,"wires":[["6fbc670f.1f89b8"]]},{"id":"9d77fa16.931b98","type":"zip","z":"3b501765.6a7958","name":"","mode":"decompress","filename":"","outasstring":false,"x":540,"y":740,"wires":[["b12057db.c97bd8"]]},{"id":"dc2e094d.3e4c88","type":"subflow:a5510213.ab69c","z":"3b501765.6a7958","name":"","env":[],"x":800,"y":740,"wires":[]},{"id":"fde08b64.a893b8","type":"comment","z":"3b501765.6a7958","name":"↑ file content: comparison, calculation, editing,...","info":"","x":920,"y":780,"wires":[]},{"id":"b12057db.c97bd8","type":"debug","z":"3b501765.6a7958","name":"msg zip","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":660,"wires":[]},{"id":"77cad8a2.aa1348","type":"comment","z":"3b501765.6a7958","name":"↑ output array file txt","info":"","x":578,"y":780,"wires":[]},{"id":"6fbc670f.1f89b8","type":"file in","z":"3b501765.6a7958","name":"","filename":"/Temp/FOLDER_TEXT.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":410,"y":640,"wires":[["9d77fa16.931b98"]]}]

■What has been done:

  • Create while loop using node of Node-RED with the value of the loop condition fixed is 4
  • The extracted input zip file the result is an array msg.payload : array[3] (★)

■Problem:

  • I don't know how to put array in (★) instead of fixed value 4 of while loop and make it work

Any idea will be great.
Thanks.

OK, quick question:

Is the number of loops known?

Silly question.

The top inject node. That starts things happening. Though I guess the bottom one does stuff too.

Is the number of loops you need to do known when you press the top inject button?
Actually, why two inject buttons?

I need to know where in the scheme of things those two inject nodes are pressed.

I can show you a way to control the switch node as is if you want.

Keeping to your flow set up / layout here is a trick you can do.

[{"id":"a5eb3e22.30b0a","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1820,"wires":[["3ee76551.6d380a"]]},{"id":"f3c4e495.867ca","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1860,"wires":[["3ee76551.6d380a"]]},{"id":"780d5119.58eef8","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1900,"wires":[["3ee76551.6d380a"]]},{"id":"aed35ff7.1c31b8","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1940,"wires":[["3ee76551.6d380a"]]},{"id":"cd3e8854.7fea9","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1980,"wires":[["3ee76551.6d380a"]]},{"id":"29b8e918.8a8e76","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"6","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2020,"wires":[["3ee76551.6d380a"]]},{"id":"2546c770.a58fc","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2060,"wires":[["3ee76551.6d380a"]]},{"id":"19ac02a9.39538d","type":"comment","z":"68e3655f.b606b4","name":"Determined else where in the flow.","info":"","x":230,"y":1780,"wires":[]},{"id":"3ee76551.6d380a","type":"change","z":"68e3655f.b606b4","name":"flow context loop_count","rules":[{"t":"set","p":"loop_count","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1940,"wires":[[]]},{"id":"77848dd5.e1e62c","type":"switch","z":"68e3655f.b606b4","name":"i<4","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"loop_count","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":1690,"wires":[["bf585193.9715a8"],["9a914e62.49a298"]]}]

But you MUST inject a value before you run the flow.

The 7 options (as examples) is set else where in the flow, where you know how many times you need to loop.

That stores the value in a flow context which is a neat thing I took a long time to get my head around.

Then the switch node looks at the value and compares it to the flow context value.
Here I've called it loop_count for clarity.

There are three kinds of context - of which I know:

Node context. So it is only seen in that node. Handy in function nodes.
Flow context. Which is visible to that ... flow. (or tab) in the editor screen.
and lastly
Global context. Which is .... global.

They are like an overlying variable you can use in scenarios like this.

Hope that helps.

1 Like

Sorry for the confusing explanation.
The picture above is just a draf illustrating what I was thinking.
The end result is aggregated into an inject node.

I want to pass an array of text files into a loop like msg.payload.length instead of the fixed 4 value of the while loop.

Array of length 3, but in the while loop is not received...

[{"id":"6683e387.1412ec","type":"template","z":"3b501765.6a7958","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","x":970,"y":1840,"wires":[["5d38e308.fc926c"]]},{"id":"b06fdcae.0566f","type":"switch","z":"3b501765.6a7958","name":"i<msg.payload","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"payload","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1380,"y":1840,"wires":[["ad7b00b5.afe92"],["1d1d7b28.2d9f95"]]},{"id":"1b90939b.ddff3c","type":"function","z":"3b501765.6a7958","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":1550,"y":1680,"wires":[["b06fdcae.0566f"]]},{"id":"72ff57b2.8d9b98","type":"debug","z":"3b501765.6a7958","name":"msg end","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1900,"y":1880,"wires":[]},{"id":"6fd1f00e.0c2f2","type":"debug","z":"3b501765.6a7958","name":"msg output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1910,"y":1800,"wires":[]},{"id":"6eb1638.8492e9c","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":730,"y":1680,"wires":[["1e822a01.417016"]]},{"id":"f56ab556.176988","type":"comment","z":"3b501765.6a7958","name":"conditional for while ↑","info":"","x":1400,"y":1880,"wires":[]},{"id":"a1f1f218.97fdd","type":"comment","z":"3b501765.6a7958","name":"↓ init value i to increment","info":"","x":1030,"y":1800,"wires":[]},{"id":"eac8b268.578e","type":"comment","z":"3b501765.6a7958","name":"↓ count up i","info":"","x":1570,"y":1640,"wires":[]},{"id":"1d1d7b28.2d9f95","type":"template","z":"3b501765.6a7958","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"End of loop","x":1660,"y":1880,"wires":[["72ff57b2.8d9b98"]]},{"id":"ad7b00b5.afe92","type":"template","z":"3b501765.6a7958","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Value i = {{i}}","x":1660,"y":1800,"wires":[["1b90939b.ddff3c","6fd1f00e.0c2f2"]]},{"id":"79958d94.6dc6a4","type":"zip","z":"3b501765.6a7958","name":"","mode":"decompress","filename":"","outasstring":false,"x":790,"y":1840,"wires":[["73a018f3.085488","6683e387.1412ec"]]},{"id":"73a018f3.085488","type":"debug","z":"3b501765.6a7958","name":"msg zip","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":980,"y":1940,"wires":[]},{"id":"6d0dc499.b9d6ec","type":"comment","z":"3b501765.6a7958","name":"output array file txt ↑","info":"","x":750,"y":1880,"wires":[]},{"id":"1e822a01.417016","type":"file in","z":"3b501765.6a7958","name":"","filename":"/Temp/FOLDER_TEXT.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":970,"y":1680,"wires":[["79958d94.6dc6a4"]]},{"id":"5d38e308.fc926c","type":"function","z":"3b501765.6a7958","name":"","func":"msg.payload = msg.payload.length;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1140,"y":1880,"wires":[["b06fdcae.0566f","92139f11.34ea8"]]},{"id":"92139f11.34ea8","type":"debug","z":"3b501765.6a7958","name":"msg function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1310,"y":1940,"wires":[]}]

Just a note, node-red is used to get a message from point a to point b with manipulation along the way: loops should be avoided when possible.

Why is there a "need" for a loop ?

1 Like

Need a loop because corresponding to the processing of each TXT file, if it fails, it will do other things such as output the file to the Error folder (1), send mail to report the error file (2), ... and this also needs to be done with the nodes of Node-RED.

If there is no separate loop, but write the loop in function node, things like (1), (2), ... cannot be done by node of Node-RED.

You dont really need a loop - you need to change your perspective :wink:

Perhaps this would work for you (I am uncertain what you intend to do with the templates so it may not work for you)

Look maa - no loops ...

results...

[{"id":"910ee8fb.f36198","type":"debug","z":"ea150be4.370218","name":"template2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1220,"y":260,"wires":[]},{"id":"1ff87870.2d6228","type":"inject","z":"ea150be4.370218","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":430,"y":120,"wires":[["57313806.5355e8"]]},{"id":"5420ca87.6bc1d4","type":"template","z":"ea150be4.370218","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Processed {{payload.length}} files","x":1160,"y":200,"wires":[["910ee8fb.f36198"]]},{"id":"9706d9df.0f3b38","type":"template","z":"ea150be4.370218","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This filename is \"{{payload.filename}}\"","x":780,"y":200,"wires":[["62e49bf8.fff2c4","21bf6987.4c4ff6"]]},{"id":"db970231.874ab","type":"zip","z":"ea150be4.370218","name":"","mode":"decompress","filename":"","outasstring":false,"x":430,"y":200,"wires":[["e9d2d77f.490a48"]]},{"id":"57313806.5355e8","type":"file in","z":"ea150be4.370218","name":"","filename":"c:/temp/images.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":650,"y":120,"wires":[["db970231.874ab"]]},{"id":"e9d2d77f.490a48","type":"split","z":"ea150be4.370218","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":580,"y":200,"wires":[["9706d9df.0f3b38","6e7dd561.f426cc"]]},{"id":"62e49bf8.fff2c4","type":"join","z":"ea150be4.370218","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":970,"y":200,"wires":[["5420ca87.6bc1d4","67706051.fe865"]]},{"id":"6e7dd561.f426cc","type":"debug","z":"ea150be4.370218","name":"split","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":260,"wires":[]},{"id":"21bf6987.4c4ff6","type":"debug","z":"ea150be4.370218","name":"template1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":820,"y":260,"wires":[]},{"id":"67706051.fe865","type":"debug","z":"ea150be4.370218","name":"join","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1010,"y":260,"wires":[]}]
2 Likes

Thank you for your support!

Need a loop because corresponding to the processing of each TXT file, if it fails, it will do other things such as output the file to the Error folder (1), send mail to report the error file (2), ... and this also needs to be done with the nodes of Node-RED.

If there is no separate loop, but write the loop in function node, things like (1), (2), ... cannot be done by node of Node-RED.

Again, it is VERY likely you do NOT need to loop. You need to learn node-red.

The "generate result output" node could be ANYTHING. It could be bunch of nodes that process a file, check it for errors, raising an error, processing data - it could be anything. It could be a subflow that does a whole bunch of pre-processing.

Perhaps you do not realise that split node generates new msg objects for you to work on individually (just like a loop would). After the split node, you might as well pretend you are a "loop" as you get access to the individual parts of the array to do as you please.

So again - why MUST there be a loop???



This doesnt make a whole lot of sense. Can you explain with details?

2 Likes

I am trying to write a flow that will extract a zip file containing TXT files, from the extracted TXT files, check the contents of each TXT file.

If the checking file content fails, it will move the TXT file to the Error folder (1), send a mail report to the TXT file (2), ... and continue to process the next TXT file.
Things like (1), (2) I also want to do with Node-RED's node

So I think there's a loop for all of this. As a newbie to Node-RED, I do not know what best practices should be in this case. Looking forward to your guidance!

@minhhn0205

I know this can be very frustrating and I know people think differently.

What you have said there is still vague.
Sorry.

So, reading what you said:

You have a zip file. (contents TXT files.)

@Steve-Mcl
I only sort of get what you mean, but alas that too is a bit vague for me, let alone a newbie.

I am a bit stuck how to write a flow to extract multiple files from one source file; check each file and act accordingly.

1 Like

What are you struggling with?

He says he wants to loop files - I said its not required (so did @bakman2)

Like this (no loop)

I dont think you are understanding the nature of node-red and messages.

After the SPLIT node, you have your individual files - no loop needed

2 Likes

Oops, I just realised I didn't really complete what I was saying.

Do you have multiple ZIP files which need to be unZipped?

If you do, that is another level of looping you may not have noticed.

So: Let's say you have only one.

You want to unzip it, examine the files in it and do things depending on what happens.
And you are doing that by looping.

Rather than that how about this method:
1 - Test the zip integrity. Good/Bad.
2 - Extract files to a temporary place.
3 - Build an index of the files. (You could call this a loop.)
4 - Test each of the files in the index. Act accordingly.

So, I guess you could say is that the index is a loop.
But that is at a more basic level for Node-Red, so you can do functions (zip stuff) on the files and see the results. (Pass / Fail).

1 Like

that has nothing to do with the OPs issue but for you information - look at the debug output I posted

image

the files are split out of the ZIP payload.

1 Like

I'll be brief:

Keh?

1 Like

um - he wants to process the file data.

If you looked at or imported the flow I posted - you will see...

1 Like

(I'm kind of glad I am only learning. Or trying to.)

I'm outa here.

Since I am new to Node-RED, I am looking to practice Node-RED node manipulation.

I would like to output like your sample:


by doing a loop like below, but do not know where is wrong and the results are not correct...
Please tell me! Thanks.

[{"id":"6683e387.1412ec","type":"template","z":"3b501765.6a7958","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","x":1070,"y":1280,"wires":[["b06fdcae.0566f"]]},{"id":"b06fdcae.0566f","type":"switch","z":"3b501765.6a7958","name":"i<msg.payload","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"payload","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1360,"y":1280,"wires":[["ad7b00b5.afe92","2e2306e9.7c408a"],["1d1d7b28.2d9f95"]]},{"id":"1b90939b.ddff3c","type":"function","z":"3b501765.6a7958","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":1530,"y":1120,"wires":[["b06fdcae.0566f"]]},{"id":"72ff57b2.8d9b98","type":"debug","z":"3b501765.6a7958","name":"msg end","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1840,"y":1320,"wires":[]},{"id":"6fd1f00e.0c2f2","type":"debug","z":"3b501765.6a7958","name":"msg output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1870,"y":1240,"wires":[]},{"id":"6eb1638.8492e9c","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":710,"y":1120,"wires":[["1e822a01.417016"]]},{"id":"f56ab556.176988","type":"comment","z":"3b501765.6a7958","name":"conditional for while ↑","info":"","x":1360,"y":1320,"wires":[]},{"id":"a1f1f218.97fdd","type":"comment","z":"3b501765.6a7958","name":"↓ init value i to increment","info":"","x":1130,"y":1240,"wires":[]},{"id":"eac8b268.578e","type":"comment","z":"3b501765.6a7958","name":"↓ count up i","info":"","x":1550,"y":1080,"wires":[]},{"id":"1d1d7b28.2d9f95","type":"template","z":"3b501765.6a7958","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"End of loop","x":1620,"y":1320,"wires":[["72ff57b2.8d9b98"]]},{"id":"ad7b00b5.afe92","type":"template","z":"3b501765.6a7958","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This filename is {{payload.filename}}","x":1620,"y":1260,"wires":[["1b90939b.ddff3c","6fd1f00e.0c2f2"]]},{"id":"79958d94.6dc6a4","type":"zip","z":"3b501765.6a7958","name":"","mode":"decompress","filename":"","outasstring":false,"x":770,"y":1280,"wires":[["73a018f3.085488","5d38e308.fc926c"]]},{"id":"73a018f3.085488","type":"debug","z":"3b501765.6a7958","name":"msg zip","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":1360,"wires":[]},{"id":"6d0dc499.b9d6ec","type":"comment","z":"3b501765.6a7958","name":"output array file txt ↑","info":"","x":730,"y":1320,"wires":[]},{"id":"1e822a01.417016","type":"file in","z":"3b501765.6a7958","name":"","filename":"/Temp/FOLDER_TEXT.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":950,"y":1120,"wires":[["79958d94.6dc6a4"]]},{"id":"5d38e308.fc926c","type":"function","z":"3b501765.6a7958","name":"","func":"msg.payload = msg.payload.length;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":1280,"wires":[["92139f11.34ea8","6683e387.1412ec"]]},{"id":"92139f11.34ea8","type":"debug","z":"3b501765.6a7958","name":"msg function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":1340,"wires":[]},{"id":"2e2306e9.7c408a","type":"debug","z":"3b501765.6a7958","name":"msg switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1550,"y":1220,"wires":[]}]

OK, so you can see the output in my debug works - yet you still want to use a loop - I dont understand.


you also say

and

My advice on best practice in this particular situation is...

"DO, NOT, USE, A, LOOP!"



I recommend watching this playlist: Node-RED Essentials. It is by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in less than an hour.

I strongly recommend you watch these videos.


If you STILL want to do a loop - I am certain someone else can assist.

2 Likes

Kiyo, maybe I can explain what @Steve-Mcl has been trying to get across.

The zip node will unzip the file you provide and output a msg. In that msg will be msg.payload that will contain an array where each item in the zip file will have an entry. For example, I created a zip file of two text files. If you look at the output, you will see that msg.payload is an array of two elements:
Screen Shot 2020-09-03 at 6.08.11 AM

Each array item contains the filename and the content of that file
msg.payload[0].filename
msg.payload[0].payload

Now if you send that msg into a split node, the split node wil output two messages, one for each file that was in the zip file.

At this point you can add logic to check each file and set a status in the msg. Then use a switch node to examine the status. If it fails, send the message along a 'failed' leg of the flow and it it passes, send it along a 'success' leg of the flow.

Here is an example where the two link nodes would go to your code for passing and failing.

2 Likes

A post was split to a new topic: Best way to code this