使用C语言计算一个目录中文件的数量

28

如何在Linux平台上使用C语言计算目录中文件的数量。

4个回答

54

不能保证这段代码能编译通过,而且它只能在Linux和BSD系统上运行:

#include <dirent.h>

...

int file_count = 0;
DIR * dirp;
struct dirent * entry;

dirp = opendir("path"); /* There should be error handling after this */
while ((entry = readdir(dirp)) != NULL) {
    if (entry->d_type == DT_REG) { /* If the entry is a regular file */
         file_count++;
    }
}
closedir(dirp);

3
如果您想处理子目录中的文件,则可以在遇到目录条目时进行递归。请确保在执行此操作时排除“。”和“..”。 - Jay Conrod
做得好,你捕捉到了dirent包含。 - Michiel Buddingh
是的,它们是POSIX函数。 - penguru
2
POSIX只指定d_ino和d_name作为dirent结构的成员。此代码测试d_type,这是特定于平台的。 - Michiel Buddingh
4
如果有人想知道为什么必须循环遍历,以及为什么没有一种能够在常数时间内获取目录中有多少个文件的函数,我找到了这个链接:http://blogs.msdn.com/b/oldnewthing/archive/2009/02/17/9426787.aspx。 - sudo
鉴于答案已经过时且文章已被删除,这是同一篇文章在WayBack Machine(2019-02-08)上的链接:https://web.archive.org/web/20190208213958/https://blogs.msdn.microsoft.com/oldnewthing/20090217-00/?p=19123/ - Marco Carlo Moriggi

6

2
如果您想要包含子目录,您可以使用这个函数,我在我的一些代码中使用它。您可能需要修改它以包括更多的错误检查和支持不同的目录分隔符。
int countfiles(char *path) {
    DIR *dir_ptr = NULL;
    struct dirent *direntp;
    char *npath;
    if (!path) return 0;
    if( (dir_ptr = opendir(path)) == NULL ) return 0;

    int count=0;
    while( (direntp = readdir(dir_ptr)))
    {
        if (strcmp(direntp->d_name,".")==0 ||
            strcmp(direntp->d_name,"..")==0) continue;
        switch (direntp->d_type) {
            case DT_REG:
                ++count;
                break;
            case DT_DIR:            
                npath=malloc(strlen(path)+strlen(direntp->d_name)+2);
                sprintf(npath,"%s/%s",path, direntp->d_name);
                count += countfiles(npath);
                free(npath);
                break;
        }
    }
    closedir(dir_ptr);
    return count;
}

0

如果您不关心当前目录.和父目录..,就像这些一样:

drwxr-xr-x  3 michi michi      4096 Dec 21 15:54 .
drwx------ 30 michi michi     12288 Jan  3 10:23 ..

你可以像这样做:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main (void){
    size_t count = 0;
    struct dirent *res;
    struct stat sb;
    const char *path = "/home/michi/";

    if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)){
        DIR *folder = opendir ( path );

        if (access ( path, F_OK ) != -1 ){
            if ( folder ){
                while ( ( res = readdir ( folder ) ) ){
                    if ( strcmp( res->d_name, "." ) && strcmp( res->d_name, ".." ) ){
                        printf("%zu) - %s\n", count + 1, res->d_name);
                        count++;
                    }
                }

                closedir ( folder );
            }else{
                perror ( "Could not open the directory" );
                exit( EXIT_FAILURE);
            }
        }

    }else{
        printf("The %s it cannot be opened or is not a directory\n", path);
        exit( EXIT_FAILURE);
    }

    printf( "\n\tFound %zu Files\n", count );
}

输出:

1) - .gnome2
2) - .linuxmint
3) - .xsession-errors
4) - .nano
5) - .kde
6) - .xsession-errors.old
7) - .gnome2_private
8) - Public
9) - .gconf
10) - .bashrc
11) - .macromedia
12) - .thunderbird
13) - Pictures
14) - .profile
15) - .cinnamon
16) - .pki
17) - Compile
18) - Desktop
19) - .Private
20) - .cache
21) - .Xauthority
22) - .ICEauthority
23) - VirtualBox VMs
24) - .bash_history
25) - .mozilla
26) - .local
27) - .config
28) - .codeblocks
29) - Documents
30) - .bash_logout
31) - Videos
32) - Templates
33) - Downloads
34) - .adobe
35) - .gphoto
36) - Music
37) - .dbus
38) - .ecryptfs
39) - .sudo_as_admin_successful
40) - .gnome

    Found 40 Files

1
不是针对下投票者,但您为什么要回答一个已经被问了将近9年并且已有采纳答案的问题呢?您的回答是否显著更好?是否发生了一些变化导致原先采纳的答案无效? - Fureeish
4
因为Stack Overflow不仅仅专门针对某一个问题或答案。如果您可以提供更多与该问题相关的信息,那么它应该是一个Stack Overflow回答。这个回答在此处是为了扩展被接受的回答。 - Michi
2
@Fureeish 好的,我明白你的观点了。我添加了更多的代码(stat和access),使答案与已接受的答案不同。 - Michi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接