Reputation: 19
i'm trying to find all files on all drives, the OS is Windows 8
std::string FolderName = "C:\\";
FindAllFiles(FolderName);
...
void FindAllFiles(std::string FolderName)
{
WIN32_FIND_DATA FileData;
HANDLE FirstFile = FindFirstFile(&FolderName[0], &FileData);
if (FirstFile == INVALID_HANDLE_VALUE) {
std::cout << "Invalid handle value" << std::endl;
return;
}
while (FindNextFile(FirstFile, &FileData))
{
if (isalpha(FileData.cFileName[0]))
{
if (FileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
std::cout << FileData.cFileName << std::endl;
}
else
{
std::string NewPath = FolderName + FileData.cFileName;
NewPath = NewPath + "\\*.*";
FindAllFiles(NewPath);
}
}
}
}
but always receive Invalid_handle_value error. What is the problem?
Upvotes: 0
Views: 81
Reputation: 283634
You didn't call GetLastError()
, which you should always do when a Windows function fails, but the most likely cause is that you failed to fill in FileData
correctly before calling FindFirstFile
.
Nearly all output buffers for use by Windows functions must be prepared:
dwSize
member using sizeof
(this structure doesn't have this)So try initializing the buffer with:
WIN32_FIND_DATAA FileData = {};
Upvotes: 1