Scanning sub directory for files

Hey guys,

I am trying to scan a sub directory for whatever files might be in a certain folder.  The example code that is given is fine except that it only shows how to scan the system.DocumentsDirectory (The root), but on IOS I have found that you cannot use “system.pathForFile” to get a path to a directory, it has to be an actual file.  Can someone please help me figure out how to get a path to just a folder itself so I can then scan it for files?  Here is what I’m attempting…

By the way, I am attempting to look at sub folders that were included in the app package, so its part of “system.ResourcesDirectory”.

--Get the path to the root of the Resources directory local doc\_path = system.pathForFile( "", system.ResourcesDirectory ) --Append onto the path to the folder I am attempting to access doc\_path = doc\_path.."images/"..localProperties.imageDir print(doc\_path) --This now fails when trying to read the path for file in lfs.dir(doc\_path) do if string.find(file, "IMG") then local fileName = string.sub(file, 1, #file - 7) table.insert(slides, "images/"..localProperties.imageDir.."/"..fileName.."IMG.png") table.insert(slideNames, fileName) end end

Any thoughts would be great, been stuck on this for a long while now.

Hi there,

You’re missing a “/” when appending the folder onto the path. It should be: 

doc_path = doc_path…"/images/"…localProperties.imageDir

Also you need local lfs = require(“lfs”) at the top (if you don’t already have that). Otherwise it won’t run on an actual device.

However, this code will only work when run on a device. It will not work in the Corona Simulator as the returned Resource directory points to the Corona Simulator as opposed to your app. I think this is a bug which I’m going to report.

Bug report filed as Case 27590.

I was under the impression that Corona couldn’t access the local file system of a device. I’m not near my machine to test this code; please let us know how you get on.

Yeah, I forgot about Android… The code will only work on iOS.

More info here:

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

@ingemar,

thanks for the response.  Unfortunately it still is not working on device for me.  I’m using the logger in xcode while I run the app, and when that code gets executed my app crashes on the device and there are no helpful messages in the logger. =(

I did have the print statement for my path and it says “/var/mobile/Applications/numbers&symbols here/myAppName.app/image/exteriorDoors”

As far as I can tell that path is what I am looking for.  The exteriorDoors folder just has images inside of it, and that is the folder I wanted to scan for files.  Any other ideas?

I found this info, but again I can’t really test it:

http://stackoverflow.com/questions/8443291/how-to-load-images-from-sdcard-in-corona-sdk

@panc software,

Thanks  but that is a different issue than what I am looking at.  I am working with an IOS only app and also just trying to read from the system.ResourcesDirectory.

I have found a post in stack overflow of a guy that seems to have found a solution, but I can’t get it to work. here it is:
 

"After much debugging I have figured out the solution for Mac. Below is my function for getting files in a directory. Works for both Windows and Mac. Must have a file named main.lua in the baseDirectory. -- path is relative to the baseDirectory function getFiles(path) local filenames = { } local baseDir = system.pathForFile('main.lua'):gsub("main.lua", "") for file in lfs.dir(baseDir .. path) do if (file ~= '.' and file ~= '..') then table.insert(filenames, file) end end if (#filenames == 0) then return false end return filenames end The problem is system.pathForFile('directory/subdirectory') does not work on Mac as it is strictly looking for a file. As long as it can find some file in the baseDirectory you can append on the relative path to retrieve all files in the intended directory."

I have attempted to use the above code but get an error on my path like always. When he says that the “baseDirectory” must have a file named “main.lua”, does that mean my “main.lua” in the system.ResourcesDirectory should work?

EDIT: This solution works on MAC but fails on my actual iPAd, still trying to figure it out…

When calling it I just passed a string of the directory path instead of trying to pass it what “system.pathForFile” would return.

For example:

local thisPath = "images/"..subDirectoryName local theFiles = getFiles(thisPath)

It then returns a table to “theFiles”, which you can iterate through to find your file names. Hope this helps someone else as well.

Ok, maybe someone can answer this question for me.  Why is it that if I attempt the following, I get nil?

local thisDir = system.pathForFile("main.lua", system.ResourcesDirectory) print(thisDir) --Outputs nil

I can’t believe that I can’t even get a path to the main root of the resources directory?!  This is on device, what is wrong here?

I officially cannot get a path to a file found in the system.ResourcesDirectory on device.  I can however get a path to anything in the system.DocumentsDirectory.

Is this perhaps a bug in IOS7?

The reason why the code you found doesn’t work on device is because there is no “main.lua” in the resource directory after compiling to device (it’s there when you run in the Corona Simulator though).

But… Your original code that you posted *does* work on device (with the minor path-modification I mentioned). I tested it myself on my iPad mini and iPod Touch. If I had to guess, I’d say that maybe the “&” in the path name is tripping something up…

Hey… wait a minute!

You’ve mis-spelled a part of your code. It should be  system.ResourceDirectory not system.ResourcesDirectory  :).

If you use your original code and just change that (and add the extra “/”) it will work on a device.

Corona simulator will still fail due to what I think is a bug (which I’ve reported).

@ingemar,

Nice catch!  And thanks for reminding me to make that small fix with a “/”.  That finally did work.  I got so mixed up with all the different methods I was trying to do that I got lost and thought nothing would work.

Thank you very much for your help, saved my sanity!

Great news  :slight_smile: Glad I could help…

Hello everyone!

I have a question, where should I add this extra “/” ??
Eg I have a function we check lfs in a given path that is received by parameter is a directory, and returns true or false. If true then return through the lfs.dir (path) traverses the path and checks the existence of images.
Where should I add the extra “/”?

Sorry for my bad english…

  :slight_smile:

That depends on how your function returns the directory. Just make sure all directories are separated by “/”.

The example from the OP had some code that did this: doc_path = doc_path…“images/”…localProperties.imageDir

The code needed an extra “/” like this: doc_path = doc_path…"/images/"…localProperties.imageDir

Wait, now I got another doubt, this extra “/” will enable use in lfs is only for android or IOS? Maybe he figured wrong, because what I really wanted was to use lfs for android because I already can for IOS.
Needed to access files that are in the resource (Android).

I really needed a way for me to check if a given path is a directory on Android, and if travel that same directory and check all files.

Any idea??