Click here to Skip to main content
13,042,819 members (84,329 online)
Click here to Skip to main content


48 bookmarked
Posted 30 May 2008

CEnum - File Enumeration and File Globbing Class

, 6 Dec 2008
CEnum is used for enumeration of files and directories using wildcard matching (globbing)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>CEnum1: Basic Usage</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
<!-- Generated by Doxygen -->
<script type="text/javascript">
function changeDisplayState (e){
  var button=this.firstChild;
  var sectionDiv=document.getElementById('dynsection'+num);
  if ('none'||''){'block';
function initDynSections(){
  var divs=document.getElementsByTagName('div');
  var sectionCounter=1;
  for(var i=0;i<divs.length-1;i++){
      var header=divs[i];
      var section=divs[i+1];
      var button=header.firstChild;
      if (button!='IMG'){
        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
window.onload = initDynSections;
<div class="navigation" id="top">
  <div class="tabs">
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
  <div class="navpath"><a class="el" href="index.html">A simple manual</a>
<div class="contents">
<h1><a class="anchor" name="basic">Basic Usage </a></h1>This page is for users who's main objective can be summed up as: <br>
 <em>"Forget the details and let me enumerate all files in my working directory in less than five seconds of coding"</em><br>
<b>Example 1: Enumerate all files on C: drive</b> <pre lang="c++"></pre><p>
<pre lang="c++"><a class="el" href="class_c_enum.html" title="CEnum - File Enumeration and File Globbing Class.">CEnum</a> enumerator;
enumerator.bRecursive = true;          // search subdirectories too
enumerator.bFullPath  = true;          // enumerate using file's full path</pre><p>
<pre lang="c++"><span class="code-comment">//</span> start search
enumerator.EnumerateAll(_T("C:\\"));   // root directory to start your search from</pre><p>
<pre lang="c++">list&lt;string&gt; * pAllFilesOnCDrive = enumerator.GetFiles();</pre><p>
<pre lang="c++"><span class="code-comment">//</span> congrats, you have just created a list of all files on C: drive
<span class="code-comment">//</span> lets print it</pre><p>
<pre lang="c++">list&lt;string&gt;:: iterator iter = pAllFilesOnCDrive-&gt;begin();
for (; iter != pAllFilesOnCDrive-&gt;end(); ++iter)
    cout &lt;&lt; iter-&gt;c_str();
<pre lang="c++"><span class="code-comment">//</span> at this point just move on,
<span class="code-comment">//</span> <a class="el" href="class_c_enum.html" title="CEnum - File Enumeration and File Globbing Class.">CEnum</a> will release memory allocated by pAllFilesOnCDrive for you
<b>Example 2: enumerate all MP3's on C: drive</b> <pre lang="c++">// first uncomment this line: //# define MFC, then recompile</pre><p>
<pre lang="c++"><a class="el" href="class_c_enum.html" title="CEnum - File Enumeration and File Globbing Class.">CEnum</a> enumerator;
enumerator.sIncPatternFiles  = _T("*.mp3");
enumerator.bNoCaseFiles      = true;       // enumerate both *.mp3 and *.MP3
enumerator.bRecursive        = true;
enumerator.bFullPath         = true;</pre><p>
<pre lang="c++"><span class="code-comment">//</span> start search
enumerator.EnumerateAll(_T("C:"));         // unlike in the first example, this time
                                           <span class="code-comment">//</span> there is no backslash at the end
                                           <span class="code-comment">//</span> of path string</pre><p>
<pre lang="c++">CStringArray * pCStringArray = enumerator.GetFilesAsCStringArray();
for(int i=0; i&lt;pCStringArray-&gt;GetSize(); ++i)
    cout &lt;&lt; (LPCTSTR) pCStringArray-&gt;GetAt[i];
<pre lang="c++"><span class="code-comment">//</span> or if you preffer CStringList...
CStringList  * pCStringList  = enumerator.GetFilesAsCStringList();
POSITION pos = pCStringList-&gt;GetHeadPosition();
while (pos != NULL)
    cout &lt;&lt; (LPCTSTR) pCStringList-&gt;GetNext(pos);
<pre lang="c++"><span class="code-comment">//</span> once again, memory management is taken care of by <a class="el" href="class_c_enum.html" title="CEnum - File Enumeration and File Globbing Class.">CEnum</a>.
<span class="code-comment">//</span> at this point you can just forget that pCStringArray and pCStringList even existed
 <dl class="note" compact><dt><b>Note:</b></dt><dd>For your convenience, the path to root folder may or may not end with backslash!</dd></dl>
This is already enough knowledge for your basic use of <a class="el" href="class_c_enum.html" title="CEnum - File Enumeration and File Globbing Class.">CEnum</a>, for details read <a class="el" href="advanced.html">Advanced Usage</a> </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Wed Dec 3 12:45:12 2008 for CEnum1 by&nbsp;
<a href="">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> </small></address>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Croatia Croatia
Nothing to brag about.
Just another engineer who works as a software developer last couple of years.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 Dec 2008
Article Copyright 2008 by Loreia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid