title: Listing of files fails for a directory which begins with a "." ( eg: ".NET")
body: <p>I am trying to list all the files in a directory. Problem is that it keep on skipping some of the files which are under directory ".NET". Basically it skips any files under a directory name which start with a "." What might be the possible reason?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  void addFileToList(list&lt;string&gt;&amp; fileList,const string&amp; directory,const string&amp; excludeFilter,const WIN32_FIND_DATA &amp;FindFileData)
  {
      string fileName = string(FindFileData.cFileName);
      string filePath = directory + fileName;
  
      if (!(FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) ) 
      {
          string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);
          if (fileExtension.empty()) 
          {
              fileList.push_back(filePath);
              return;
          } 
          else if ( ( excludeFilter.find(fileExtension) == string::npos ) )
          {
              fileList.push_back(filePath);
          }
      }
  }
  
  void GetFileListing(list&lt;string&gt;&amp; fileList, string directory,const string&amp; excludeFilter,bool recursively=true)
  {
      string filePath,fileName,fileExtension;
      if (recursively)
      {
          GetFileListing(fileList, directory, excludeFilter,false);
      }
  
      WIN32_FIND_DATA FindFileData;
      HANDLE hFind = INVALID_HANDLE_VALUE;
  
      directory += "\\";
      string filter = directory + "*";
  
      // Find the first file in the directory.
      hFind = FindFirstFile(filter.c_str(), &amp;FindFileData);
  
      if (hFind == INVALID_HANDLE_VALUE)
      {
          DWORD dwError = GetLastError();
          if (dwError!=ERROR_FILE_NOT_FOUND)
          {
              //cout &lt;&lt; "Invalid file handle for filter "&lt;&lt;filter&lt;&lt;". Error is " &lt;&lt; GetLastError() &lt;&lt; endl;
          }
      }
      else
      {
          // Add the first file found to the list
          if (!recursively)
          {
              addFileToList(fileList,directory,excludeFilter,FindFileData);
          }
  
          // List all the other files in the directory.
          while (FindNextFile(hFind, &amp;FindFileData) != 0)
          {
              if (!recursively)
              {
                  addFileToList(fileList,directory,excludeFilter,FindFileData);
              }
              else
              {
                  // If we found a directory then recurse into it
                  if ((FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) &amp;&amp; FindFileData.cFileName[0]!='.')
                  {
                      GetFileListing(fileList, directory + string(FindFileData.cFileName),excludeFilter);
                  }
              }
          }
  
          DWORD dwError = GetLastError();
          FindClose(hFind);
          if (dwError != ERROR_NO_MORE_FILES)
          {
              //cout &lt;&lt; "FindNextFile error. Error is "&lt;&lt; dwError &lt;&lt; endl;
          }
      }
  }
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      list&lt;string&gt; fileList;
      list&lt;string&gt;::iterator it;
      string excludeFilter;
      string directory=".";
      GetFileListing(fileList, directory,excludeFilter);
  
      for( it=fileList.begin(); it!=fileList.end();++it) 
      {
          cout&lt;&lt;(*it)&lt;&lt;endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Updated the code:</strong></p>
  
  <p>Based on Hans suggestion, I added a check for the directory. Now it works! Any suggestions?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  void addFileToList( list&lt;string&gt;&amp; fileList,const string&amp; directory,const string&amp; excludeFilter,const string &amp;fileName )
  {
      string filePath = directory + fileName;
      string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);
  
      if (fileExtension.empty()) 
      {
          fileList.push_back(filePath);
          return;
      } 
      else if ( ( excludeFilter.find(fileExtension) == string::npos ) )
      {
          fileList.push_back(filePath);
      }
  }
  
  void GetFileListing(list&lt;string&gt;&amp; fileList, string directory,const string&amp; excludeFilter,bool recursively=true)
  {
      // If we are going to recurse over all the subdirectories, first of all
      // get all the files that are in this directory that match the filter
  
      string filePath,fileName,fileExtension;
      if (recursively)
      {
          GetFileListing(fileList, directory, excludeFilter,false);
      }
  
      WIN32_FIND_DATA FindFileData;
      HANDLE hFind = INVALID_HANDLE_VALUE;
  
      directory += "\\";
      string filter = directory + "*";
  
      // Find the first file in the directory.
      hFind = FindFirstFile(filter.c_str(), &amp;FindFileData);
  
      if (hFind == INVALID_HANDLE_VALUE)
      {
          DWORD dwError = GetLastError();
          if (dwError!=ERROR_FILE_NOT_FOUND)
          {
              //cout &lt;&lt; "Invalid file handle for filter "&lt;&lt;filter&lt;&lt;". Error is " &lt;&lt; GetLastError() &lt;&lt; endl;
          }
      }
      else
      {
          // Add the first file found to the list
          if (!recursively)
          {
              if (!(FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) )
              {
                  string fileName = string(FindFileData.cFileName);
                  addFileToList(fileList,directory,excludeFilter,fileName);
              } 
          }
  
          if(FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) 
          {
              // List all the other files in the directory.
              while (FindNextFile(hFind, &amp;FindFileData) != 0)
              {
                  if (!recursively)
                  {
                      if (!(FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) )
                      {
                          string fileName = string(FindFileData.cFileName);
                          addFileToList(fileList,directory,excludeFilter,fileName);
                      } 
                      else 
                      {
                          if ( string(FindFileData.cFileName)=="." || string(FindFileData.cFileName)==".." ) continue;
                          GetFileListing(fileList, directory + string(FindFileData.cFileName),excludeFilter);
                      }
                  }
              }
          }
  
          DWORD dwError = GetLastError();
          FindClose(hFind);
          if (dwError != ERROR_NO_MORE_FILES)
          {
              //cout &lt;&lt; "FindNextFile error. Error is "&lt;&lt; dwError &lt;&lt; endl;
          }
      }
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      list&lt;string&gt; fileList;
      list&lt;string&gt;::iterator it;
      string excludeFilter;
      string directory=".";
      GetFileListing(fileList, directory,excludeFilter);
  
      for( it=fileList.begin(); it!=fileList.end();++it) 
      {
          cout&lt;&lt;(*it)&lt;&lt;endl;
      }
  
      return 0;
  }
  </code></pre>