计算目录和子目录中的文件数? iPhone
我正在尝试计算目录中的整个文件,包括子目录。这就是我必须计算第一个文件夹中的文件的数量:
-(NSString *)numberOfPhotos
{
NSString *MyPath = @"/rootdirectory/";
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath:MyPath];
return [NSString stringWithFormat:@"%d", [directoryContent count]];
}
我在想也许是类似的东西
for (file in folder){
[file count]
{
,但似乎不起作用。
更新: 其实,很简单:
NSDirectoryEnumerator *subs = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:musicPath error:nil];
I am trying to count entire files in a directory, including subdirectories. This is what I have to count files in the first folder:
-(NSString *)numberOfPhotos
{
NSString *MyPath = @"/rootdirectory/";
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath:MyPath];
return [NSString stringWithFormat:@"%d", [directoryContent count]];
}
I was thinking maybe something like
for (file in folder){
[file count]
{
but doesnt seem to work.
UPDATE:
Actually, was very easy:
NSDirectoryEnumerator *subs = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:musicPath error:nil];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你走在正确的轨道上。你需要一个递归方法。您传入一个目录,该方法会获取该目录中的所有文件,然后检查每个文件以查看它是否是一个目录。这里你有一个 for 循环来检查当前对象是否是一个目录。如果它是一个目录,那么它将使用目录名称来调用自身。如果不是,它将计数器加一并继续。
现在无法发布代码,但这就是您要做的事情。
You're on the right track. You need a recursive method. You pass in a directory, the method grabs all of the files in the directory, then checks each one to see if it is a directory or not. Here you'd have a for loop to check if the current object is a directory. If it is a directory, then it would call itself with the directory name. If not, it increments a counter by one and continues.
Can't post code right now, but that's the way you'd do it.
看看我能不能用iPad键盘写代码...用伪代码来描述递归算法:
See if I can write code using the iPad keyboard... Using pseudo code to describe the recursive algorithm: