title: c++ - extract all files names - FindFirstFile
body: <p>I want to get a list of all files in particular directory.For that |I use the following code</p>
  
  <pre><code>void GetFileListing(std::list&lt;std::string&gt;&amp; listing, std::string directory, std::string fileFilter, bool recursively=true)
  {
  
  
  //check if directory exits:
  
       DWORD attribs = ::GetFileAttributesA(directory.c_str());
    if (attribs == INVALID_FILE_ATTRIBUTES || !(attribs &amp; FILE_ATTRIBUTE_DIRECTORY)) {
      return ;
    }
  
    // 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
    if (recursively)
      GetFileListing(listing, directory, fileFilter, false);
  
    directory += "\\";
  
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
  
    // Setup the filter according to whether we are getting the directories
    // or just the files
    std::string filter = directory + (recursively ? "*" : fileFilter);
  
    // Find the first file in the directory.
    hFind = FindFirstFile(LPCWSTR(filter.c_str()), &amp;FindFileData);
  
    if (hFind == INVALID_HANDLE_VALUE)
    {
      DWORD dwError = GetLastError();
      if (dwError!=ERROR_FILE_NOT_FOUND)
      {
        std::cout &lt;&lt; "Invalid file handle for filter "&lt;&lt;filter&lt;&lt;". Error is " &lt;&lt; GetLastError() &lt;&lt; std::endl;
      }
    }
    else
    {
      // Add the first file found to the list
      if (!recursively)
      {
          wstring wFindFileData = FindFileData.cFileName;
        listing.push_back(directory + std::string(wFindFileData.begin(),wFindFileData.end()));
      }
  
      // List all the other files in the directory.
      while (FindNextFile(hFind, &amp;FindFileData) != 0)
      {
        if (!recursively)
        {
          wstring wFindFileData = FindFileData.cFileName;
          listing.push_back(directory + std::string(wFindFileData.begin(),wFindFileData.end()));
        }
        else
        {
          // If we found a directory then recurse into it
          if ((FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)&gt;0 &amp;&amp; FindFileData.cFileName[0]!='.')
          {
              wstring wFindFileData = FindFileData.cFileName;
            GetFileListing(listing, directory + std::string(wFindFileData.begin(),wFindFileData.end()), fileFilter);
          }
        }
      }
  
      DWORD dwError = GetLastError();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES)
      {
        std::cout &lt;&lt; "FindNextFile error. Error is "&lt;&lt; dwError &lt;&lt; std::endl;
      }
    }
  }
  
  int main(int argc, char *argv[])
  {
      if(argc &lt; 2)
          return 0;
      std::list&lt;std::string&gt; listing;
      GetFileListing(listing, argv[1], "*");
      for(std::list&lt;std::string&gt;::iterator it = listing.begin(); it!=listing.end();++it)
      {
          std::cout &lt;&lt; *it &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p>In args I transfer a valid existing directory .Actualy args[1] becomes "C:\dir".
  But I don`t get a desired list,instead of it the following error occurs :</p>
  
  <pre><code>Invalid file handle for filter C:\dir\*. Error is 123
  </code></pre>
  
  <p>I can`t understand what is wrong here?</p>