List of files in particular folder

Hi guys,

I’m trying to populate a table with a list of filenames from a given directory, and I’m failing miserably. 

I can list all of the files using the following method:

local lfs = require ( "lfs" ) local path = system.pathForFile( "", system.DocumentsDirectory ) for file in lfs.dir ( path ) do print (file) end

but I can’t figure out how to insert the filenames into a table.

Ideally what I’d like to do is to scan through a particular subfolder in the users pictures folder and only select the files of a certain type ( .png ).  This is so the user can select a folder and use the pictures it contains to design a game level.

If anyone could help that would great.

Cheers

Chris

Hi Chris

try this

local lfs = require ( "lfs" ) local path = system.pathForFile( "", system.DocumentsDirectory ) local pngFiles = {} for file in lfs.dir ( path ) do     if string.find( file, ".png" ) then         table.insert( pngFiles, file )     end end

Brilliant thanks ojnab :slight_smile:

Hi Chris

try this

local lfs = require ( "lfs" ) local path = system.pathForFile( "", system.DocumentsDirectory ) local pngFiles = {} for file in lfs.dir ( path ) do     if string.find( file, ".png" ) then         table.insert( pngFiles, file )     end end

Brilliant thanks ojnab :slight_smile: