Copy folder from ResourceDirectory to DocumentsDirectory

Hi Corona folks,

I need to copy a folder from ResourceDirectory to DocumentsDirectory on iPad. I expected to find something like os.copyDir or lfs.copyDir but I’m coming up empty. There is the os.rename function which almost works except that it deletes the original folder and since ResourceDirectory is read-only I doubt that will work on the device.

Am I really going to have to recursively read each file/folder from source and do a write for each file/folder? Seems like a common task that Lua/Corona (or other folks) would have handled already.

Thanks for any help,

JB Tellez

See the sample code provided in the documentation pages : 

http://docs.coronalabs.com/api/type/File/write.html

Hope this helps.

Thanks ksan,

So I guess I’ll have to recursively loop through the directory to create new folders and read/write files to those new directories. Not the worst programming chore ever, but I still can’t believe that something like an ‘xcopy’ function doesn’t already exist :( 

Appreciate the help

You’re most welcome. You could write a neat function that does it all I suppose. Here’s another idea… If you put your files in the right directory stucture in a zip file and then copy that over to Documents Directory and unzip it there then all the copy and folder structure creation work should be taken care of by the zip plugin. Please check if the zip plugin supports this though. I’m going on a theory here but it could do the job.

Great idea! I’ll look into that. I’m fiddling around with os.execute(‘cp’) right now. Will definitely report back once I’ve got something working.

Zipping did the trick. Thanks so much for the idea!

Great!!! I am happy to hear this worked. Its always great when you can offload some work to components / plugins etc. 

See the sample code provided in the documentation pages : 

http://docs.coronalabs.com/api/type/File/write.html

Hope this helps.

Thanks ksan,

So I guess I’ll have to recursively loop through the directory to create new folders and read/write files to those new directories. Not the worst programming chore ever, but I still can’t believe that something like an ‘xcopy’ function doesn’t already exist :( 

Appreciate the help

You’re most welcome. You could write a neat function that does it all I suppose. Here’s another idea… If you put your files in the right directory stucture in a zip file and then copy that over to Documents Directory and unzip it there then all the copy and folder structure creation work should be taken care of by the zip plugin. Please check if the zip plugin supports this though. I’m going on a theory here but it could do the job.

Great idea! I’ll look into that. I’m fiddling around with os.execute(‘cp’) right now. Will definitely report back once I’ve got something working.

Zipping did the trick. Thanks so much for the idea!

Great!!! I am happy to hear this worked. Its always great when you can offload some work to components / plugins etc.