Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

CEnum - File Enumeration and File Globbing Class

, 6 Dec 2008 CPOL
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">
</head><body>
<!-- Generated by Doxygen 1.5.7.1 -->
<script type="text/javascript">
<!--
function changeDisplayState (e){
  var num=this.id.replace(/[^[0-9]/g,'');
  var button=this.firstChild;
  var sectionDiv=document.getElementById('dynsection'+num);
  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
    sectionDiv.style.display='block';
    button.src='open.gif';
  }else{
    sectionDiv.style.display='none';
    button.src='closed.gif';
  }
}
function initDynSections(){
  var divs=document.getElementsByTagName('div');
  var sectionCounter=1;
  for(var i=0;i<divs.length-1;i++){
    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
      var header=divs[i];
      var section=divs[i+1];
      var button=header.firstChild;
      if (button!='IMG'){
        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
        button=document.createElement('img');
        divs[i].insertBefore(button,divs[i].firstChild);
      }
      header.style.cursor='pointer';
      header.onclick=changeDisplayState;
      header.id='dynheader'+sectionCounter;
      button.src='closed.gif';
      section.id='dynsection'+sectionCounter;
      section.style.display='none';
      section.style.marginLeft='14px';
      sectionCounter++;
    }
  }
}
window.onload = initDynSections;
-->
</script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <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>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="index.html">A simple manual</a>
  </div>
</div>
<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>
<p>
<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><p>
<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
</pre><p>
<br>
 <br>
<p>
<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><p>
<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><p>
<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
</pre><p>
<br>
 <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="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.7.1 </small></address>
</body>
</html>

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.

License

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

Share

About the Author

Loreia

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 6 Dec 2008
Article Copyright 2008 by Loreia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid