0

Hello there im stuck on this thing where i need to cycle through a certain dir that i need to get all the file names from and get there paths as a variable.

I know how to create a loop, but to get the directory's contents, I do not.

Any help is appreciated, thanks!

Rstew
  • 585
  • 2
  • 9
  • 21
  • 1
    Look at the NSFileManager class there are methods in there to help you. – Abizern Sep 08 '13 at 12:55
  • I know about NSFileManager, Im just stuck with how to use it in order to get the paths of the files – Rstew Sep 08 '13 at 13:09
  • 1
    `enumeratorAtPath:' (for example) does that not tell you anything? The doc even has sample code – Abizern Sep 08 '13 at 13:47
  • You could read the documentation for NSFileManager. Note that it has a list of methods that do things. Some of them return lists of files, etc. – Hot Licks Sep 08 '13 at 20:34

1 Answers1

3

Try like this:

// If your folder is a document.
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:  @"Documents"];
// else you can give your folder path as well, if you know 
// for example like this NSString *docsDir = @"user/desktop/testFolder"

NSFileManager *localFileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
NSString *file = nil;
NSData *fileContents = [NSData data];
while ((file = [dirEnum nextObject])) 
{
   NSLog(@"your file name%@",file); // This will give your filename
   // Now for getting file path follow below.
   // here we are adding path to filename.
   NSString *fileNamePath = [docsDir stringByAppendingPathComponent:file];
   NSLog(@"your fileNamePath%@",fileNamePath); // This will give your filename path
   fileContents = [NSData dataWithContentsOfFile:fileNamePath]; // This will store file contents in form of bytes

}

Hope it helps:)

CRD
  • 52,522
  • 5
  • 70
  • 86
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56