find the number of files in a directory? -

I have the path of a directory in a CString variable called sNewFolder.
I need to find the total number of files in this directory.
and just messagebox it out.

I think i have to use CFileFind. Any suggetions.
Who is Participating?
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
This should do it
      CFileFind ff;
      CString s(sNewFolder);
      s += "*.*";   //may need \\*.*   depends on contents of sNewFolder
      int i = 0;
      bool bWorking = ff.FindFile(s, 0);
            if(!ff.IsDots() && !ff.IsDirectory())      
            bWorking = ff.FindNextFile();
      s.Format("Found %d files", i);
Kiran Paul VJComputer EngineerCommented:

try this
      CFileFind objFile;
      int counter = 0 ;
      BOOL bl;
      CString szCounter;
      CString filename;

      bl = objFile.FindFile(NULL,0);
            bl = objFile.FindNextFile();
                  filename = objFile.GetFileName();
                  counter = counter + 1;
      szCounter.Format("Total %d files ",counter);


hope this helps
thank u
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.