Advertisement
Guest User

recursive_directory_iteration

a guest
Sep 19th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | None | 0 0
  1. // Chomp.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <stdafx.h>
  5. #include <windows.h>
  6. #include <string>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <stdlib.h>
  10. #include <cctype>
  11. #include <algorithm>
  12. #include <vector>
  13. #include <stack>
  14.  
  15. //std::ifstream inFile ( "c:/temp/input.txt" ) ;
  16. std::wofstream outFile(L"c:/temp/output.txt") ;
  17.  
  18. using namespace std;
  19.  
  20. /////////////////////////////////////////////////////////////////////////////////////////////
  21. /////////////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. void openFiles()
  24. {
  25.     if (!(outFile.is_open()))
  26.     {
  27.        printf ("Could not Create Output file\n");
  28.        exit(0);
  29.     }
  30. }
  31.  
  32. /////////////////////////////////////////////////////////////////////////////////////////////
  33. /////////////////////////////////////////////////////////////////////////////////////////////
  34.  
  35. bool ListFiles(wstring path, const wstring& mask, vector<wstring>& files)
  36. {
  37.     HANDLE hFind = INVALID_HANDLE_VALUE;
  38.     WIN32_FIND_DATA ffd;
  39.     wstring spec;
  40.     stack<wstring> directories;
  41.  
  42.     directories.push(path);
  43.     files.clear();
  44.  
  45.     while (!directories.empty())
  46.     {
  47.         path = directories.top();
  48.         spec = path + L"\\" + mask;
  49.         directories.pop();
  50.  
  51.         hFind = FindFirstFile(spec.c_str(), &ffd);
  52.         if (hFind == INVALID_HANDLE_VALUE)  
  53.             return false;
  54.  
  55.         do
  56.         {
  57.             if (wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0)
  58.             {
  59.                 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  60.                     directories.push(path + L"\\" + ffd.cFileName);
  61.                 else
  62.                     files.push_back(path + L"\\" + ffd.cFileName);
  63.             }
  64.         } while (FindNextFile(hFind, &ffd) != 0);
  65.  
  66.         if (GetLastError() != ERROR_NO_MORE_FILES)
  67.         {
  68.             FindClose(hFind);
  69.             return false;
  70.         }
  71.  
  72.         FindClose(hFind);
  73.         hFind = INVALID_HANDLE_VALUE;
  74.     }
  75.  
  76.     return true;
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////////////////////
  80. /////////////////////////////////////////////////////////////////////////////////////////////
  81.  
  82. void processFiles(const wstring& inFileName, const wstring& findFirst, const wstring& findLast )
  83. {
  84. /*    
  85.        std::string findFirst = "testing" ;
  86.        std::string findLast  = "finish" ;
  87. */      
  88.        wstring inputLine ;
  89.        int lineNum = 0 ;
  90.        wchar_t buffer[2048];
  91.        size_t found = 0;
  92.  
  93.        wifstream inFile;
  94.        inFile.open (inFileName.c_str());        // Open The file
  95.  
  96.        if (inFile.is_open())
  97.        {
  98.           while( getline( inFile, inputLine ))
  99.           {
  100.              ++lineNum ;
  101.  //          printf ("Line len = %d\n ", inputLine.length());
  102.  
  103.              if( (found = inputLine.find(findFirst)) != wstring::npos )
  104.              {
  105.                  cout << "###Line " << lineNum << " At Position [ " << found << " ]\n" ;
  106.                  swprintf_s(buffer, 2048, L"[%-5.5d] %s\n", lineNum, inputLine.c_str());
  107.                  outFile << buffer ;
  108.  
  109.                  bool foundLast = 0;
  110.                  while( getline( inFile, inputLine ))
  111.                  {
  112.                      ++lineNum ;
  113.  
  114.                      swprintf_s(buffer, 2048, L"[%-5.5d] %s\n", lineNum, inputLine.c_str());
  115.                      if( (found = inputLine.find(findLast)) != wstring::npos )
  116.                      {
  117.                          outFile << buffer ;
  118.                          break;  // Found last string - so stop after printing last line
  119.                      }
  120.                      else
  121.                           outFile << buffer ;
  122.                  }
  123.              }
  124.              else
  125.              {
  126.                 // std::cout << "=>" << inputLine << '\n' ;
  127.              }
  128.           }
  129.        }
  130.        else
  131.        {
  132.              printf ("Cant open file \n");
  133.              exit(0);
  134.        }
  135.  
  136.        inFile.close() ;     // Close The file
  137. }
  138.  
  139. /////////////////////////////////////////////////////////////////////////////////////////////
  140. ///                                   M    A    I    N
  141. /////////////////////////////////////////////////////////////////////////////////////////////
  142.  
  143. int main()
  144. {
  145.        wifstream inFile ;
  146.  
  147.        int startLine = 0;
  148.        int endLine = 0;
  149.        int lineSize = 0;
  150.        wchar_t buffer[512];
  151.  
  152.        vector<wstring> files;           // For Parsing Directory structure
  153.  
  154.        openFiles();
  155.  
  156.        // Start The Recursive parsing of Directory Structure
  157.  
  158.        if (ListFiles(L"C:\\temp", L"*.*", files))
  159.        {
  160.             for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it)
  161.             {
  162.  
  163.                 wprintf (L"Filename1 is %s\n", it->c_str());
  164.                 wprintf (L"Filename2 is %s\n", files.begin()->c_str());
  165.  
  166.                 outFile <<  "\n------------------------------\n";
  167.                 //outFile << *it  << endl;
  168.                 wcout << *it << endl;
  169.                 outFile <<  "\n------------------------------\n";
  170.  
  171.                 processFiles(*it, L"testing", L"finish");
  172.                 getchar();
  173.             }
  174.        }
  175.        outFile.close();
  176.        getchar();
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement