how to read all Files in a folder

Hi, I would like to count  all files on my CacheDirectory plus I want to modify it, where I can delete a file if it is not detected on my code.

like for example:

CachesDirectory contains

1.txt

2.txt

3.txt

In my supposed code I have a table named as “a” like this:

a ={1,2}

now, my plan is supposed to be in a loop,

if anything that is not in a has been detected in CachesDirectory, delete it, if not keep it.

thanks in advance I hope this is possible

something like this:

[lua]

local lfs = require “lfs”

local path = system.pathForFile( “”, system.CachesDirectory )

local a = {“1.txt”, “2.txt”}

for file in lfs.dir(path) do

if not table.indexOf( a, file ) then

os.remove( system.pathForFile( file, system.CachesDirectory  ) )

end

end

[/lua]

OOh got it thanks though :slight_smile: what is lfs though?

nvm found it, thanks though :slight_smile:

http://docs.coronalabs.com/api/library/lfs/index.html

something like this:

[lua]

local lfs = require “lfs”

local path = system.pathForFile( “”, system.CachesDirectory )

local a = {“1.txt”, “2.txt”}

for file in lfs.dir(path) do

if not table.indexOf( a, file ) then

os.remove( system.pathForFile( file, system.CachesDirectory  ) )

end

end

[/lua]

OOh got it thanks though :slight_smile: what is lfs though?

nvm found it, thanks though :slight_smile:

http://docs.coronalabs.com/api/library/lfs/index.html