Is there a function to obtain a listing of all the files in the file directory?
-Frank.
[import]uid: 8093 topic_id: 2149 reply_id: 302149[/import]
Is there a function to obtain a listing of all the files in the file directory?
-Frank.
[import]uid: 8093 topic_id: 2149 reply_id: 302149[/import]
There is no function to list the files in a directory. Since your app is the one that creates the files in the Documents or Temporary directory you can check if a specific file does exist using the io.open function.
-Tom [import]uid: 7559 topic_id: 2149 reply_id: 6441[/import]
I want to drive part of the app thru file naming conventions.
Like picture files for a sprite/movie as pic-1.png, pic-2.png, pic-3.png.
and let the app find out how many pictures make up the movie by iterating over those files that fit the pattern.
If I use display.newImage( fname ), it will barf when the file doesn’t exist.
I could open the file with io.open to test for its existence and close it again if it does…and then call display newImage().
I hope you agree that that is kind of ugly…
The other usage is for themes, where again thru a naming convention the user would have the option to chose a theme, which would map to a set of pattern-defined png-files.
Now the Document directory is supposed to persist across different invocation, which means that if the user downloaded different themes from my website in that directory, you wouldn’t be able to find them as you only know the pattern but not the exact name.
In other words… please give us a way to see the files in a directory - all directories that is: DocumentsDirectory, TemporaryDirectory and ResourceDirectory.
Unless there is a reason that you cannot provide that function (?), but then it would be helpful to explain why not.
Thanks, Frank.
[import]uid: 8093 topic_id: 2149 reply_id: 6443[/import]
I just found out that you can use “system.pathForFile( “Icon.png”, system.ResourceDirectory )” to test for a file’s existence as it returns either a string with the file-path or nil when the file is not present.
Guess there must be some directory lookup in there 
The only annoying thing is that it always prints out a warning on the console:
“WARNING: Cannot create path for resource file ‘worm-head-hungry-5.png’. File does not exist.”
Would it be ok to use that call to simply stat a file?
Can I turn that warning off?
Thanks, Frank.
PS. However, this still would not give you a directory listing…which would be a nice feature to have as I mentioned before. [import]uid: 8093 topic_id: 2149 reply_id: 6446[/import]
Note that the trick to use “system.pathForFile( “Icon.png”, system.ResourceDirectory )” to test for the file’s existence ONLY works for the “system.ResourceDirectory”!
In the case of the DocumentsDirectory, a call to system.pathForFile( “Icon.png”, system.DocumentsDirectory ) will return a string with the full path name also when the file doesn’t exist (yet).
I guess the reason for the difference is that the ResourceDirectory is a read-only file system, while you can create new files in the DocumentsDirectory.
-Frank. [import]uid: 8093 topic_id: 2149 reply_id: 6500[/import]
I added this as feature request #1200.
The warning message you see is only in the simulator and would not be present when running on the device. It cannot be turned off.
I tested a work-around that you might be able to use. It seems to work in the Corona simulator, iPhone, and Android (Nexus One). It will not work for files in the Resource directory.
-- Create file of files in /Documents directory
-- Note: This does not work for /Resource directory
-- Creates a text file: "\_files.txt" containing the list of files found
--
local dir = "'" .. system.pathForFile( x, system.DocumentsDirectory ) .. "'"
local fileDir = "'" .. system.pathForFile( "\_files.txt", system.DocumentsDirectory ) .. "'"
os.execute("ls " .. dir .. "\>" .. fileDir)
It places all the files in the /Documents directory in a file in the same directory: _files.txt
You can parse the file by reading it with io.lines (it stores one file per line).
-Tom [import]uid: 7559 topic_id: 2149 reply_id: 6560[/import]
Interesting… I was playing around with a similar “fuzzydir” trick for the ResourceDirectory:
– The fuzzydir.ResourceDirectory array of file names can be used for pattern matching,
– which allows the use of themes and such thru naming conventions
I didn’t realize you were “allowed” to use “os.execute()” - it will solve the issue for the DocumentsDirectory.
The ResourceDirectory is read-only and shouldn’t change - so if Corona could run that script to update the _file.txt every time it builds/reloads, that would give you enough of a solution.
Somewhat of a hack, but it works…
-Thanks, Frank. [import]uid: 8093 topic_id: 2149 reply_id: 6577[/import]
I just found out that you can actually read the ResourceDirectory files with os.execute(“ls…”) and redirect it into a file in the DocumentsDirectory.
Still not pretty, but now you can obtain directory file listings for both the ResourceDirectory and DocumentsDirectory from within your Corona-Lua script.
-FS.
[import]uid: 8093 topic_id: 2149 reply_id: 6615[/import]
I submitted a module to Ansca’s Code contribution section:
http://developer.anscamobile.com/code/coronasystemdirectoryfilenameslua
That code addresses my issues with the availability of directory file listings - hopefully it will be useful for others.
Enjoy, Frank.
[import]uid: 8093 topic_id: 2149 reply_id: 6653[/import]
Thanks Frank.
-Tom [import]uid: 7559 topic_id: 2149 reply_id: 7070[/import]
Unfortunately the code from Tom doesn’t seem to work anymore using iOS 4.2.1. I always get a ‘deny process fork’ message.
I tried FrankS’s code as well (nice stuff, BTW) but I get the same error. Thank’s to both of you guys for trying.
Not being able to get a listing of the files in my apps document folder is a real issue. I think I’ll need to keep track then of every time I create, rename, or delete a file … basically I need to create and maintain a file directory myself.
This is really disappointing. [import]uid: 7101 topic_id: 2149 reply_id: 24175[/import]
this has to be a joke. The dir and ls have got to be the first functions ever made for any OS, this can really be a make or break for me to buy a subscription for corona sdk. Very disappointing. Say for instance if I do end up manually creating a text file with all the files i ever created and somehow this file gets corrupted or deleted then there will be no way for me to access all my data unless I start guessing at the filenames. I am using corona as a patient data collection platform and forcing me the user to manually keep track of all these files just because there is no dir function is just outrageous. Not only did I have to buy an android 2.2 tablet with an arm v7 or higher (this has cost me extra shipping and a big hassle), but now all these restrictions too? Please fix this issue, it should be a given in ANY operating system or sdk
Thanks [import]uid: 55057 topic_id: 2149 reply_id: 39519[/import]
+1, Corona needs a dir() function.
[import]uid: 61132 topic_id: 2149 reply_id: 39551[/import]
+1, Corona needs a dir() function.
[import]uid: 61520 topic_id: 2149 reply_id: 47864[/import]
+1, Corona needs a dir() function. [import]uid: 56133 topic_id: 2149 reply_id: 52711[/import]
+1, Corona needs a dir() function. [import]uid: 83077 topic_id: 2149 reply_id: 76945[/import]
+1 here, definitely the way to list files in the folder is a basic requirement. Any chance to expose some of the functionality of NSFileManager instead of working with os.execute ? [import]uid: 80100 topic_id: 2149 reply_id: 89873[/import]
According to the Corona SDK documentation for the os.execute() API call, it only works on the simulator and not on the device. [import]uid: 19626 topic_id: 2149 reply_id: 89895[/import]
+1, Corona needs a dir() function. [import]uid: 118390 topic_id: 2149 reply_id: 98738[/import]