Click here to Skip to main content
12,954,403 members (52,464 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 24 Sep 2012

Yet Another Useful Class for Listing Files

, 24 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A class for listing files (including ADS) with callback


This is my first attempt to write an article for I have written a useful and extremely simple C++ class for listing files in a directory. I skipped recursive searching just to keep article focused on basic class style.   


Reader can read about ADS (Alternate Data Streams).

Using the code    

It has two functions to list files in a given directory with the same name (overloaded). The class looks something like following class diagram.     

The listDir functions are like following:   

int listDir(wstring csvExt, unsigned int adsReq=0); 
int listDir(PInspectFile InspectFunction, unsigned int adsReq=0);

PInspectFile is a function pointer defined like following:

typedef unsigned char   (*PInspectFile)( wstring wFilePath );
  1. int listDir(wstring csvExt, unsigned int adsReq=0);
  2. The first function takes the directory path and compares the extensions of each file against desired extension. If extension matches with desired one, it puts the filename in a public member variable ‘filenames’ which is defined like  vector<wstring> filenames;.

  3. int listDir(PInspectFile InspectFunction, unsigned int adsReq=0);
  4. The second function takes the directory path and checks the file according to a callback function. It computes the complete path of the file and then calls the provided callback function. If callback function returns 1 then it puts the filename in a public member variable ‘filenames’ which is defined like following 

    vector<wstring> filenames;

Here is one sample callback function:

unsigned char InspectFunction( wstring wFilePath){
  char ret=0; 
  DWORD dwType;
  if( GetBinaryType(wFilePath.c_str(), &dwType) ){
        if( (dwType == SCS_32BIT_BINARY)|| (dwType == SCS_64BIT_BINARY) )
              ret = 1;
  return ret; 

If user does not want ADS to be listed, no need to pass argument ‘adsReq’. It is set default to 0. But if user wants to list ADS also, then pass value 1 in the argument adsReq. If adsReq is 1, then listdir functions will also call member function GetStreams(wstring argFilename). With this class, a main program will look like this:

CListDir    sample; 

sample.listDir(InspectFunction, 0);
while( i < sample.filenames.size() ) {

Points of Interest 

I posted this article because I thought simplicity of this class may be useful to some. And I found the idea of listing file based on callback function and clubbing with ADS a little bit interesting.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer National Knowledge Network
India India
Himanshu Pareek
Currently working as software enginner at Hyderabad.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 24 Sep 2012
Article Copyright 2012 by himanshupareek
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid