Ok people, i made this code (it is not complete yet) to hold all files and subdirectories inside a master directory
But, for some reason the output information come 2 times (compile it and you will se).
Why? Any information you guys need, just ask me (sorry if my code is too much confuse, im new to c++).
Code:
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
//class to hold info of each file/folder
class database{
public:
string name;
bool is_folder;
long lenght;
};
//this int is used to not overwrite other class elements when messing with subdirectories
int last_i;
int b = 0;
//function that put files in the database lass and cout some info
void search_files(database& files, string test);
//function that takes the file leght (not implemented yet)
void contar_tamanho(database& files, const char* filename);
#include "main.h"
int main()
{
database files[99];
int i = 0;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
LPCSTR hFile;
//creates one string for the user put the directory to be examinated and add a \* in the end.
string file;
cout << "Select a directory to be examinated:" << endl;
cin >> file;
file += "\\*";
hFind = FindFirstFile(file.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n",
GetLastError ());
system("Pause");
return (0);
}
else
{
cout << "File found: " << FindFileData.cFileName << "." << endl;
for (i = 0; FindNextFile(hFind, &FindFileData) != 0; i++)
{
string dottest = FindFileData.cFileName;
if (dottest != "..") search_files(files[i], FindFileData.cFileName);
else i--;
}
//here the files of each subdirectory is examinated.
for (int a = 0; a <= 99; a++){
if (files[a].is_folder == true)
{
string subdirectory = file.substr(0, file.length()-1) + files[a].name + "\\*";
cout << endl << subdirectory.substr(0, subdirectory.length()-1) << endl;
hFind = FindFirstFile(subdirectory.c_str(), &FindFileData);
for (i = last_i+1; FindNextFile(hFind, &FindFileData) != 0; i++)
{
string dottest = FindFileData.cFileName;
if (dottest != "..") search_files(files[i], FindFileData.cFileName);
else i--;
}
}
}
//subdiretory check done... now closing the program
FindClose(hFind);
system("Pause");
return (1);
}
}
void search_files(database& files, string test){
arquivos.name = test;
string::size_type dot = teste.find( ".", 0 );
if (dot != string::npos){ cout << "File found: " << test << endl; files.is_folder = false;}
else { cout << "Folder found: " << test << endl; files.is_folder = true;}
if (b == 0) last_i = 0;
else last_i++;
b++;
}
//this function is not being used yet
void contar_tamanho(database& files, const char* filename)
{
long size;
FILE *f;
f = fopen(filename, "rb");
fseek(f, 0, SEEK_END);
size = ftell(f);
fclose(f);
arquivos.lenght = size;
}
error image:
http://img181.imageshack.us/img181/6295/imagemor2.jpg