title: How do I organize the file recursive search with file operations?
body: <p>I write for myself a small program in C ++, which could perform some operations on files that it finds (in my filter), and that's stumbled on the mechanism of searching for files. At start the program asks the full path, and then by file type recursively looking for them in all subdirectories of the selected directory. The trouble is that after performing an operation (cycle fopen - operation - fclose) can not rename or delete the file. The program simply exits with code 0. It is I sin on the file searching mechanism, as is likely, the function uses image for the time of its implementation and does not delete or rename the file. I tried different options to manage files through WinAPI, std (fstream) and just fopen / fclose. Nothing comes out.</p>
  
  <p>Code snippet: </p>
  
  <pre><code>int main() {
  
      char sPath[MAX_PATH] = "C:\\TmpDir";
  
      char sExt[10] = "doc";
      char sEXT[10] = "DOC";
      GetFileList(sPath, sExt, sEXT);
  
      printf("Results= %d\n", rez);
      system("pause");
      return 0;
  
  }
  
  void GetFileList(LPTSTR sPath, LPTSTR sExt, LPTSTR sEXT) {
      WIN32_FIND_DATA pFILEDATA;
      HANDLE hFile = FindFirstFile(strcat(sPath, "\\*.*"), &amp;pFILEDATA);
      sPath[strlen(sPath) - strlen(strstr(sPath, "*.*"))] = '\0';
      if (hFile != INVALID_HANDLE_VALUE) {
          char * chBuf;
          do {
              if (strlen(pFILEDATA.cFileName) == 1 &amp;&amp; strchr(pFILEDATA.cFileName, '.') != NULL)
                  if (FindNextFile(hFile, &amp;pFILEDATA) == 0)
                      break;
              if (strlen(pFILEDATA.cFileName) == 2 &amp;&amp; strstr(pFILEDATA.cFileName, "..") != NULL)
                  if (FindNextFile(hFile, &amp;pFILEDATA) == 0)
                      break;
              if (pFILEDATA.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) {
                  GetFileList(strcat(sPath, pFILEDATA.cFileName), sExt, sEXT);
                  sPath[strlen(sPath) - strlen(pFILEDATA.cFileName) - 1] = '\0';
              } else {
                  if ((chBuf = strrchr(pFILEDATA.cFileName, '.'))) {
                      if (strstr(chBuf + 1, sExt) || strstr(chBuf + 1, sEXT)) {
                          CharToOem(sPath, sPath);
                          printf("%s", sPath);
                          OemToChar(sPath, sPath);
                          CharToOem(pFILEDATA.cFileName, pFILEDATA.cFileName);
                          printf("%s\n", pFILEDATA.cFileName);
                          /* Какая-то операция с файлом.
                          ...
                          Конец операции с файлом. */
                          rez++;
                      }
                  }
              }
          } while (FindNextFile(hFile, &amp;pFILEDATA));
      }
  }
  </code></pre>