Scanning files in folder of system.ResourceDirectory

Hi there,

I have som folders in the root of my app project.

One of them is called images - and I can acces it just fine and work with the images, also in subfolders like “images/ui” etc.

However, I also have one folder called “images/tracks/trackname” - and in that folder I have some .png files.

What I’m trying to do is, loop through the files in “images/tracks/trackname” and add them to an array.

After this I can then dispaly them in an animation sequence based on the array.

I’m using the code snippet from this tutorial to loop through the files :http://coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/  - but it fails for me.

Any pointers to steer me in the right direction ?

Best Wishes

/Thomas

This should get you started:

local lfs = require "lfs" local doc\_path = system.pathForFile( "", system.ResourceDirectory ) for file in lfs.dir(doc\_path) do --file is the current file or directory name print( "Found file: " .. file ) end

Some more information:

http://coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

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

Best regards,

Tomas

Hi there Tomas,

Thank you very much for the snippet and the link.

I already have that working - my problem is scanning a dir within  the system.ResourceDirectory.

The folder structure is:

system.ResourceDirectory

  |—images

           |----tracks

                      |—track1 (I need to scan this folder for files and add all .png files to an array)

I have tried :

local doc_path = system.pathForFile( “”, system.ResourceDirectory…“images/tracks/track1” )

But it throws an error - not sure what I’m doing wrong.

Best Wishes

/Thomas

Have you tried:

local doc\_path = system.pathForFile( "images/tracks/track1", system.ResourceDirectory )

You can see in the documentation that the first argument is the filename and the second is the base directory:

system.pathForFile( filename [, baseDirectory] )

Think of the baseDirectory as C: or D: on a Windows drive. You need to change drive to be able to copy between them. The same goes for iPhone and Android. the system.ResourceDirectory is one and another one is system.DocumentsDirectory.

When we have changed the base directory we can now, from the location either change the directory (lfs.chdir) or reference to our file through it’s fully qualified path (relatively to the base directory of course):

filename (required)

String. The name of the file. Alternatively, a path to the file that is relative to baseDirectory.

baseDirectory (optional)

Constant. Constant corresponding to the base directory where the file is located. Default value is system.ResourceDirectory if the parameter is not provided.

http://docs.coronalabs.com/api/library/system/pathForFile.html

Best regards,

Tomas

First, based on your first message, I understand that you want to create an animation based on several image files. If that is correct, you should do it using sprites and not looping thru several independent images.

You can read about sprites here: http://docs.coronalabs.com/guide/media/imageSheets/index.html

Now about listing files:

You will have problem trying to list all files on Resources Directory on Android. You can do a work-around by having all files written in sequence so you try to open each one by doing a loop, but even that way on Android will work only for specific files types.

In the end, is that you are supposed to know what is inside of your Resources Directory, so you should be able to access it directly and not having to list the files inside it.

You can read more about that below:

http://coronalabs.com/blog/2012/05/16/faq-wednesday-5/

http://coronalabs.com/blog/2013/02/13/faq-wednesday-sub-folder-and-file-access/

http://forums.coronalabs.com/topic/43171-problems-determining-if-image-file-exists-on-android/

Thank you so much for the answers - this forum, as well as the engine, is amazing.

Tomas, you’r suggestion worked perfectly - thank you so much for the nice and clear explanation.

What confused me was the tag “filename” - since i wanted to scan a folder. But in this case, they are the same.

That said, Jedi - ofcourse you’r right !
I “kindda” forgot that I’m using a 2D game engine to make my Business app :slight_smile:

Stupid me - thank you both for quick and clear answers :slight_smile:

Best Wishes

/Thomas

This should get you started:

local lfs = require "lfs" local doc\_path = system.pathForFile( "", system.ResourceDirectory ) for file in lfs.dir(doc\_path) do --file is the current file or directory name print( "Found file: " .. file ) end

Some more information:

http://coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

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

Best regards,

Tomas

Hi there Tomas,

Thank you very much for the snippet and the link.

I already have that working - my problem is scanning a dir within  the system.ResourceDirectory.

The folder structure is:

system.ResourceDirectory

  |—images

           |----tracks

                      |—track1 (I need to scan this folder for files and add all .png files to an array)

I have tried :

local doc_path = system.pathForFile( “”, system.ResourceDirectory…“images/tracks/track1” )

But it throws an error - not sure what I’m doing wrong.

Best Wishes

/Thomas

Have you tried:

local doc\_path = system.pathForFile( "images/tracks/track1", system.ResourceDirectory )

You can see in the documentation that the first argument is the filename and the second is the base directory:

system.pathForFile( filename [, baseDirectory] )

Think of the baseDirectory as C: or D: on a Windows drive. You need to change drive to be able to copy between them. The same goes for iPhone and Android. the system.ResourceDirectory is one and another one is system.DocumentsDirectory.

When we have changed the base directory we can now, from the location either change the directory (lfs.chdir) or reference to our file through it’s fully qualified path (relatively to the base directory of course):

filename (required)

String. The name of the file. Alternatively, a path to the file that is relative to baseDirectory.

baseDirectory (optional)

Constant. Constant corresponding to the base directory where the file is located. Default value is system.ResourceDirectory if the parameter is not provided.

http://docs.coronalabs.com/api/library/system/pathForFile.html

Best regards,

Tomas

First, based on your first message, I understand that you want to create an animation based on several image files. If that is correct, you should do it using sprites and not looping thru several independent images.

You can read about sprites here: http://docs.coronalabs.com/guide/media/imageSheets/index.html

Now about listing files:

You will have problem trying to list all files on Resources Directory on Android. You can do a work-around by having all files written in sequence so you try to open each one by doing a loop, but even that way on Android will work only for specific files types.

In the end, is that you are supposed to know what is inside of your Resources Directory, so you should be able to access it directly and not having to list the files inside it.

You can read more about that below:

http://coronalabs.com/blog/2012/05/16/faq-wednesday-5/

http://coronalabs.com/blog/2013/02/13/faq-wednesday-sub-folder-and-file-access/

http://forums.coronalabs.com/topic/43171-problems-determining-if-image-file-exists-on-android/

Thank you so much for the answers - this forum, as well as the engine, is amazing.

Tomas, you’r suggestion worked perfectly - thank you so much for the nice and clear explanation.

What confused me was the tag “filename” - since i wanted to scan a folder. But in this case, they are the same.

That said, Jedi - ofcourse you’r right !
I “kindda” forgot that I’m using a 2D game engine to make my Business app :slight_smile:

Stupid me - thank you both for quick and clear answers :slight_smile:

Best Wishes

/Thomas