Reading from SPIFFS - What's the best way to deal with a string (or char) array with undefined length?
Well, you cannot use more memory than you have. If the length of file names is (probably/possibly) too much, you just cannot store it in memory. Some alternatives:


Use some compression... This will save you a bit, but at the end if you don't know how much the maximum bytes you need to store is, it will not help much
Use external SRAM memory... You need to write your own memory functions. Also a slight performance hit.
Use SD card memory... As SRAM, but much slower. Also not sure if you can use the same SD card to write on.
The best solution, but could be a big performance hit, is not to store at all the file names, but go through the file names and process what you need along the way. This is not always possible or feasible though. In your solution you can print the file names directly without storing (but I guess you want to do some additional processing).