Thursday, June 19, 2008

Getting a list of files of a specific extension in a specified folder in Win32 API for c/c++

Often in a Win32/C/C++ application, we need a function to go through a specific folder and snatch all files matching a specified extension. While there is support for doing so in C# but there is no convenient function to do it in Win32/C/C++. Here I am sharing the function I use quite often in my day to day applications. It might help others also.

void GetFileList (std::vector& list, std::string dir, std::string extension)
WIN32_FIND_DATA findData;
HANDLE fileHandle;
int flag = 1;
std::string search ("*.");
if (dir.length() == 0) dir = ".";
dir += "/";
//search = dir + search + extension;
std::string temp = dir;
search = temp;
// SetCurrentDirectory(dir.c_str());
fileHandle = FindFirstFile(search.c_str(), &findData);

if (fileHandle == INVALID_HANDLE_VALUE) return;
while (flag)
flag = FindNextFile(fileHandle, &findData);


Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.