Ladies and Gents,

I am trying to write a function in my ever growing program to give me information on a selected folder. I actually only need the number of files in the folder so I can use that number in a loop. Is there a way to get the number of files in INT form for this purpose ? Thanks in advance!!


Muthu

Recommended Answers

All 8 Replies

What operating system are you using?

I am using WIndows XP, but will have to write a similar program for linux later.

What operating system are you using?

use FindFirstFile() and FindNextFile() to loop through all the files in a directory. Search MSDN and you will find some examples.

I searched MSDN for examples and I got the one here but I don't know where to place the path of my folder. Can you give me a simpley example than this beast:

int main(int argc, char *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   char DirSpec[MAX_PATH + 1];  // directory specification
   DWORD dwError;

   printf ("Target directory is %s.\n", argv[1]);
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   strncat (DirSpec, "\\*", 3);

   hFind = FindFirstFile(DirSpec, &FindFileData);

   if (hFind == INVALID_HANDLE_VALUE) 
   {
      printf ("Invalid file handle. Error is %u\n", GetLastError());
      return (-1);
   } 
   else 
   {
      printf ("First file name is %s\n", FindFileData.cFileName);
      while (FindNextFile(hFind, &FindFileData) != 0) 
      {
         printf ("Next file name is %s\n", FindFileData.cFileName);
      }
    
      dwError = GetLastError();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES) 
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
      }
   }
   return (0);
}

Here is simple code to get all the file names in a directory. It does not parse sub-directories.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH]; // directory specification

strcpy (DirSpec, argv[1]);
strcat (DirSpec, "\\*");
HANDLE  hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
   do {
      // If this filename is NOT a directory
     if(!FindFileData.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY)
    {
          // do something with the filename

    }
  } while( !FindNextFile(hFile,&FindFileData);
  FindClose(hFile);

Thanks. I'll give it a try.

Here is simple code to get all the file names in a directory. It does not parse sub-directories.

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH]; // directory specification

strcpy (DirSpec, argv[1]);
strcat (DirSpec, "\\*");
HANDLE  hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
   do {
      // If this filename is NOT a directory
     if(!FindFileData.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY)
    {
          // do something with the filename

    }
  } while( !FindNextFile(hFile,&FindFileData);
  FindClose(hFile);

I found a little error in earlier replies.
The line:

if(!FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

is wrong.

It should be:

if( ! (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )


That's all.

If you have the boost library installed, you might consider using boost::filesystem instead (which would be more portable):

#include <boost/filesystem/operations.hpp>
#include <iostream>
using namespace boost::filesystem;
int main()
{
path mypath("/boost_1_33_1");
if (!exists(mypath))
{
  std::cerr<<"Path does not exist!";
 return EXIT_FAILURE;
}
directory_iterator dirIt(mypath);
int filecount=0;
for (directory_iterator dirIt(mypath);dirIt!=directory_iterator();dirIt++)
{
if (!is_directory(*dirIt))
  {
    std::cout<<dirIt->leaf()<<std::endl;
    filecount++;
  }
}
std::cout<<"Number of files in directory: "<<filecount;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.