Find Directory Files Easily and Execute a Function for Each of Them


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

The CScanPath is designed for all developers who want to implement a scanning function for directory/subdirectory files and interact with each of them.

How It Works

When starting the scanning job through the CScanPath class, a separate thread search is created for all files in the directory tree structure. It executes the specified function for each of them. The class also gives you the possibility to get the total directory files' numbers, and the total directory files' sizes.

How to Use It

If you want to know the total directory files sizes or the total directory files numbers, you can use the following method:

  • DWORD GetDirFilesNumbers( char * path )
  • double GetDirFilesSizes( char * path )

where path is the directory to search for.

Now, to scan a directory and manipulate each file found, proceed like this:

  1. Declare a static function.
  2. static void MyFunction( char * file );
  3. Make the function to manipulate files found.
  4. void MyFunction( char * file )
       printf("-> %s\n", file);
  5. Call the RunScan method.
  6. CScanPath ScanPath;
    ScanPath.RunScan( "c:\\windows", MyFunction );

    If you want to stop scanning for any reason, just do this:


That's ALL!

Sample Code for Console Program

#include <windows.h>
#include "<stdio.h>"
#include "ScanPath.h"

void MyFunction( char * file )
   printf("-> %s\n", file );

void main( int argc, char **argv )

   if( argc < 2 )
      printf("Usage : %s <path to scan>\n", argv[0] );

   if( !SetCurrentDirectory( argv[1] ) )
      printf("Error : %s in not a valid directory !\n", argv[1]);

   CScanPath Scan;

   // the third argument means that the function doesn't return
   // until the scan ends
   // this is FALSE by default.
   Scan.RunScan( argv[1], MyFunction, TRUE );

About the Author

Aurelien BOUDOUX

Visit my web site on http://www.techandfeel.com !



  • Why my Console Program can not be compiling?

    Posted by caixing_200 on 01/13/2008 06:07am

    I am a beginner of C++. I want to use CScanpath class. I use it in a C++ Console Program according to the author's method as possible as I can. But the program can not be compiling. When I compile the program, It produces the error information error C2065b: undeclared identifier .What should I do? Please explain it step by step and offer the source code. Thank you! (My source code with some wrong is in my yahoo Email.username:c.question,password:jg123456) Development environment: Microsoft Windows 2000 Service pack 4 and Microsoft Visual C++.net 2003.

  • How to handle quit while processing?

    Posted by Large on 06/20/2006 07:13pm

    Hi the class look great!
    I wonder how you would handle if the program was closed while thread is running. Is this the best way?
    	WaitForSingleObject( hThread, INFINITE );
    Please give me suggestions if you have any :)

    • Source code updated to 1.1

      Posted by aurelien on 06/21/2006 08:52pm

      Hi Lars, and thanks for your comment. I have make a little update to the Class for properly kill the working thread when object is deleted, and prevent multiple RunScan method launch. I hope it will be online soon. Thank you very much for your remark, and sorry for my english ;)

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date