Click here to Skip to main content
13,199,026 members (41,107 online)
Click here to Skip to main content
Add your own
alternative version


79 bookmarked
Posted 23 Jan 2001

SADirRead - directory and file list class

, 2 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Scans a folder for sub-folders and files. Simple and easy to use.


CSADirRead is a class that will generate a list of files and folders contained in a folder of your choice.


First, declare a CSADirRead object

#include <span class="code-string">"SADirRead.h"</span>


CSADirRead dr;

First, we build our list of folders to scan.

dr.ClearDirs();         // start clean
dr.GetDirs("c:\\temp", true); // get all folders under c:\temp

// dump the directory list to the debug trace window:

// get the dir array
CSADirRead::SADirVector &dirs = dr.Dirs();

// loop over it
for (CSADirRead::SADirVector::iterator dit = dirs.begin(); 
     dit!=dirs.end(); dit++)
    TRACE("%s\n", (*dit).m_sName);

Now that the object knows which directories to scan, tell it to scan for files:

dr.ClearFiles();        // start clean

dr.GetFiles("*.jpg");   // get all *.JPG files in c:\temp and below

// get the file array
CSADirRead::SAFileVector &files = dr.Files();   

// dump it...
for (CSADirRead::SAFileVector::iterator fit = files.begin(); 
     fit!=files.end(); fit++)
    TRACE("%s\n", (*fit).m_sName);

Because you can access and modify the folder list before scanning for files, you can manually add folders to (or remove folders from) the folder list. Likewise, you can call GetDirs() multiple times to build up the folder list before scanning for files, like this:

dr.ClearDirs();                   // start clean
dr.GetDirs("c:\\temp", true);     // get all folders under c:\temp, recursively
dr.GetDirs("c:\\windows", false); // look in c:\windows. but don't recurse 
                                  // to sub-folders

dr.GetFiles("*.JPG"); // gets files from all of the above folders

Sorting the results

After building the file list, you can sort the files using CSADirRead::SortFiles. CSADirRead has three built-in sorting methods - alphabetic, date and size - and can sort in ascending or descending order.

Get a list of subfolders in c:\windows

CSADirRead dr;
dr.GetDirs("c:\\windows", false);
dr.GetFiles("*.*", false, true);
// get the file array
CSADirRead::SAFileVector &files = dr.Files();   

Now, files will have a list of subfolder of C:\Windows (no files included, just the folders)


  • 20 Jan 2002 - updated download
  • 24 May 2002 - updated download
  • 3 March 2003 - new revision.


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


About the Author

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

You may also be interested in...

Comments and Discussions

QuestionBuggy Code? Pin
EScout2091-Feb-07 4:37
memberEScout2091-Feb-07 4:37 
AnswerRe: Buggy Code? Pin
Chris Losinger1-Feb-07 4:48
memberChris Losinger1-Feb-07 4:48 
GeneralRe: Buggy Code? Pin
EScout2091-Feb-07 5:28
memberEScout2091-Feb-07 5:28 
GeneralRe: Buggy Code? Pin
Chris Losinger1-Feb-07 6:17
memberChris Losinger1-Feb-07 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 2 Mar 2003
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid