Question

My project has a folder (not group) named "data" which contains many subfolders, each of which contains a set of files.

My question is, how do I grab a URL (or path) reference to the "data" folder? How about to its subfolders? I'm sure it's a fairly simple task, so forgive my ignorance, but I've never used folder refs in a project so I'm not familiar with the code. I did look over the NSFileManager ref but I'm fuzzy on how to make use of it.

Thanks in advance.

Was it helpful?

Solution

NSBundle can give you your absolute path in the system.
NSString (or NSURL) has methods for working with paths.
NSFileManager allows you to move, copy, delete (…) files.

This is how you get path to your Data directory:

NSString *dataDir = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Data"];
// "/var/private/Application/.../YourApp.app/Data"

Now you just append multiple directory names to dataDir using the same method above and you should get any path you want.

In case you don't know the exact path and you want to scan the directory, you will have to use :

NSArray *dataDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataDir error:nil]
// "file1.data", "file2.data", ...

Then it's all about appending path components.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top