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

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

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 20, 2006
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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:

    ScanPath.StopScan();

That’s ALL!

Advertisement

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] );
      ExitProcess(0);
   }
   if( !SetCurrentDirectory( argv[1] ) )
   {
      printf(“Error : %s in not a valid directory !\n”, argv[1]);
      ExitProcess(0);
   }
   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 );
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.