Cycle through a directory and get paths of all the files and folders [closed]

亡梦爱人 提交于 2019-12-29 02:08:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!