GetLastError returns ERROR_FILE_NOT_FOUND, although the folder contains files that fall under the mask. What is the problem? I would be grateful for the help!

#include <windows.h> #include <stdio.h> LPWSTR fileName = L"*.exe"; LPWIN32_FIND_DATAW data32; HANDLE fileHandle; int main(){ fileHandle = FindFirstFileW(fileName, data32); printf("%08x\n", GetLastError()); if(fileHandle != 0){ while(FindNextFileW(fileHandle, data32)){ MessageBoxW(0, data32->cFileName, L"File Found!", 0); } } FindClose(fileHandle); system("pause"); return 0; } 
  • In which folder? - Enikeyschik
  • C: \ Users \ Admin \ Documents \ Visual Studio 2008 \ Projects \ 1 \ Debug \ - dagger d1ck 2:58 pm
  • And looking for a folder? - Enikeyschik
  • In the same. Even if you set C: \ Users \ Admin \ Documents \ Visual Studio 2008 \ Projects \ 1 \ Debug * .exe in the fileName, there will be no result. - dagger d1ck
  • There will be a result, there will definitely be. Just need to write without errors. For example, not Debug*.exe , but Debug\*.exe . And in general, the path should always be indicated. - freim 4:05 pm

2 answers 2

You have the following errors:

1) Use a pointer to the WIN32_FIND_DATAW structure, but you do not create it. Therefore, garbage is passed to FindFirstFileW .

2) Looking for *.exe where there are none. The program runs in the project directory, not in the Debug directory. It is clear that searching for executable files will not return anything. The path must always be set explicitly.

3) Well, small things like assigning LPWSTR fileName = L"*.exe"; (address of a constant to a non-constant pointer).

Here is the corrected version (looking for *.* To make it clearer):

 int main() { LPCWSTR fileName = L"*.*"; WIN32_FIND_DATAW data32; HANDLE fileHandle; fileHandle = FindFirstFileW(fileName, &data32); if(fileHandle != NULL) { while(FindNextFileW(fileHandle, &data32)){ MessageBoxW(0, data32.cFileName, L"File Found!", 0); } FindClose(fileHandle); } else printf("%08x\n", GetLastError()); system("pause"); return 0; } 

    Not to say that all the problems in this, but ...

    We read in the description:

     BOOL FindNextFileW( HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData ); 

    lpFindFileData
    A pointer to the WIN32_FIND_DATA structure.

    At least - where is your structure?

    Prepare it and submit the address:

     WIN32_FIND_DATA ffd; ... FindFirstFileW(fileName, &ffd); 

    Also, if the file is unique, it will be found by FindFirstFile (and information about it is not output), and FindNextFile will be nothing to find FindNextFile .

    Also - why do you need MessageBox console application?

    Again - if this is C ++, then why not use the <filesystem> ?