Jump to content
  • Advertisement
Sign in to follow this  
stick_man

Opening all Bitmap files in a single directory

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Both of those samples are incorrect. FindNextFile() does not return 0 when there are no more files - it returns ERROR_NO_MORE_FILES.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!