iSagar
iSagar

Reputation: 107

how to read files of a directory?

FM = [NSFileManager defaultManager];
directoryContents = [FM contentsOfDirectoryAtPath:@"/Users/myComputer/Library/Application Support/iPhone Simulator/4.3/Applications/CD88649C-E545-4E10-84DF-F4E5D829641B/Documents" error:NULL];

but it gives me object not the path for object.

Upvotes: 4

Views: 613

Answers (3)

iSagar
iSagar

Reputation: 107

I found out how to read files stored in directory. By using:

[[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil]

It returns the file or folder name stored at that location. Then we can again append these names in a string to read these files.

Upvotes: 3

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
for (NSString *tString in dirContents) 
{
  //Do some stuff here  
}

See this Post Get directory contents in date modified order

Upvotes: 2

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

I really not understand the hardcoded string for the bundle path use below for getting the bundle path

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];

your code must be similar with below.

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];

Upvotes: 0

Related Questions