tags:

views:

206

answers:

3

I'm trying to get FindFileFirst() windows API call to work and it's totally failing on every attempt. I've tried ., C:\., .txt, C:\.txt and yet it doesn't even iterate down directory names. Not sure what to try anymore. I'm getting ERROR_FILE_NOT_FOUND 2 (0x2) back when I call GetLastError(). Thanks for any help you can give.

HANDLE hFind;
LPWIN32_FIND_DATA FindFileData;

hFind = FindFirstFile("*.*", &FindFileData);

if(hFind == INVALID_HANDLE_VALUE)
{
    printf("\nFindFirstFile failed (%u)\n",GetLastError());
    return;
}

do
{
    if(FindFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
        if(FindFileData->cFileName[0] != '.')
            continue;
        else
            searchDir(makePath(path, FindFileData->cFileName));
    }

    printf("Found %s %s\n",
        FindFileData->dwFileAttributes,FindFileData->cFileName);
    FindClose(hFind);
}
while(FindNextFile(hFind, &FindFileData));

FindClose(hFind);
A: 

you should make sure to double slashes the path

i.e. "c:\\*.*"

it should work fine.

RageZ
+2  A: 

LPWIN32_FIND_DATA FindFileData;

You are using FindFileData pointer without allocating memory to it.

use this way.. ("remove LP")

WIN32_FIND_DATA FindFileData; // this will use stack memory

then refer to members like FindFileData.dwFileAttributes instead of FindFileData->dwFileAttributes

A: 

And now a surprise from FindFirstFile Function.

As stated previously, you cannot use a trailing backslash () in the lpFileName input string for FindFirstFile, therefore it may not be obvious how to search root directories. If you want to see files or get the attributes of a root directory, the following options would apply:

  • To examine files in a root directory, you can use "C:*" and step through the directory by using FindNextFile.

  • To get the attributes of a root directory, use the GetFileAttributes function.

For example you cannot use "C:\\*.txt" to search for the text files in the root directory
but in subdirectories it'll work.

Nick D