Latest News

Recursive File Search in C | Source Code

Saturday, 5 March 2011 , Posted by genesisdatabase at 17:52

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.

[code]
#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;
}
[/code]

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

Currently have 2 comments:

  1. darkcoders says:

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

  1. about us says:

    i want to get one

Leave a Reply

Post a Comment