I am trying to recursively search for files in the computer using WinAPI: FindFirstFile
and FindNextFile
I do not understand why, but my code does not work.
on the first run, everything is great - I can see all of the content in the C drive.
when I go into the sub-folders, for example C:Program Files (x86)
, I am getting that all the files inside the folders are just .
, while if I'd use the function without recursively-search, with the given path of C:Program Files (x86)
, I would get a list of all the folders inside.
Here is my code :
#include <Windows.h>
#include <iostream>
#include <string>
void FindFile(std::string directory)
{
std::string tmp = directory + "\*";
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(tmp.c_str(), &file);
if (search_handle != INVALID_HANDLE_VALUE)
{
do
{
std::wcout << file.cFileName << std::endl;
directory += "" + std::string(file.cFileName);
FindFile(directory);
}while(FindNextFile(search_handle,&file));
if (GetLastError() != 18) // Error code 18: No more files left
CloseHandle(search_handle);
}
}
int main()
{
FindFile("C:");
}
The variable tmp
is used to store the wildchar *
, while directory is containing the next file to be searched (inside the loop).
What is my error?
Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…