Click here to Skip to main content
11,639,178 members (64,096 online)
Click here to Skip to main content

SADirRead - directory and file list class

, 2 Mar 2003 CPOL 233.6K 3.4K 79
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

 
GeneralNewbie: How to integrate Class Pin
caribuni15-Jan-04 5:28
membercaribuni15-Jan-04 5:28 
GeneralRe: Newbie: How to integrate Class Pin
Chris Losinger15-Jan-04 6:10
memberChris Losinger15-Jan-04 6:10 
GeneralRe: Newbie: How to integrate Class Pin
caribuni15-Jan-04 6:30
membercaribuni15-Jan-04 6:30 
GeneralRe: Newbie: How to integrate Class Pin
caribuni15-Jan-04 6:39
membercaribuni15-Jan-04 6:39 
GeneralRe: Newbie: How to integrate Class Pin
Chris Losinger15-Jan-04 7:48
memberChris Losinger15-Jan-04 7:48 
GeneralRe: Newbie: How to integrate Class Pin
caribuni15-Jan-04 8:14
membercaribuni15-Jan-04 8:14 
GeneralRe: Newbie: How to integrate Class Pin
Chris Losinger15-Jan-04 8:42
memberChris Losinger15-Jan-04 8:42 
GeneralRe: Newbie: How to integrate Class Pin
caribuni15-Jan-04 9:03
membercaribuni15-Jan-04 9:03 
I use vc7 and a dialog app
GeneralHelp with accessing folder Pin
vgandhi13-Jun-03 8:35
membervgandhi13-Jun-03 8:35 
GeneralFile dates are off Pin
Akolade7-May-03 8:05
memberAkolade7-May-03 8:05 
QuestionHow-TO work with directory? Pin
HuaHsin4-May-03 19:30
memberHuaHsin4-May-03 19:30 
AnswerRe: How-TO work with directory? Pin
Chris Losinger5-May-03 1:23
memberChris Losinger5-May-03 1:23 
Generalfile name from file handle ! Pin
bfadi24-Feb-03 7:42
memberbfadi24-Feb-03 7:42 
Questionhow to get path to my own executable? Pin
Jesse Evans4-Feb-03 11:34
memberJesse Evans4-Feb-03 11:34 
AnswerRe: how to get path to my own executable? Pin
Chris Losinger4-Feb-03 13:47
memberChris Losinger4-Feb-03 13:47 
GeneralRe: how to get path to my own executable? Pin
Jesse Evans5-Feb-03 7:00
memberJesse Evans5-Feb-03 7:00 
Generalfiles longer than 179 Pin
Hector T26-Nov-02 1:18
memberHector T26-Nov-02 1:18 
GeneralRe: files longer than 179 Pin
Hector T26-Nov-02 2:01
memberHector T26-Nov-02 2:01 
GeneralRe: files longer than 179 Pin
funvill11-May-05 8:29
memberfunvill11-May-05 8:29 
GeneralUnhandled exception ...access violation Pin
jimNLX18-Sep-02 9:07
memberjimNLX18-Sep-02 9:07 
GeneralRe: Unhandled exception ...access violation Pin
Chris Losinger18-Sep-02 9:09
memberChris Losinger18-Sep-02 9:09 
GeneralRe: Unhandled exception ...access violation Pin
jimNLX18-Sep-02 9:39
memberjimNLX18-Sep-02 9:39 
GeneralRe: Unhandled exception ...access violation Pin
Chris Losinger18-Sep-02 10:13
memberChris Losinger18-Sep-02 10:13 
GeneralSmall problem with find() Pin
Paresh Solanki11-Mar-02 0:16
memberParesh Solanki11-Mar-02 0:16 
GeneralRe: Small problem with find() Pin
Chris Losinger11-Mar-02 1:49
memberChris Losinger11-Mar-02 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150728.1 | Last Updated 2 Mar 2003
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid