• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 577
  • Last Modified:

recursive file list in linux

Hi,

I made a simple recursive function to count the number of files in a directory under win32.  I want to make an equivalent under linux - where is the linux system API documentation (online)? Like win32 stuff you can find at msdn.com. I guess I need the equivalents for:

    WIN32_FIND_DATA;
    HANDLE;
    Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
    FindNextFile(hFindFile, &Win32FindData);

Thanks
0
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
2 Solutions
 
itsmeandnobodyelseCommented:
you may use the items defined in dirent.h

Regards. Alex
0
 
itsmeandnobodyelseCommented:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

   char *szpath = "path/dir";
   struct stat fs;
   if (stat(szpath, &fs) == 0)  
   {
         if( fs.st_mode & S_ISDIR))
         {
              DIR *dir = opendir(szpath);
              ...
   }

0
 
itsmeandnobodyelseCommented:
       
       struct dirent* drp;
       while((drp = readdir(dir)) != NULL)
        {
          printf("%s\n", drp->d_name);
            if (stat(drp->d_name, &fs) == 0)
            {
                   if( fs.st_mode & S_ISDIR)) // next directory
                        // call recursive
                   else
                        // collect file
            }
        }
        closedir(dir);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DJ_AM_JuiceboxAuthor Commented:
@itsmeandnobodyelse:

Sorry I'm a bit confused - which code snippet should I use?

Thanks
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi DJ_AM_Juicebox,

Some of both.  :)

Note that nothing in the standard requires that readdir() is reentrant.  You call opendir() much as you would call fopen().  Then you repeatedly call readdir() just like you would normally call a fread() or fgetxxx() function.

Be careful that if you encounter a directory name part way through the process that you handle it correctly.  One of the traps is opening the child directory and losing your location in the parent directory.  Also note that the calls are asyncronous to everything else.  The directory contents can change while you're reading through them, though that shouldn't affect you.


Good Luck,
Kent
0
 
jkrCommented:
Or in complete:

#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <iostream>
using namespace std;

void list_all_files ( const string& sStartDir, list<string>& lstFound) {

   cout << "checking " << sStartDir.c_str () << endl;

   DIR* pDir = opendir ( sStartDir.c_str ());

   if ( !pDir) return false;

   dirent* pEntry;

   while ( pEntry = readdir ( pDir)) {

       cout << "found " << pEntry->d_name << endl;

       if ( DT_DIR & pEntry->d_type && strcmp ( pEntry->d_name, ".") && strcmp ( pEntry->d_name, "..")) {

           string sSubDir = sStartDir + string ( "/") + string ( pEntry->d_name);

           list_all_files ( sSubDir, sFound));

       }

       string sFound = sStartDir + string ( "/") + string ( pEntry->d_name);

       lstFound.push_back ( sFound);
   }
}

and use it like

list<string> lstFiles;

list_all_files ( "/home/somedir", lstFiles);
0
 
jkrCommented:
Correction, the complete compilable code is:

#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <list>
#include <iostream>
using namespace std;

void list_all_files (   const string& sStartDir,
                        list<string>& lstFound) {

   cout << "checking " << sStartDir.c_str () << endl;

   DIR* pDir = opendir ( sStartDir.c_str ());

   if ( !pDir) return;

   dirent* pEntry;

   while ( pEntry = readdir ( pDir)) {

       cout << "found " << pEntry->d_name << endl;

       if ( DT_DIR & pEntry->d_type && strcmp ( pEntry->d_name, ".") && strcmp ( pEntry->d_name, "..")) {

           string sSubDir = sStartDir + string ( "/") + string ( pEntry->d_name);

           list_all_files ( sSubDir, lstFound);

       }

       string sFound = sStartDir + string ( "/") + string ( pEntry->d_name);

       lstFound.push_back ( sFound);
   }
}

int main () {

list<string> lstFiles;

list_all_files ( "/home/somedir", lstFiles);

return 0;
}
0
 
itsmeandnobodyelseCommented:
>>>> Sorry I'm a bit confused - which code snippet should I use?
>>>> Some of both.  :)
Kent was right. You really would need both and make a function boy around so that you could call it recrsively. Sorry for the snippets. The first snippet shows how to use opendir together with the stat function. The second shows how to retrieve subfolders and files from the opened directory. Folders and files will be separated by using the stat function again.

Regards, Alex
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now