Ok, I have the solution > credit goes to David Torno and chauffeurdevan over on the adobe-forums!
> I made quite an error in logic when comparing the strings because the numbered layers do not necessary have to match the current i-value in the for-loop. As soon as there are items BEFORE the numbered layer-folders in the project panel, or items IN those numbered folders, the item-index and ‘i’ don’t match anymore..
Here’s a very nice RegExp-solution for this:
if ((app.project.item(i) instanceof FolderItem) && (new RegExp(/^layer\d+/).test(app.project.item(i).name))) .... (C) by chauffeurdevan