Click here to Skip to main content
Licence CPOL
First Posted 23 Jan 2001
Views 208,249
Downloads 2,750
Bookmarked 74 times

SADirRead - directory and file list class

By Chris Losinger | 2 Mar 2003
Scans a folder for sub-folders and files. Simple and easy to use.

1
1 vote, 3.7%
2
1 vote, 3.7%
3
4 votes, 14.8%
4
21 votes, 77.8%
5
4.88/5 - 53 votes
2 removed
μ 4.48, σa 1.28 [?]

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)

About the Author

Chris Losinger

Software Developer

United States United States

Member
Chris Losinger is the president of Smaller Animals Software, Inc..

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExcellant Job PinmemberCharlie Curtis12:03 29 Oct '08  
GeneralRe: Excellant Job PinsupporterChris Losinger12:29 29 Oct '08  
GeneralRe: Excellant Job PinmemberCharlie Curtis12:37 29 Oct '08  
QuestionCan I have a C# version of this application over .NET 2.0 Pinmembernitinr7081:31 22 Aug '07  
QuestionBuggy Code? PinmemberEScout2095:37 1 Feb '07  
AnswerRe: Buggy Code? PinsupporterChris Losinger5:48 1 Feb '07  
GeneralRe: Buggy Code? PinmemberEScout2096:28 1 Feb '07  
GeneralRe: Buggy Code? PinsupporterChris Losinger7:17 1 Feb '07  
Generalworkin with simple Win32 app PinmemberMATRIX-3:41 11 Jan '07  
GeneralRe: workin with simple Win32 app PinsupporterChris Losinger3:50 11 Jan '07  
GeneralRe: workin with simple Win32 app PinmemberMATRIX-3:03 12 Jan '07  
Questionerror LNK2001: unresolved external symbol PinmemberMukund - - Kumar2:58 25 May '06  
AnswerRe: error LNK2001: unresolved external symbol PinsupporterChris Losinger3:23 25 May '06  
GeneralRe: error LNK2001: unresolved external symbol PinmemberMukund - - Kumar3:29 25 May '06  
GeneralRe: error LNK2001: unresolved external symbol PinsupporterChris Losinger3:34 25 May '06  
Questionunicode file name and directory name? Pinmemberdungbkhn23:42 20 May '06  
Generalformat file size PinmemberRoger654:35 8 Mar '06  
GeneralReturn relative path Pinmemberovolok1:06 6 Apr '04  
GeneralRe: Return relative path PinsupporterChris Losinger3:10 6 Apr '04  
Generalproblems with integrating Pinmembercryptic_silences19:22 5 Apr '04  
GeneralRe: problems with integrating Pinmembercryptic_silences21:08 5 Apr '04  
GeneralRe: problems with integrating Pinmemberbart1322:13 20 Jan '06  
GeneralNewbie: How to integrate Class Pinmembercaribuni6:28 15 Jan '04  
GeneralRe: Newbie: How to integrate Class PinsupporterChris Losinger7:10 15 Jan '04  
GeneralRe: Newbie: How to integrate Class Pinmembercaribuni7:30 15 Jan '04  

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.

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