title: How can I obtain all of the file names in a directory?
body: <p>I am writing a program in C++. I am trying to obtain all of the files in the folder where the program executable is located and store them in a vector. I have been told that the following code should work, but the FindFirstFile operation only finds one file (the name of the folder that it should be searching through). How can I change the code so that it looks through the folder properly?</p>
  
  <pre><code>std::vector&lt;char*&gt; fileArray;
  
  //Get location of program executable
  HMODULE hModule = GetModuleHandleW(NULL);
  WCHAR path[MAX_PATH];
  GetModuleFileNameW(hModule, path, MAX_PATH);
  
  //Remove the executable file name from 'path' so that it refers to the folder instead
  PathCchRemoveFileSpec(path, sizeof(path));
  
  //This code should find the first file in the executable folder, but it fails
  //Instead, it obtains the name of the folder that it is searching
  WIN32_FIND_DATA ffd;
  HANDLE hFind = INVALID_HANDLE_VALUE;
  hFind = FindFirstFile(path, &amp;ffd);
  
  do
  {
      //The name of the folder is pushed onto the array because of the previous code's mistake
      //e.g. If the folder is "C:\\MyFolder", it stores "MyFolder"
      fileArray.push_back(ffd.cFileName); //Disclaimer: This line of code won't add the file name properly (I'll get to fixing it later), but that's not relevant to the question
  } while (FindNextFile(hFind, &amp;ffd) != 0); //This line fails to find anymore files
  </code></pre>