-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (80 loc) · 3.08 KB
/
Copy pathmain.cpp
File metadata and controls
112 lines (80 loc) · 3.08 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
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
#include <iostream> //
#include <string>
#include <vector>
#include <filesystem>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define WHITE "\033[37m"
struct FileInfo
{
std::wstring filename; //!use w string casue win has many file name in hind and other language
std::wstring filepath;
};
bool sortFile(const FileInfo& file1, const FileInfo& file2) {
// file1 and 2 for comp
return file1.filename < file2.filename; // if yes then true
}
int main() {
std::string targetFolder{};
std::cout << "User Add The Folder To Scan-"; // -
std::getline(std::cin, targetFolder);
std::vector<FileInfo> file{}; //! fileinfo is a vector that contain 2 value name and location
//! safely report errors
std::error_code errorbyfilesystem;
// how the iterator will work
auto option{ std::filesystem::directory_options::skip_permission_denied };
auto start{ std::filesystem::recursive_directory_iterator(targetFolder , option , errorbyfilesystem) };
auto end{ std::filesystem::recursive_directory_iterator() }; // ! this is the end point we wan't the start to be like this (mean that all file are pushed)
while (start != end) {
// ! if no error find
if (!errorbyfilesystem) {
FileInfo tempFile;
tempFile.filename = start->path().filename().wstring();
tempFile.filepath = start->path().wstring();
file.push_back(tempFile);
}
start.increment(errorbyfilesystem); // !just move to the next file if error if no error then it will act Normal
}
if (file.size() == 0) {
std::cerr << "Error(Use Real File Location)";
}
else {
std::cout << "File Size-" << file.size() << "\n";
std::sort(file.begin(), file.end(), sortFile); // ! begin and end a methold that give an object ittrator that point at the begin and end
//! and this sort work in the file vector no need to make new var ect
for (size_t i = 0; i < file.size(); i++) {
std::wcout << GREEN << "[" << file[i].filename << "]" WHITE << '\n';
}
std::cout << "----------------------------------------------------------------------\n";
std::wstring userGiveFileName{};
std::wcout << RED << "Enter File Name-" << WHITE;
// !Use getline so it captures spaces! (std::ws clears any leftover invisible 'Enters')
std::getline(std::wcin >> std::ws, userGiveFileName);
for (size_t i = 0; i < file.size(); i++) {
if (file[i].filename == userGiveFileName) {
std::wcout << "found the file" << '\n';
std::wcout << RED << "File Location:" << file[i].filepath << '\n';
std::cout << "-----------------------------------------------------------" << std::endl;
{ // ? this is called block scope
int usergiventemp{};
std::cout << GREEN << "Press (1) to open the file press (2) to not open the file-" << WHITE;
std::cin >> usergiventemp;
if (usergiventemp == 1) {
std::wstring command{ L"\"" + file[i].filepath + L"\"" };
_wsystem(command.c_str());
}
else {
}
}
}
}
}
system("pause");
return 0;
}