Click here to Skip to main content
11,412,451 members (63,775 online)
Click here to Skip to main content

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..

Comments and Discussions

 
GeneralExcellant Job Pin
Charlie Curtis at 29-Oct-08 12:03
memberCharlie Curtis29-Oct-08 12:03 
GeneralRe: Excellant Job Pin
Chris Losinger at 29-Oct-08 12:29
memberChris Losinger29-Oct-08 12:29 
GeneralRe: Excellant Job Pin
Charlie Curtis at 29-Oct-08 12:37
memberCharlie Curtis29-Oct-08 12:37 
QuestionCan I have a C# version of this application over .NET 2.0 Pin
nitinr708 at 22-Aug-07 1:31
membernitinr70822-Aug-07 1:31 
QuestionBuggy Code? Pin
EScout209 at 1-Feb-07 5:37
memberEScout2091-Feb-07 5:37 
AnswerRe: Buggy Code? Pin
Chris Losinger at 1-Feb-07 5:48
memberChris Losinger1-Feb-07 5:48 
GeneralRe: Buggy Code? Pin
EScout209 at 1-Feb-07 6:28
memberEScout2091-Feb-07 6:28 
GeneralRe: Buggy Code? Pin
Chris Losinger at 1-Feb-07 7:17
memberChris Losinger1-Feb-07 7:17 
Generalworkin with simple Win32 app Pin
MATRIX- at 11-Jan-07 3:41
memberMATRIX-11-Jan-07 3:41 
GeneralRe: workin with simple Win32 app Pin
Chris Losinger at 11-Jan-07 3:50
memberChris Losinger11-Jan-07 3:50 
GeneralRe: workin with simple Win32 app Pin
MATRIX- at 12-Jan-07 3:03
memberMATRIX-12-Jan-07 3:03 
Questionerror LNK2001: unresolved external symbol Pin
Mukund - - Kumar at 25-May-06 2:58
memberMukund - - Kumar25-May-06 2:58 
AnswerRe: error LNK2001: unresolved external symbol Pin
Chris Losinger at 25-May-06 3:23
memberChris Losinger25-May-06 3:23 
GeneralRe: error LNK2001: unresolved external symbol Pin
Mukund - - Kumar at 25-May-06 3:29
memberMukund - - Kumar25-May-06 3:29 
GeneralRe: error LNK2001: unresolved external symbol Pin
Chris Losinger at 25-May-06 3:34
memberChris Losinger25-May-06 3:34 
Questionunicode file name and directory name? Pin
dungbkhn at 20-May-06 23:42
memberdungbkhn20-May-06 23:42 
Generalformat file size Pin
Roger65 at 8-Mar-06 4:35
memberRoger658-Mar-06 4:35 
GeneralReturn relative path Pin
ovolok at 6-Apr-04 1:06
memberovolok6-Apr-04 1:06 
GeneralRe: Return relative path Pin
Chris Losinger at 6-Apr-04 3:10
memberChris Losinger6-Apr-04 3:10 
Generalproblems with integrating Pin
cryptic_silences at 5-Apr-04 19:22
membercryptic_silences5-Apr-04 19:22 
Hi, thanks for the cool code mate, I can't seem to get it to work in mine though.   Here are the compiler errors I get

Compiling...
main.cpp
C:\Matthew_W\SADirRead.h(127) : error C2629: unexpected 'struct CSADirRead::CSADirEntry ('
C:\Matthew_W\SADirRead.h(127) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
C:\Matthew_W\SADirRead.h(131) : error C2146: syntax error : missing ';' before identifier 'm_sName'
C:\Matthew_W\SADirRead.h(131) : error C2501: 'CString' : missing storage-class or type specifiers
C:\Matthew_W\SADirRead.h(131) : error C2501: 'm_sName' : missing storage-class or type specifiers
C:\Matthew_W\SADirRead.h(144) : error C2146: syntax error : missing ';' before identifier 'm_sName'
C:\Matthew_W\SADirRead.h(144) : error C2501: 'CString' : missing storage-class or type specifiers
C:\Matthew_W\SADirRead.h(144) : error C2501: 'm_sName' : missing storage-class or type specifiers
C:\Matthew_W\SADirRead.h(174) : error C2143: syntax error : missing ',' before '&'
C:\Matthew_W\SADirRead.h(174) : error C2059: syntax error : '&'
C:\Matthew_W\SADirRead.h(175) : error C2146: syntax error : missing ';' before identifier 'FindFiles'
C:\Matthew_W\SADirRead.h(175) : error C2501: 'UINT' : missing storage-class or type specifiers
C:\Matthew_W\SADirRead.h(175) : error C2143: syntax error : missing ',' before '&'
C:\Matthew_W\SADirRead.h(175) : error C2059: syntax error : '&'
C:\Matthew_W\SADirRead.h(176) : error C2061: syntax error : identifier 'CString'
C:\Matthew_W\SADirRead.h(178) : error C2146: syntax error : missing ';' before identifier 'm_sSourceDir'
C:\Matthew_W\SADirRead.h(178) : error C2501: 'CString' : missing storage-class or type specifiers
C:\Matthew_W\SADirRead.h(178) : error C2501: 'm_sSourceDir' : missing storage-class or type specifiers
C:\Matthew_W\main.cpp(113) : error C2039: 'm_sName' : is not a member of 'CSAFileEntry'
            C:\Matthew_W\SADirRead.h(143) : see declaration of 'CSAFileEntry'
Error executing cl.exe.
Creating browse info file...

hrugen.exe - 19 error(s), 0 warning(s)

GeneralRe: problems with integrating Pin
cryptic_silences at 5-Apr-04 21:08
membercryptic_silences5-Apr-04 21:08 
GeneralRe: problems with integrating Pin
bart132 at 20-Jan-06 2:13
memberbart13220-Jan-06 2:13 
GeneralNewbie: How to integrate Class Pin
caribuni at 15-Jan-04 6:28
membercaribuni15-Jan-04 6:28 
GeneralRe: Newbie: How to integrate Class Pin
Chris Losinger at 15-Jan-04 7:10
memberChris Losinger15-Jan-04 7:10 
GeneralRe: Newbie: How to integrate Class Pin
caribuni at 15-Jan-04 7:30
membercaribuni15-Jan-04 7:30 

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
Web02 | 2.8.150427.1 | Last Updated 2 Mar 2003
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid