Splitting 1 item of binary files into 1 item per file

Hello,
I managed to decompress a zip file containing a dozen of files thanks to a Compression node. The output is only 1 item containing all the files as binary data.

I would like to extract text and filename from each file but I cannot loop on the output, as it is only 1 item !
I saw a closed question with a code node workaround to split 1 list of binary files into several items, but the script does not work any more.

Here is the closed question : Process decompressed files
Here is the old script given as an answer to this question:

let results = ;

for (item of items) {
for (key of Object.keys(item.binary)) {
results.push({
json: {
fileName: item.binary[key].fileName
},
binary: {
data: item.binary[key],
}
});
}
}

return results;

Thank you for your help!

Just an update to say that I finally copied the old Code component from the template to my workflow and it worked!

(I had not thought of this simple solution, as I am still not used to workf on n8n)

I am still wondering how to do the same thing in the new “Code” node, but for now I have a way to make my workflow run.

Hey @c.le hope all is good. Welcome to the community.
Just tried the code you posted above. Please see the result.

As you can see on the screenshot: 1 item coming in 2 items going out.

Workflow

Could you please clarify when you say “does not work”, what do you mean exactly? Could you show an example of this not working?

1 Like

Hello @jabbson , I hope all is good for you too!
Thank you for looking into my issue: I realize now that I really need time to understand how to use n8n…
I actually did not try to use the code as I found it.

When I initially copied the code into my node, I saw an error under the “items” variable and altered the code immediately, before even trying.

As the error was “Cannot find name ‘items’. Did you mean ‘item’? “, I put “input.all()” instead of “items” and as it did not work (of course), I tried many other fruitless alterations…

The error message mislead me, sorry!

1 Like

Glad to hear that the code worked. Sometimes the editor will show you error even if there are none, I don’t trust it :slight_smile:.

You could have made this work with input.all() too.

let output = [];

for (const item of $input.all()) {
    for (key of Object.keys(item.binary)) {
        output.push({
            json: {
                fileName: item.binary[key].fileName
            },
            binary: {
                data: item.binary[key],
            }
        });
    }
}

return output;
1 Like

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