title: Trouble with recursive file search
body: <p>I am trying to write a script to recursivly scan a disk and return the total numbers of directories, files and also display the largest file found.
  I know that there is much information to find on StackOverflow or on the web, but all the examples I found so far seem to have issues with really scan a disk recursivly. 
  Below script uses the FindFile and FindNextFile functions to scan for files. If the file atribute returns that the the function found a directory it writes the name into a list to be searched later. After the current directory has been searched the script takes the first item (being a directory to be scanned) of the list, removes that item from the list and scans this directory.</p>
  
  <p>My problem is that after scanning a few hundred directories and subdirectories the program end with an error</p>
  
  <pre><code>Unhandled exception at ..... (ntdll.dll) in File Lister 6.0.exe: .....: Stack overflow
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Unhandled exception at ..... (ntdll.dll) in File Lister 6.0.exe: ......: Access violation writing location 0x00f30fe8.
  </code></pre>
  
  <p>But the then scanned directory is never the same.</p>
  
  <p>I have been trying to solves this issue but can not find a clear reason for it. 
  Therefore I would much appreciate if someone could help</p>
  
  <p>Below is my code. I know it's looking very novice, my apologises for that</p>
  
  <pre><code>    #include &lt;windows.h&gt;
      #include &lt;iostream&gt;
      #include &lt;fstream&gt;
      #include &lt;conio.h&gt;
      #include &lt;ctype.h&gt;
      #include &lt;string&gt;
      #include &lt;string.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;direct.h&gt;
      #include &lt;list&gt;
  
      using namespace std;
  
  
      list &lt;string&gt; myList;
      std::list&lt;string&gt;::iterator it;
  
         int siZe;
         int siZekB;
         bool  bSearchSubdirectories=true;
         HANDLE handle;
         LPCTSTR strPattern;
         std::string temp;
         std::string temp1;
         std::string temp2;
         std::string temp3;
         WIN32_FIND_DATA search_data;
         int DIRPlace;
         int MaxDir=400000000;
  
         int telDIR=1;
         int telFILES=1;
         double LargestFile=0;
         string LargestFileName;
         string SDir;
  
  
      std::string string_to_hex(const std::string&amp; input)
      {
          static const char* const lut = "0123456789ABCDEF";
          size_t len = input.length();
  
          std::string output;
          output.reserve(2 * len);
          for (size_t i = 0; i &lt; len; ++i)
          {
              const unsigned char c = input[i];
              output.push_back(lut[c &gt;&gt; 4]);
              output.push_back(lut[c &amp; 15]);
          }
          return output;
      }
  
  
  
      int SearchDirectory(string FileSearch ,string refvecFiles,
                          bool bSearchSubdirectories)
      {  
  
         WIN32_FIND_DATA search_data;
         memset(&amp;search_data, 0, sizeof(WIN32_FIND_DATA));
         HANDLE handle = FindFirstFile((refvecFiles+FileSearch).c_str(), &amp;search_data);  
         temp = refvecFiles;
         while(handle != INVALID_HANDLE_VALUE)   {
  
                 do{
                     if (search_data.cFileName[0]!='.'){  
                             temp2=search_data.dwFileAttributes;
                             temp3=string_to_hex(temp2);
                             DIRPlace=strlen(temp3.c_str())-1;
                         switch (temp3[DIRPlace-1])
                         {
                            case '1':    //Directory
                                 temp = refvecFiles;
                                 temp1=search_data.cFileName;
                                 temp2=search_data.dwFileAttributes;
                                 myList.push_back(temp+temp1);
                                 telDIR++;
                                 break; 
                           default:   //Other types (Files etc)
                                 siZe=(search_data.nFileSizeHigh * (MAXDWORD+1)) + search_data.nFileSizeLow;
                                 siZekB=((search_data.nFileSizeHigh * (MAXDWORD+1)) + search_data.nFileSizeLow)/1024;
                                 temp = refvecFiles;
                                 temp1=search_data.cFileName;
                                 temp2=search_data.dwFileAttributes;
                                 if (siZekB&gt;LargestFile){
                                    LargestFile=siZekB;
                                    LargestFileName=temp.substr(0, temp.size()) + "\\" + temp1;
                                 }
                                 telFILES++;
                                 break;
                         }
                     }  
                  }while      (FindNextFile(handle, &amp;search_data) != FALSE &amp;&amp; telDIR&lt;MaxDir);
                                    string line,SearD, LineFiller,FrontString, BackString;
  
  
                        it=myList.begin();
                          SearD=*it+"\\\\";
                          myList.remove(*it);
                        if (SearD.length()&gt;60)
                        {
                            FrontString=SearD.substr(0,10);
                            BackString=SearD.substr(SearD.length()-44);
                            LineFiller="......";
                            FrontString=FrontString+LineFiller+BackString;
                        }else{
                          FrontString=SearD;
                        }
                        cout&lt;&lt;"Exploring:                                                              "&lt;&lt;"\r";
                        cout&lt;&lt;"Exploring: "&lt;&lt;FrontString&lt;&lt;"\r\r";
                        SearchDirectory("\\*",SearD, false);     
         }
         FindClose(handle);
         return 0;
  
      }
  
  
  
  
  
      int main(int argc, char* argv[])
      {   
          std::cout&lt;&lt; "Enter directory to be searched: ";
          getline(cin,SDir);
          std::cout&lt;&lt; "\n";
          try{
          SearchDirectory("\\*",SDir, false);
          }catch (int e)
    {
      cout &lt;&lt; "An exception occurred. Exception Nr. " &lt;&lt; e &lt;&lt; '\n';
    }
                  std::cout&lt;&lt; "\n"&lt;&lt;"\n";
          std::cout&lt;&lt;"Directories found: "&lt;&lt; telDIR&lt;&lt; "\n";
          std::cout&lt;&lt;"Files found: "&lt;&lt; telFILES&lt;&lt; "\n"; 
          std::cout&lt;&lt;"Largest File: "&lt;&lt;LargestFileName &lt;&lt; " ("&lt;&lt; LargestFile &lt;&lt; " kB)"&lt;&lt;"\n";     
          std::cout&lt;&lt;"press any key";
          getch();       
      }
  </code></pre>
  
  <p>EDIT: Below is a snapshot from the debugger Call Stack</p>
  
  <pre><code>&gt;   File Lister 6.0.exe!std::operator&lt;&lt;&lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt; &amp; _Ostr={...}, const char * _Val=0x000d51c8)  Line 791 + 0x20 bytes   C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\numeric\\interval\\", bool bSearchSubdirectories=false)  Line 110 + 0x16 bytes   C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\numeric\\conversion\\", bool bSearchSubdirectories=false)  Line 113  C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\multi_index\\detail\\", bool bSearchSubdirectories=false)  Line 113  C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\multiprecision\\traits\\", bool bSearchSubdirectories=false)  Line 113   C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\multiprecision\\detail\\", bool bSearchSubdirectories=false)  Line 113   C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\multiprecision\\cpp_int\\", bool bSearchSubdirectories=false)  Line 113  C++
      File Lister 6.0.exe!SearchDirectory(std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; FileSearch="\*", std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; refvecFiles="c:\\boost\\multiprecision\\concepts\\", bool bSearchSubdirectories=false)  Line 113 C++
  </code></pre>
  
  <p>The debugger stops in ostream at</p>
  
  <pre><code>if (_State == ios_base::goodbit
      &amp;&amp; _Ostr.rdbuf()-&gt;sputn(_Val, _Count) != _Count)
      _State |= ios_base::badbit;
  </code></pre>