-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSearchRecursive.cpp
More file actions
77 lines (66 loc) · 1.86 KB
/
FileSearchRecursive.cpp
File metadata and controls
77 lines (66 loc) · 1.86 KB
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Archivo{
string name; //Nombre del archivo
};
struct Directorio {
string name; //Nombre del directorio
vector<Archivo> archivos; //Vector de archivos
vector<Directorio> subdirectorio; //Vector de subdirectorios
};
bool findFile(Directorio& dir, string& path, string& target){ //Mutacion de path con el ampersand
// Caso base
for(auto arch: dir.archivos){ //Iteracion de archivos dentro de la raiz
if(arch.name == target){ //Si el archivo es igual al target
path += "/" + dir.name; //Se agrega el nombre del directorio a la ruta
return true; //Se retorna verdadero
}
}
for(auto subdir: dir.subdirectorio){ //Iteracion de subdirectorios dentro de la raiz
string subdirpath = path + "/" + dir.name; //Se agrega el nombre del directorio a la ruta
if(findFile(subdir, subdirpath, target)){
path = subdirpath;
return true;
}
}
return false;
}
int main(){
Directorio sistemaDeArchivos = {
"root",
{
{"archivo1.txt"},
{"archivo2.txt"}
},
{
{
"subdir1",
{
{"archivo3.txt"},
},
{
{
"subdir2",
{
{"target.txt"},
{"archivo4.txt"}
},
{}
}
}
},
{"archivo5.txt"}
}
};
string target = "archivo3.txt";
string path = "";
cout << &sistemaDeArchivos << endl;
if(findFile(sistemaDeArchivos, path, target)){
cout << "Archivo encontrado en: " << path << "/" << target << endl;
} else {
cout << "Archivo no encontrado" << endl;
}
return 0;
};