Click here to Skip to main content
12,242,431 members (50,367 online)
Click here to Skip to main content
Add your own
alternative version

Stats

237K views
3.4K downloads
79 bookmarked
Posted

SADirRead - directory and file list class

, 2 Mar 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
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 <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.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)

Share

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

 
GeneralRe: "Look In" Combo Box Pin
George Clarence24-Jan-02 3:38
memberGeorge Clarence24-Jan-02 3:38 
GeneralRe: "Look In" Combo Box Pin
Chris Losinger11-Mar-02 2:48
memberChris Losinger11-Mar-02 2:48 
QuestionWhat changes were made... Pin
J.G. Hattingh20-Jan-02 6:34
memberJ.G. Hattingh20-Jan-02 6:34 
AnswerRe: What changes were made... Pin
Chris Losinger20-Jan-02 6:34
memberChris Losinger20-Jan-02 6:34 
GeneralCToolBarCtrl Help!! Pin
Silky Octopus19-Jan-02 6:05
memberSilky Octopus19-Jan-02 6:05 
GeneralRe: CToolBarCtrl Help!! Pin
Silky Octopus19-Jan-02 7:21
memberSilky Octopus19-Jan-02 7:21 
GeneralGet File Date Pin
Picasso18-Jan-02 6:57
memberPicasso18-Jan-02 6:57 
GeneralRe: Get File Date Pin
Chris Losinger18-Jan-02 6:58
memberChris Losinger18-Jan-02 6:58 
GeneralRe: Get File Date Pin
Picasso19-Jan-02 5:33
memberPicasso19-Jan-02 5:33 
GeneralRe: Get File Date Pin
Chris Losinger19-Jan-02 5:37
memberChris Losinger19-Jan-02 5:37 
GeneralOoops. don't download yet - not updated Pin
Chris Losinger19-Jan-02 5:39
memberChris Losinger19-Jan-02 5:39 
Generalsort by date takes too long Pin
mike dilworth27-Nov-01 22:22
membermike dilworth27-Nov-01 22:22 
GeneralRe: sort by date takes too long Pin
Chris Losinger28-Nov-01 2:53
memberChris Losinger28-Nov-01 2:53 
GeneralRe: sort by date takes too long Pin
mike dilworth28-Nov-01 3:04
membermike dilworth28-Nov-01 3:04 
GeneralRe: sort by date takes too long Pin
Chris Losinger28-Nov-01 3:54
memberChris Losinger28-Nov-01 3:54 
Generalit seemed that the Demo can not be used Pin
haozl6-Sep-01 16:29
memberhaozl6-Sep-01 16:29 
GeneralRe: it seemed that the Demo can not be used Pin
Chris Losinger7-Sep-01 3:17
memberChris Losinger7-Sep-01 3:17 
GeneralRe: it seemed that the Demo can not be used Pin
anhbucon24-Oct-02 13:21
sussanhbucon24-Oct-02 13:21 
GeneralRe: it seemed that the Demo can not be used Pin
Chris Losinger24-Oct-02 13:26
memberChris Losinger24-Oct-02 13:26 
GeneralDirectories on network computers Pin
Anonymous6-Sep-01 5:08
memberAnonymous6-Sep-01 5:08 
GeneralRe: Directories on network computers Pin
pesglobe8-Jan-02 2:54
memberpesglobe8-Jan-02 2:54 
GeneralRe: Directories on network computers Pin
Chris Losinger8-Jan-02 8:20
memberChris Losinger8-Jan-02 8:20 
GeneralOK, done Pin
Chris Losinger10-Jan-02 4:02
memberChris Losinger10-Jan-02 4:02 
GeneralRe: OK, done Pin
Chris Maunder10-Jan-02 4:16
memberChris Maunder10-Jan-02 4:16 
GeneralRe: OK, done Pin
Chris Losinger10-Jan-02 5:22
memberChris Losinger10-Jan-02 5:22 
GeneralWeird Output Pin
James M. Kralec27-Jul-01 11:01
memberJames M. Kralec27-Jul-01 11:01 
GeneralRe: Weird Output Pin
Chris Losinger27-Jul-01 11:12
memberChris Losinger27-Jul-01 11:12 
GeneralRe: Weird Output Pin
Chris Losinger27-Jul-01 11:15
memberChris Losinger27-Jul-01 11:15 
GeneralRe: Weird Output Pin
James M. Kralec30-Jul-01 3:27
memberJames M. Kralec30-Jul-01 3:27 
GeneralSubDirs and recursion Pin
CG27-Jul-01 5:35
memberCG27-Jul-01 5:35 
GeneralRe: SubDirs and recursion Pin
Chris Losinger27-Jul-01 5:41
memberChris Losinger27-Jul-01 5:41 
QuestionHow to delete file or folder Pin
SeanLin20-Mar-01 20:41
memberSeanLin20-Mar-01 20:41 
AnswerRe: How to delete file or folder Pin
Chris Losinger7-May-01 19:23
memberChris Losinger7-May-01 19:23 
GeneralNo dirs in File list please... Pin
Leon Matthews23-Feb-01 2:20
memberLeon Matthews23-Feb-01 2:20 
QuestionIsn't some of this code not y2k compliant? Pin
Darren Schroeder27-Jan-01 7:42
memberDarren Schroeder27-Jan-01 7:42 
AnswerRe: no Pin
Chris Losinger27-Jan-01 9:49
memberChris Losinger27-Jan-01 9: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.

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