Jump to content
  • Advertisement
Sign in to follow this  
ronnybrendel

enumerating files and directories?

This topic is 4419 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

i am currently searching a way to enumerate alle files and directories of a directory ... most likely with c-std-runtime-functions ... plz help :( €: by saying std-c i meant that should be systemindependant ... but as i can see, that seems not to work out ...

Share this post


Link to post
Share on other sites
Advertisement
Here ya go:



void Blah::_getAllNamesLike( const String& strStartPath, const String& strPattern, std::vector<String>& sv, bool bRecursive, bool caseSensitive )

long lHandle, res;
struct _finddata_t tagData;

lHandle = (long)_findfirst("*.*", &tagData);
res = 0;
while (lHandle != -1 && res != -1)
{
if(!(tagData.attrib & _A_SUBDIR))
{
String s = strStartPath + tagData.name;
if (s.match (strPattern, caseSensitive))
{
sv.push_back(s);
}
}
res = _findnext( lHandle, &tagData );
}
// Close if we found any files
if(lHandle != -1)
{
_findclose(lHandle);
}

// Now find directories
if (bRecursive)
{
lHandle = (long)_findfirst("*", &tagData);
res = 0;
while (lHandle != -1 && res != -1)
{
if((tagData.attrib & _A_SUBDIR) && strcmp(tagData.name, ".") && strcmp(tagData.name, ".."))
{
// recurse
String dir = strStartPath + tagData.name + "/";
pushDirectory(tagData.name);
_getAllNamesLike (dir, strPattern, sv, bRecursive, caseSensitive);
popDirectory();
}
res = _findnext( lHandle, &tagData );
}
// Close if we found any files
if(lHandle != -1)
{
_findclose(lHandle);
}
}



Share this post


Link to post
Share on other sites
There is no way to do this using only standard C++; it is system dependant. There is a library called boost::filesystem which does this in a cross-platform way.

Here is a windows-only solution that I wrote a while ago:
#include <windows.h>

std::vector< std::string > GetFiles( std::string directory )
{
std::vector< std::string > files;

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;

std::string dir = directory + "\\*";

hFind = FindFirstFile( dir.c_str(), &FindFileData );

if( hFind != INVALID_HANDLE_VALUE )
{
files.push_back( FindFileData.cFileName );

while( FindNextFile( hFind, &FindFileData ) )
{
files.push_back( FindFileData.cFileName );
}
}

return files;
}



I have not tested it too much, but it works for my needs. Also note that it returns the '.' and '..' directories in the vector.

Hope this helps in some way.

Share this post


Link to post
Share on other sites
If you just want directory/file iteration, try boost::filesystem

Otherwise, PhysFS might be worth looking into if this is for resource handling in a game.

Share this post


Link to post
Share on other sites
Files and directories (at least directories) are only a concept that's introduced by OS's. Any new OS can deal with storage management in anyway it likes (the designers like). That's why you can't link a language to a platform-specific concept, though it is widely generalize.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!