?
Solved

Count files in folder!!!

Posted on 1999-11-25
5
Medium Priority
?
392 Views
Last Modified: 2010-04-15
I need to know how many files there are in a specific folder, please give me a code example in C or C++.

Thanks!
0
Comment
Question by:slobstar
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2234594
#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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2234597
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
 
LVL 1

Author Comment

by:slobstar
ID: 2235395
Can you or someone else do this so that it works with Visual Studio under Win NT.


0
 
LVL 2

Accepted Solution

by:
lif081199 earned 800 total points
ID: 2235561
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
 
LVL 2

Expert Comment

by:lif081199
ID: 2235564
Don't forget

#include <io.h>
....
Lionel :)
0

Featured Post

The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question