Recursive problem

Hi,

as many others overhere I am new to C++, that's why my script is looking very amateurish. My apologises for that. I have below script to list all files in directories and subdirectories starting from a defined folder. I have seen numerous scripts which tried to do the same, but was not able to find one which actually did the job.
Therefore I decided to invoke an array to store directories which were found to search these after the current folder was fully searched. The script actually is working fine for smaller folders and subs, but it looks like that at some point the script stops with an error. (I don't know if the output file is getting to big or if the script is aborted by an error in the script it self)

I have tried some things but I can not get it solved.

Any suggestion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <windows.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <ctype.h>
#include <string>
#include <string.h>
#include <stdio.h>

#define BUFSIZE 4096

using namespace std;
   string filenameOut="example.txt";
   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;
   string vec[20000][2];
   int telDIR=1;
   int telFILES=1;

void DeleteFile(){
     remove(  filenameOut.c_str( ) );      
}   

void WriteFile(string DType){
   ofstream myfile1;
   myfile1.open (filenameOut.c_str( ), ios::out | ios::app);      
   myfile1 <<"\n" << DType << temp.substr(0, temp.size()) << "\\" << temp1;
   myfile1.close();     
} 

void WriteFile1(string FType){  
   ofstream myfile1;
   myfile1.open (filenameOut.c_str( ), ios::out | ios::app);
   myfile1 <<"\n" << FType << temp.substr(0, temp.size()) << "\\" << temp1 <<  " - " << siZe << " bytes (" << siZekB << " kB)";
   myfile1.close();     
}   

std::string string_to_hex(const std::string& 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 < len; ++i)
    {
        const unsigned char c = input[i];
        output.push_back(lut[c >> 4]);
        output.push_back(lut[c & 15]);
    }
    return output;
}


  
int SearchDirectory(string FileSearch ,string refvecFiles,
                    bool bSearchSubdirectories)
{  
   WIN32_FIND_DATA search_data;
   memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
   HANDLE handle = FindFirstFile((refvecFiles+FileSearch).c_str(), &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;
                           WriteFile("<DIR> ");   
                           vec[telDIR][1]=temp+"\\"+temp1;
                           vec[telDIR][2]="False"; 
                           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;
                           WriteFile1("<FILE>");
                           telFILES++;
                           break;
                   }
               }  
            }while      (FindNextFile(handle, &search_data) != FALSE);
        
              for (int t=1;t<telDIR;t++){
                  if (vec[t][2]=="False"){
                  vec[t][2]="True";
                   SearchDirectory("\\*",vec[t][1], false);
                   }
              }
              break;
  
   }
   FindClose(handle);
   return 0;
   
}



int main(int argc, char* argv[])
{
    DeleteFile();
    SearchDirectory("\\*","C:\\Program Files", false);
    std::cout<<"Directories found"<< telDIR<< "\n";
    std::cout<<"Files found"<< telFILES<< "\n";     
    std::cout<<"press any key";
   _getch();

   
}
Last edited on
Topic archived. No new replies allowed.