Click here to Skip to main content
15,887,135 members
Articles / Desktop Programming / MFC
Article

SADirRead - directory and file list class

Rate me:
Please Sign up or sign in to vote.
4.88/5 (27 votes)
2 Mar 2003CPOL 284.5K   3.6K   80   86
Scans a folder for sub-folders and files. Simple and easy to use.

Introduction

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

Usage

First, declare a CSADirRead object

#include "SADirRead.h"

...

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.ClearFiles();
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)

History

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

License

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


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralRe: it seemed that the Demo can not be used Pin
Chris Losinger7-Sep-01 2:17
professionalChris Losinger7-Sep-01 2:17 
GeneralRe: it seemed that the Demo can not be used Pin
anhbucon24-Oct-02 12:21
sussanhbucon24-Oct-02 12:21 
GeneralRe: it seemed that the Demo can not be used Pin
Chris Losinger24-Oct-02 12:26
professionalChris Losinger24-Oct-02 12:26 
GeneralDirectories on network computers Pin
6-Sep-01 4:08
suss6-Sep-01 4:08 
GeneralRe: Directories on network computers Pin
pesglobe8-Jan-02 1:54
pesglobe8-Jan-02 1:54 
GeneralRe: Directories on network computers Pin
Chris Losinger8-Jan-02 7:20
professionalChris Losinger8-Jan-02 7:20 
GeneralOK, done Pin
Chris Losinger10-Jan-02 3:02
professionalChris Losinger10-Jan-02 3:02 
GeneralRe: OK, done Pin
Chris Maunder10-Jan-02 3:16
cofounderChris Maunder10-Jan-02 3:16 
Consider it updated. Smile | :)

cheers,
Chris Maunder
GeneralRe: OK, done Pin
Chris Losinger10-Jan-02 4:22
professionalChris Losinger10-Jan-02 4:22 
GeneralWeird Output Pin
James M. Kralec27-Jul-01 10:01
James M. Kralec27-Jul-01 10:01 
GeneralRe: Weird Output Pin
Chris Losinger27-Jul-01 10:12
professionalChris Losinger27-Jul-01 10:12 
GeneralRe: Weird Output Pin
Chris Losinger27-Jul-01 10:15
professionalChris Losinger27-Jul-01 10:15 
GeneralRe: Weird Output Pin
James M. Kralec30-Jul-01 2:27
James M. Kralec30-Jul-01 2:27 
GeneralSubDirs and recursion Pin
27-Jul-01 4:35
suss27-Jul-01 4:35 
GeneralRe: SubDirs and recursion Pin
Chris Losinger27-Jul-01 4:41
professionalChris Losinger27-Jul-01 4:41 
QuestionHow to delete file or folder Pin
20-Mar-01 19:41
suss20-Mar-01 19:41 
AnswerRe: How to delete file or folder Pin
Chris Losinger7-May-01 18:23
professionalChris Losinger7-May-01 18:23 
GeneralNo dirs in File list please... Pin
Leon Matthews23-Feb-01 1:20
Leon Matthews23-Feb-01 1:20 
QuestionIsn't some of this code not y2k compliant? Pin
Darren Schroeder27-Jan-01 6:42
Darren Schroeder27-Jan-01 6:42 
AnswerRe: no Pin
Chris Losinger27-Jan-01 8:49
professionalChris Losinger27-Jan-01 8:49 

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.