title: Using recursion to get all files in some directory
body: <p>I try to write application that find all files in current diretory and it's subdirectories. I have next problem I can't completely understand</p>
  
  <ol>
  <li>How I need use recursion <code>GetFiles()</code> function in my case, and</li>
  <li>Where I need free memory that was dynamically allocated in <code>PathCreator()</code> function. When I'm testing this program only for some directory (without subdirectories) it works (look commented code in <code>_tmain()</code>). But when I'm try to get all files there is crash.</li>
  </ol>
  
  <p>Here is my code: </p>
  
  <pre><code>// ConsoleApplication1.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #define SIZE 300
  
  int is_directory(wchar_t *p)
  {
      wchar_t temp[300];
      int i;
      i = 0;
      while(*p != '\0')
      {
          temp[i] = *p;
          p++;
          i++;
      }
      temp[i] = '\\';
      i++;
      temp[i] = '\0';
      WIN32_FIND_DATA file;
      HANDLE search_hendle = FindFirstFile(temp, &amp;file);
      long error = GetLastError();
      if(error == 267)
      {
          return 0;
      }
      else
      {
          return 1;
      }
  }
  
  
  wchar_t *PathCreator(wchar_t *dir, wchar_t *fileName)
  {
      wchar_t* path = new wchar_t[SIZE];
      int j = 0;
      while(j &lt; SIZE)
      {
          path[j] = '\0';
          j++;
      }
      int i;
      i = 0;
      while(*dir != '*' &amp;&amp; *dir != '\0')
      {
          path[i] = *dir;
          i++;
          dir++;
      }
      wchar_t *t = fileName;  
      while(*t != '\0')
      {
          path[i] = *t;
          i++;
          t++;
      }
      path[i] = '\0';
      return path;
  } 
  
  wchar_t* allFlsArr[SIZE];
  int i = 0;
  wchar_t **GetAllFiles(wchar_t* dir)
  {
      WIN32_FIND_DATA file;
      HANDLE search_hendle = FindFirstFile(dir, &amp;file);
      if(search_hendle)
      {
          do
          {
              wchar_t *p = PathCreator(dir,file.cFileName);
              allFlsArr[i++] = p;
          }
          while(FindNextFile(search_hendle, &amp;file));
          allFlsArr[i] = '\0';
      }
      CloseHandle(search_hendle);
      return allFlsArr;
  }
  
  void GetFiles(wchar_t *dir)
  {
      wchar_t **filePaths = 0;
      filePaths = GetAllFiles(dir);
      int  i = 0;
      while(filePaths[i] != '\0'){
          if(!is_directory(filePaths[i]))
          {
              std::wcout &lt;&lt; filePaths[i] &lt;&lt; std::endl;
          }
          else
          {
              GetAllFiles(filePaths[i]);
          }
      }
      delete [] filePaths;
  }
  int _tmain(int argc, _TCHAR* argv[])
  {
      /*wchar_t **wch = GetAllFiles(L"C:\\*");
      int i = 0;
      while( *wch != '\0')
      {
          std::wcout &lt;&lt; *wch &lt;&lt; std::endl;
          wch++;
      }*/
      GetFiles(L"C:\\*");
  }
  </code></pre>