post
poster: G-man
description: Recursive file search
language: C
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void search_dir(char * dir, char * input, file_list * list, HWND hwnd_in, bool update_notify){
    HANDLE search_handle;
    WIN32_FIND_DATA wfd;
    
    // First preform the search criteria in the dir given
    
    if(dir[strlen(dir)-1] != '\\') {
        strcat(dir, "\\");
    }
    
    SetCurrentDirectory(dir);

    bool is_done = 0;

    search_handle = FindFirstFile(input, &wfd);
    while(!is_done && search_handle != INVALID_HANDLE_VALUE){
        file_list_append(list, &wfd); // This is multi-thread safe
        if(update_notify){
            SendMessage(hwnd_in, UWM_SEARCH_UPDATE, 0, 0);
        }
        if(!FindNextFile(search_handle, &wfd) ){
            if(GetLastError() != ERROR_NO_MORE_FILES){
                error("from:FindNextFile");
            }
            is_done = 1;
        }
    }

    /*
    Now that we've searched the current directory, let iterate through all
    the sub dirs in this directory and recursively call this function on them.
    */

    char path[MAX_PATH+3]; // space for the \\*
    get_astrick_path(dir, path); // Path should now be dir\*
    
    // Disregard ./ (this directory)
    search_handle = FindFirstFile(path, &wfd);
    // Now disregard ../ (parent dir)
    FindNextFile(search_handle, &wfd);
    // Check the rest
    while(FindNextFile(search_handle, &wfd)){
        if(is_fdir(wfd)){ // Is this file a dir?
            strcpy(path, dir);
            strcat(path, wfd.cFileName);
            // Path should now be current_dir\this_dir
            search_dir(path, input, list, hwnd_in, update_notify);
        }
    }
}