循环浏览目录并获取所有文件和文件夹的路径 [英] Cycle through a directory and get paths of all the files and folders

查看:279
本文介绍了循环浏览目录并获取所有文件和文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello在那里我坚持这个东西,我需要循环通过一定的 dir ,我需要得到所有的文件名,并得到路径作为一个变量。

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.

推荐答案

尝试这样:

// 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

}

它有助于:)

这篇关于循环浏览目录并获取所有文件和文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆