Suppose i have a folder, In this folder exist some files and folders, I want to access to folders not to files. How do i do that. I know to exist this function in c++ : FindFirstFileA
and i write following code, but this work for files.
WIN32_FIND_DATAA ffd;
string s = "E:\\OpenCV\\SABT\\Old";
HANDLE hFind = FindFirstFileA(s.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
printf("no file found");
return -1;
}
if (ffd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
do
{
std::string fn = path + ffd.cFileName;
printf("file %s\n", fn.c_str());
} while (FindNextFileA(hFind, &ffd) != 0);
}
You are checking ffd.dwFileAttributes
the wrong way. It is a bitmask, and a directory can have multiple attributes at a time, so you need to use the &
bitwise AND operator instead of the ==
equality operator.
Don't forget to also check/ignore the "."
and ".."
entries when enumerating subfolders. And call FindClose()
when you are done enumerating.
Try something more like this instead:
WIN32_FIND_DATAA ffd;
string path = "E:\\OpenCV\\SABT\\Old\\";
HANDLE hFind = FindFirstFileA((path + "*.*").c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
printf("no folders found\n");
else
printf("error searching for folders\n");
return -1;
}
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((lstrcmpA(ffd.cFileName, ".") != 0) &&
(lstrcmpA(ffd.cFileName, "..") != 0))
{
std::string fn = path + ffd.cFileName;
printf("%s\n", fn.c_str());
}
}
}
while (FindNextFileA(hFind, &ffd));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
printf("error searching for folders\n");
}
FindClose(hFind);
You want this:
WIN32_FIND_DATAA ffd;
string s = "E:\\OpenCV\\SABT\\Old";
HANDLE hFind = FindFirstFileA(s.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
printf("no file found");
return -1;
}
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::string fn = path + ffd.cFileName;
printf("file %s\n", fn.c_str());
}
} while (FindNextFileA(hFind, &ffd) != 0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.