Skip to main content
gamedev.net
🔒 Locked

Counting files

Started by Broccoli Apr 7, 2005 at 8:59 AM 4 replies 800+ views
Original Post
Broccoli
Broccoli
Here's the deal. For my RPG I have created an NPC class, which has has a function for reading in all needed info from a single file. So basically I have a folder with a bunch of text files, one for each NPC. Now, to make an array of NPCs I need to first count all of the files in this folder and make the array that big and then open them up one by one and read in the stuff. Reading the data is no problem, however, I don't want to to code in which files to open. Basically, all you should have to do to add a new NPC is a create a file in that folder and it should be loaded automatically. So how do I count all files in a single folder and then open them all up one by one? Hope you get my drift.
Nik02
Nik02
Assuming you use Windows: You could call FindFirstFile with "*" as the file name to create a file finder token. If it succeeds, add 1 to your own counter variable.
Then, call FindNextFile in a loop until it fails, everytime incrementing the counter.

When FindNextFile finally fails, you have the total count of all files that match the original find specification.
Niko Suni
Toolmaker
Toolmaker
If I were you, I'd stick to std::vector instead of using an array.

As for finding all the files:
#ifndef _WIN32_FILESEARCH_H_#define _WIN32_FILESEARCH_H_#include <windows.h>#include <string>using std::string;class CFileSearch{public:    CFileSearch():m_hHandle(INVALID_HANDLE_VALUE) {}    CFileSearch(const string& strSearch)    {        Search(strSearch);    }    // Search    // Does a search and stores the result. Will return false on error    bool Search(const string& strSearch)    {        m_bHasNext = true;        m_hHandle = FindFirstFile(strSearch.c_str(), &m_hSearchResult);        if (m_hHandle == INVALID_HANDLE_VALUE)            m_bHasNext = false;        return m_bHasNext;    }    // GetNext    // Places the next return value in strResult. Will return false when there is no    // new result    bool GetNext(string &Result)    {        if (!m_bHasNext)            return (false);        Result = m_hSearchResult.cFileName;        m_bHasNext = FindNextFile(m_hHandle, &m_hSearchResult) ? true : false;        return (true);    }private:    HANDLE m_hHandle;    WIN32_FIND_DATA m_hSearchResult;    bool  m_bHasNext;};#endif


Using it is easy, but it doesn't make a difference between directories and files.

Toolmaker
 
Nik02
Nik02
You could check for FILE_ATTRIBUTE_DIRECTORY flag in lpFileFindData.dwFileAttributes to determine if the hit is a directory or a file.

EDIT:
Quote:
If I were you, I'd stick to std::vector instead of using an array.

Seconded [wink]
Niko Suni
Broccoli
Broccoli
Thanks alot guys, I got it.
Toolmaker
Toolmaker
Quote:
Original post by Nik02
You could check for FILE_ATTRIBUTE_DIRECTORY flag in lpFileFindData.dwFileAttributes to determine if the hit is a directory or a file.

EDIT:
Quote:
If I were you, I'd stick to std::vector instead of using an array.

Seconded [wink]


When I wrote this code, I knew for sure that there are no sub-directories in the folder I'm searching. However, I need to change it a bit around so you can see the difference between directories, etc.

Toolmaker
 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.