Recursive File Search in C | Source Code


This source code below is written by se7en from LeetCoders.  It is capable of running through the enter C drive in 8 seconds on my computer finding more than 230,000 files.  Although the downside of it is that it costs quite an amount of CPU usage during its process.  You might try to optimize it by placing Sleep function or something that is possible in reducing the CPU usage.

#pragma comment(lib, "shlwapi.lib")
#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
int search(LPSTR);
int main()
{
    search("C:\\Program Files");
    return 0;
}
int search(LPSTR lpszPath)
{
    WIN32_FIND_DATA WFD;
    HANDLE hSearch;
    CHAR szFilePath[MAX_PATH + 1];
    PathCombine(szFilePath, lpszPath, "*.*");
    hSearch = FindFirstFile(szFilePath,&WFD);
    if(hSearch == INVALID_HANDLE_VALUE)
    {
        printf("Error Handle Value\n");
    }
    while (FindNextFile(hSearch,&WFD))
    {
        if(strcmp(WFD.cFileName,"..") && strcmp(WFD.cFileName,"."))
        {
                if(WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                PathCombine(szFilePath, lpszPath, WFD.cFileName);
                search(szFilePath);
                }
                else
                {
                PathCombine(szFilePath, lpszPath, WFD.cFileName);
                printf("%s\n",szFilePath);
                }
        }
    }
    FindClose(hSearch);
    return 0;
}

Source: http://www.leetcoders.org/showthread.php?tid=7261

Advertisements

One Response to “Recursive File Search in C | Source Code”

  1. darkcoders Says:

    well nice distributing, people will learn some from it.
    it is me se7en.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: