问题
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!
回答1:
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:)
来源:https://stackoverflow.com/questions/18683345/cycle-through-a-directory-and-get-paths-of-all-the-files-and-folders