Jump to content

Getting files under a directory


smashthewindow
 Share

Recommended Posts

Hi,

 

This isn't directly related to Leadwerks but I guess someone can help me out here.

I'm looking for a function that returns files under a directory on Windows. (It doesn't have to be cross-platform.)

 

So for example, an ideal example would be something like:

 

vector<string> get_files_in_dir( string directory );

 

I'm fine with Win32 API functions but I'm trying to avoid using boost.

Can anyone help me out here?

Blog & Portfolio

 

Current project: moon.chase.star

Link to comment
Share on other sites

typedef std::vector<std::string> StringArray;

StringArray ListFiles( const std::string folder, const std::string& pattern )
{
   std::string root = folder;
   if( root[root.length()-1] != '\\' &&
       root[root.length()-1] != '/' )
   root += '/';
   root += pattern;
   StringArray v;

   WIN32_FIND_DATA fd;
   HANDLE h = FindFirstFile( root.c_str(), &fd);
   if( h == INVALID_HANDLE_VALUE )
       return v;
   if( fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
       v.push_back(fd.cFileName);

   while ( FindNextFile(h, &fd) )
       if( fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
           v.push_back(fd.cFileName);
   FindClose( h );
   return v;
}

StringArray ListSubfolders( const std::string folder )
{
   StringArray v;

   std::string root = folder;
   if( root[root.length()-1] != '\\' &&
   root[root.length()-1] != '/' )
   root += '/';
   root += "*";

   WIN32_FIND_DATA fd;
   HANDLE h = FindFirstFile( root.c_str(), &fd);
   if( h == INVALID_HANDLE_VALUE )
       return v;
   if( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
       strcmp( fd.cFileName, "." ) &&
       strcmp( fd.cFileName, ".." ) )
   {
       v.push_back(fd.cFileName);
   }
   while ( FindNextFile(h, &fd) )
   {
       if( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
           strcmp( fd.cFileName, "." ) &&
           strcmp( fd.cFileName, ".." ) )
       {
             v.push_back(fd.cFileName);
       }
   }
   FindClose( h );
   return v;
}

  • Upvote 1

AV MX Linux

Link to comment
Share on other sites

Or there is this taken from a little util I wrote:

 

void loadOutputFileDirectoryFileNames(void)
{
DIR *dp;
struct dirent *dirp;
fileListData* pFileListDataItem;
std::string fullFileName;
std::string fileName;
std::string fileExt;
if((dp = opendir(appData.currentOutputDirectory.c_str())) == NULL) {
 MessageBoxA(0,"An error occured trying to open the output directory","ERROR",0); // errno
 return;
}
clearDirectoryFileList();

while ((dirp = readdir(dp)) != NULL) {

fullFileName = dirp->d_name;
// Check the file extension
if(getFileNameComponents(fullFileName, fileName, fileExt))
{
for(int i = 0; i < vecExtList.size(); i++)
{
if(fileExt == vecExtList[i])
{
 pFileListDataItem = new fileListData;
 pFileListDataItem->currentFileName = fileName;
 pFileListDataItem->currentFileExt = fileExt;
 pFileListDataItem->updated= false;
 vecFileList.push_back(pFileListDataItem);
 break;
}
}
}
}
closedir(dp);
return;
}

 

 

requires dirent.h form the C POSIX library

 

It's doing more than you require but its another alternative.

 

Sorry for the poor tabs ... this container always seens to screw them up. How come your's is perfect Roland wink.png

  • Upvote 1

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...