计算目录和子目录中的文件数? iPhone

发布于 2024-09-01 11:06:00 字数 604 浏览 8 评论 0原文

我正在尝试计算目录中的整个文件,包括子目录。这就是我必须计算第一个文件夹中的文件的数量:

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

养猫人 2024-09-08 11:06:00
NSDirectoryEnumerator *subs = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:musicPath error:nil];
NSDirectoryEnumerator *subs = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:musicPath error:nil];
妄断弥空 2024-09-08 11:06:00

你走在正确的轨道上。你需要一个递归方法。您传入一个目录,该方法会获取该目录中的所有文件,然后检查每个文件以查看它是否是一个目录。这里你有一个 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.

jJeQQOZ5 2024-09-08 11:06:00

看看我能不能用iPad键盘写代码...用伪代码来描述递归算法:

int fileCount(directory)
{
    int count = 0;
    for (file in directory)
    {
        if (isDirectory(file))
            count += fileCount(file);
        else
            count++;
    }
    return count;
}                

See if I can write code using the iPad keyboard... Using pseudo code to describe the recursive algorithm:

int fileCount(directory)
{
    int count = 0;
    for (file in directory)
    {
        if (isDirectory(file))
            count += fileCount(file);
        else
            count++;
    }
    return count;
}                
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文