背景:
目前debug过程中需要导入很多素材,如果代码中直接写死比较麻烦且耗时,所以比较好的办法就是材料放在某目录中,由程序自动检索所有符合后缀的文件,然后进行操作。
void Find(const char* lpPath, std::vector<const std::string> &fileList)
{
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind, lpPath);
strcat(szFind, "\\*.*");
HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind){
strcpy(szFind, lpPath);
strcat(szFind, "\\*.*");
hFind = ::FindFirstFile(szFind, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind)
return;
}
while (true)
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (FindFileData.cFileName[0] != '.')
{
char szFile[MAX_PATH];
strcpy(szFile, lpPath);
strcat(szFile, "\\");
strcat(szFile, (char*)(FindFileData.cFileName));
Find(szFile, fileList);
}
}
else
{
std::string path = lpPath;
path += "\\";
path += FindFileData.cFileName;
fileList.push_back(path);
}
//std::cout << FindFileData.cFileName<<::std::endl;
if (!FindNextFile(hFind, &FindFileData)) break;
}
FindClose(hFind);
}
int main()
{
std::cout << "put the input image path" << endl;
std::string inputPath;
std::getline(std::cin, inputPath);
std::vector<const std::string> fileList;
Find(inputPath.c_str(), fileList);
std::vector<const std::string>::iterator it;
for (it = fileList.begin(); it != fileList.end(); it++)
{
if (it->find("jpg") != -1 || it->find("png") != -1 || it->find("bmp") != -1 || it->find("JPEG") != -1 || it->find("JPG") != -1 ||
it->find("PNG") != -1 || it->find("BMP") != -1 || it->find("jpeg") != -1)
;//
else
{
continue;
}
std::string imgPath = *it;
//std::cout << imgPath << std::endl;
int pos = it->find_last_of("\\");
std::string imgname = it->substr(pos + 1);
std::string imgdir = it->substr(0, pos);
pos = imgname.find_last_of(".");
imgname = imgname.substr(0, pos);
printf( "[%s] [%s]\n", imgdir.c_str(), imgname.c_str());
printf("\n");
}
return 0;
}
put the input image path
F:\videos
[F:\videos]\[1.jpg][F:\videos]\[2.jpg]