Sign in to follow this  
stick_man

Opening all Bitmap files in a single directory

Recommended Posts

First off, to make things easier, lets assume all the files are in the same directory as the executable. Does anyone know how to search for all Bitmap files of unspecified names in a directory?

Share this post


Link to post
Share on other sites
ok, Im being a little presumptuous here, but Im assuming C++, Windows, VS.NET

WIN32_FIND_DATA wfd;
HANDLE hf= FindFirstFile("*.bmp", &wfd);

if (hf != INVALID_HANDLE_VALUE)
{
do
{

// name of file stored in wfd.cFileName

} while (FindNextFile(hf, &wfd));

FindClose(hf);

}

Share this post


Link to post
Share on other sites
#ifdef WIN32
#include <windows.h>
#else
#include <dirent.h>
#endif

#include <iostream>
#include <vector>
#include <string>
using namespace std;

namespace file {

void getlist (std::vector<std::string>& list, std::string dir, std::string extension)
{
#ifdef WIN32
WIN32_FIND_DATA findData;
HANDLE fileHandle;
int flag = 1;
std::string search ("*.");
if (dir == "") dir = ".";
dir += "/";
search = dir + search + extension;
// SetCurrentDirectory(dir.c_str());
fileHandle = FindFirstFile(search.c_str(), &findData);
if (fileHandle == INVALID_HANDLE_VALUE) return;
while (flag)
{
list.push_back(findData.cFileName);
flag = FindNextFile(fileHandle, &findData);
}
FindClose(fileHandle);

#else

std::string search(".");
search += extension;
DIR* directory = opendir(dir.c_str());
dir += "/";
struct dirent* entry;
std::string temp;
std::string final_name;

while ((entry = readdir(directory)) != NULL)
{
temp = entry->d_name;
if (temp.find(search, 0) != string::npos)
{
final_name.clear();
final_name = dir + temp;
list.push_back(final_name);
}
}
closedir(directory);
#endif
}

} // end namespace file



Boder Games

Share this post


Link to post
Share on other sites
Actually I just "Googled" WIN32_FIND_DATA, reading the descriptions of the functions involved at MSDN I came across the fallowing...



FindNextFile

The FindNextFile function continues a file search from a previous call to the FindFirstFile or FindFirstFileEx function.

BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);

Parameters

hFindFile
[in] Search handle returned by a previous call to the FindFirstFile or FindFirstFileEx function.
lpFindFileData
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.

The structure can be used in subsequent calls to FindNextFile to see the found file or directory.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero (0). To get extended error information, call GetLastError.

If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this