int CmFile::GetNames(CStr &nameW, vecS &names) { string dir = GetFolder(nameW); names.clear(); names.reserve(6000); DIR *dp = opendir(_S(dir)); if (dp == NULL){ cout << dir << endl; perror("Cannot open directory"); return EXIT_FAILURE; } struct dirent *dirContent; while ((dirContent = readdir(dp)) != NULL){ if (string(dirContent->d_name)[0] == '.') continue; struct stat st; lstat(dirContent->d_name,&st); if(S_ISREG(st.st_mode)){ cout << string(dirContent->d_name) << " " << st.st_mode << endl; names.push_back(string(dirContent->d_name)); } } closedir(dp); return (int)names.size(); }
int CmFile::GetSubFolders(CStr& folder, vecS& subFolders) { subFolders.clear(); DIR *dp = opendir(_S(folder)); if (dp == NULL){ cout << folder << endl; perror("Cannot open folder"); return EXIT_FAILURE; } struct dirent *dirContent; while ((dirContent = readdir(dp)) != NULL){ if (string(dirContent->d_name)[0] == '.') continue; struct stat st; lstat(dirContent->d_name,&st); if(S_ISDIR(st.st_mode)){ subFolders.push_back(string(dirContent->d_name)); } } closedir(dp); return (int)subFolders.size(); }
int CmFile::GetNames(CStr& rootFolder, CStr &fileW, vecS &names) { GetNames(rootFolder + fileW, names); vecS subFolders, tmpNames; int subNum = CmFile::GetSubFolders(rootFolder, subFolders); for (int i = 0; i < subNum; i++){ subFolders[i] += "/"; int subNum = GetNames(rootFolder + subFolders[i], fileW, tmpNames); for (int j = 0; j < subNum; j++) names.push_back(subFolders[i] + tmpNames[j]); } return (int)names.size(); }
int CmFile::GetSubFolders(CStr& folder, vecS& subFolders) { subFolders.clear(); WIN32_FIND_DATAA fileFindData; string nameWC = folder + "\\*"; HANDLE hFind = ::FindFirstFileA(nameWC.c_str(), &fileFindData); if (hFind == INVALID_HANDLE_VALUE) return 0; do { if (fileFindData.cFileName[0] == '.') continue; // filter the '..' and '.' in the path if (fileFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) subFolders.push_back(fileFindData.cFileName); } while (::FindNextFileA(hFind, &fileFindData)); FindClose(hFind); return (int)subFolders.size(); }
// Get image names from a wildcard. Eg: GetNames("D:\\*.jpg", imgNames); int CmFile::GetNames(CStr &nameW, vecS &names, string &dir) { dir = GetFolder(nameW); names.clear(); names.reserve(10000); WIN32_FIND_DATAA fileFindData; HANDLE hFind = ::FindFirstFileA(_S(nameW), &fileFindData); if (hFind == INVALID_HANDLE_VALUE) return 0; do{ if (fileFindData.cFileName[0] == '.') continue; // filter the '..' and '.' in the path if (fileFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; // Ignore sub-folders names.push_back(fileFindData.cFileName); } while (::FindNextFileA(hFind, &fileFindData)); FindClose(hFind); return (int)names.size(); }