在Windows上搜索C中的文件

时间:2022-08-02 22:09:41

How would one search for files on a computer? Maybe looking for certain extensions.

如何在计算机上搜索文件?也许正在寻找某些扩展。

I need to iterate through all the files and examine file names.

我需要遍历所有文件并检查文件名。

Say I wanted to find all files with an .code extension.

说我想找到扩展名为.code的所有文件。

4 个解决方案

#1


For Windows, you would want to look into the FindFirstFile() and FindNextFile() functions. If you want to implement a recursive search, you can use GetFileAttributes() to check for FILE_ATTRIBUTE_DIRECTORY. If the file is actually a directory, continue into it with your search.

对于Windows,您需要查看FindFirstFile()和FindNextFile()函数。如果要实现递归搜索,可以使用GetFileAttributes()来检查FILE_ATTRIBUTE_DIRECTORY。如果文件实际上是一个目录,请继续搜索。

#2


A nice wrapper for FindFirstFile is dirent.h for windows (google dirent.h Toni Ronkko)

FindFirstFile的一个很好的包装是dirent.h for windows(google dirent.h Toni Ronkko)


#define S_ISREG(B) ((B)&_S_IFREG)
#define S_ISDIR(B) ((B)&_S_IFDIR)

static void
scan_dir(DirScan *d, const char *adir, BOOL recurse_dir)
{
    DIR *dirfile;
    int adir_len = strlen(adir);

    if ((dirfile = opendir(adir)) != NULL) {
        struct dirent *entry;
        char    path[MAX_PATH + 1];
        char    *file;

        while ((entry = readdir(dirfile)) != NULL)
        { 
            struct stat buf;
            if(!strcmp(".",entry->d_name) || !strcmp("..",entry->d_name))
                continue;

            sprintf(path,"%s/%.*s", adir, MAX_PATH-2-adir_len, entry->d_name);

            if (stat(path,&buf) != 0)
                continue;

            file = entry->d_name;
            if (recurse_dir && S_ISDIR(buf.st_mode) )
                scan_dir(d, path, recurse_dir);
            else if (match_extension(path) && _access(path, R_OK) == 0) // e.g. match .code
                strs_find_add_str(&d->files,&d->n_files,_strdup(path));
        }
        closedir(dirfile);
    }
    return;
}

#3


Use FindFirstFile() or FindNextFile() functions and a recursive algorithm to traverse sub-folders.

使用FindFirstFile()或FindNextFile()函数和递归算法遍历子文件夹。

#4


FindFirstFile()/ FindNextFile() will do the job in finding the list of files in the directory. To do recursive search through the sub-directories you might use _splitpath

FindFirstFile()/ FindNextFile()将完成查找目录中文件列表的工作。要通过子目录进行递归搜索,可以使用_splitpath

to split the path, into directory and filenames, and then use the resulting directory detail to do a recursive directory search.

将路径拆分为目录和文件名,然后使用生成的目录详细信息进行递归目录搜索。

#1


For Windows, you would want to look into the FindFirstFile() and FindNextFile() functions. If you want to implement a recursive search, you can use GetFileAttributes() to check for FILE_ATTRIBUTE_DIRECTORY. If the file is actually a directory, continue into it with your search.

对于Windows,您需要查看FindFirstFile()和FindNextFile()函数。如果要实现递归搜索,可以使用GetFileAttributes()来检查FILE_ATTRIBUTE_DIRECTORY。如果文件实际上是一个目录,请继续搜索。

#2


A nice wrapper for FindFirstFile is dirent.h for windows (google dirent.h Toni Ronkko)

FindFirstFile的一个很好的包装是dirent.h for windows(google dirent.h Toni Ronkko)


#define S_ISREG(B) ((B)&_S_IFREG)
#define S_ISDIR(B) ((B)&_S_IFDIR)

static void
scan_dir(DirScan *d, const char *adir, BOOL recurse_dir)
{
    DIR *dirfile;
    int adir_len = strlen(adir);

    if ((dirfile = opendir(adir)) != NULL) {
        struct dirent *entry;
        char    path[MAX_PATH + 1];
        char    *file;

        while ((entry = readdir(dirfile)) != NULL)
        { 
            struct stat buf;
            if(!strcmp(".",entry->d_name) || !strcmp("..",entry->d_name))
                continue;

            sprintf(path,"%s/%.*s", adir, MAX_PATH-2-adir_len, entry->d_name);

            if (stat(path,&buf) != 0)
                continue;

            file = entry->d_name;
            if (recurse_dir && S_ISDIR(buf.st_mode) )
                scan_dir(d, path, recurse_dir);
            else if (match_extension(path) && _access(path, R_OK) == 0) // e.g. match .code
                strs_find_add_str(&d->files,&d->n_files,_strdup(path));
        }
        closedir(dirfile);
    }
    return;
}

#3


Use FindFirstFile() or FindNextFile() functions and a recursive algorithm to traverse sub-folders.

使用FindFirstFile()或FindNextFile()函数和递归算法遍历子文件夹。

#4


FindFirstFile()/ FindNextFile() will do the job in finding the list of files in the directory. To do recursive search through the sub-directories you might use _splitpath

FindFirstFile()/ FindNextFile()将完成查找目录中文件列表的工作。要通过子目录进行递归搜索,可以使用_splitpath

to split the path, into directory and filenames, and then use the resulting directory detail to do a recursive directory search.

将路径拆分为目录和文件名,然后使用生成的目录详细信息进行递归目录搜索。