[ create a new paste ] login | about

Link: http://codepad.org/SGj2ihfk    [ raw code | fork ]

C, pasted on Nov 2:
/*
 * @filename : EnumDirectory.c
 * @author   : edisonx / edison.shih
 * @compiler : gcc with minGW 4.6.1 / vc2012
 *
 */

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

// ------------------------------------------------------------

//#define NO_OUTPUT
#ifdef NO_OUTPUT
#define printf //
#define puts   //
#endif

// ------------------------------------------------------------
void DFS_Travel(const char * SearchPath, const char * Filter);
void BFS_Travel(const char * SearchPath, const char * Filter);
unsigned count ; // Total Folder and File Counter
// ------------------------------------------------------------
int main()
{
    clock_t begin;
    const char * Filter = "*.*" ;
    const char * SearchPath = "D:\\Programming" ;

    count = 0, begin=clock();
    DFS_Travel(SearchPath, Filter);
    printf("DFS_Travel : %ld\n", clock()-begin);
    printf("Count : %u\n", count);

    count = 0, begin=clock();
    BFS_Travel(SearchPath, Filter);
    printf("BFS_Travel : %ld\n", clock()-begin);
    printf("Count : %u\n", count);

	getchar();
    return EXIT_SUCCESS;
}

// ------------------------------------------------------------
void DFS_Travel(const char * SearchPath, const char * Filter)
{
    WIN32_FIND_DATA FileData;
    HANDLE hFind;
    char FullPath[MAX_PATH];

    sprintf(FullPath, "%s\\%s", SearchPath, Filter);
    hFind = FindFirstFile(FullPath, &FileData);

    if(hFind==INVALID_HANDLE_VALUE){
        //printf("FindFirstFile Error %d\n", GetLastError());
        return ;
    }
    do{

        if(!strcmp(FileData.cFileName, ".") ||
           !strcmp(FileData.cFileName, ".."))
            continue;
        ++count;
        sprintf(FullPath, "%s\\%s",SearchPath, FileData.cFileName);
        //printf("%s\n", FullPath);
        if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {// IsFolder
            DFS_Travel(FullPath, Filter);
        }
    }while(FindNextFile(hFind, &FileData)!=0);
    FindClose(hFind);
}

// ------------------------------------------------------------
typedef char FString[MAX_PATH];
void BFS_Travel(const char * SearchPath, const char * Filter)
{
    size_t QSize = 128, QCnt=0, Qi;
    WIN32_FIND_DATA FileData;
    HANDLE hFind;
    char FullPath[MAX_PATH];
    FString *Queue = (FString*)malloc(sizeof(*Queue) * QSize);

    sprintf(FullPath, "%s\\%s", SearchPath, Filter);
    hFind = FindFirstFile(FullPath, &FileData);

    if(hFind==INVALID_HANDLE_VALUE){
        printf("FindFirstFile Error %d\n", GetLastError());
        return ;
    }
    do{
        if(!strcmp(FileData.cFileName, ".") || !strcmp(FileData.cFileName, ".."))
            continue;
        ++count;
        sprintf(FullPath, "%s\\%s",SearchPath, FileData.cFileName);

        if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {// IsFolder
            if(QCnt==QSize) { // realloc
                QSize <<= 1;
                FString *tmp;
                tmp = (FString*)realloc(Queue, sizeof(*Queue)*QSize);
                if(tmp==NULL) { // realloc fail defect
                    puts("realloc fail");
                    free(Queue);
                    exit(EXIT_FAILURE);
                }
                Queue = tmp;
            }
            strcpy(Queue[QCnt], FullPath);
            ++QCnt;
        } else {
            //printf("%s\n", FullPath);
        }
    }while(FindNextFile(hFind, &FileData)!=0);
    // Scan Folder
    for (Qi = 0; Qi < QCnt; ++Qi) {
        //printf("%s\n", Queue[Qi]);
        BFS_Travel(Queue[Qi], Filter);
    }
    FindClose(hFind);
    free(Queue);
}


Create a new paste based on this one


Comments: