• C

Count files in folder!!!

I need to know how many files there are in a specific folder, please give me a code example in C or C++.

Thanks!
LVL 1
slobstarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KangaRooCommented:
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int
main (void)
{
  DIR *dp;
  struct dirent *ep;
  int count = 0;

  dp = opendir ("./");
  if (dp != NULL)
    {
      while (ep = readdir (dp)) ++count;
        //puts (ep->d_name);
       
      (void) closedir (dp);
    }
  else
    puts ("Couldn't open the directory.");
  printf("%d files\n", count);
  return 0;
}
0
KangaRooCommented:
Note: There is no ANSI C way of handling directories. Example above uses POSIX functions which as standard as it will get.
Tested with Mingw32 (gcc 2.8.1) on Windows 95
0
slobstarAuthor Commented:
Can you or someone else do this so that it works with Visual Studio under Win NT.


0
lif081199Commented:
int CountDirectory(char *sDir)
{
        int NbFiles = 0;
        if (!sDir)      return NbFiles;

        char Dir[512];
        strcpy(Dir,sDir);
        if (Dir[strlen(Dir)-1] != '\\') strcat(Dir,"\\"); // convert "c:\mydir" to "c:\mydir\"
        strcat(Dir,"*.*"); // convert "c:\mydir\" to "c:\mydir\*.*"

        struct _finddata_t fileinfo;
        long handle;
        int rc;

        handle = _findfirst( Dir, &fileinfo );
        rc = handle;
        while( rc != -1 )
        {
                // directory ?
                if ((fileinfo.attrib & _A_SUBDIR)==_A_SUBDIR)
                {
                                    // Don't count the dir
                }
                else
                {
                        // Do what you want with the file
                                    NbFiles ++;
                }

                rc = _findnext( handle, &fileinfo );
        }
        _findclose( handle );
            return NbFiles;
}

Good luck,
Lionel.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lif081199Commented:
Don't forget

#include <io.h>
....
Lionel :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.