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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <windows.h>
#include <queue>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <cctype>
#include <vector>
#include <sstream>
#include <fstream>
#include <iomanip>
const HANDLE HNULL = INVALID_HANDLE_VALUE;
const int A_DIR = FILE_ATTRIBUTE_DIRECTORY;
std::ostream &operator<<(std::ostream &os, FILETIME const &ft)
{
SYSTEMTIME st;
FileTimeToSystemTime(&ft, &st);
return os << st.wHour << ":" << st.wMinute << ":" << st.wSecond << " " << st.wYear << "/" << st.wMonth << "/" << st.wDay;
}
int get_file_size(std::string filename)
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(), "rb");
fseek(p_file, 0, SEEK_END);
int size = ftell(p_file);
fclose(p_file);
return size;
}
void process(std::string
const path, WIN32_FIND_DATA
const file, std::string asking_file_size, std::string word_in_title)
{
std::string file_perso;
file_perso = file.cFileName;
std::string ext_file;
std::string ask_size;
int asking;
if (file_perso.find_last_of(".") != std::string::npos)
{
ext_file = file_perso.substr(file_perso.find_last_of(".") + 1);
}
asking = atoi(asking_file_size.c_str());
std::string path_file = path + file.cFileName;
if (asking == 0)
{
if (word_in_title == "-")
{
std::cout << path << file_perso << "\n";
std::cout << file.ftCreationTime << "\t" << ext_file << "\t" << get_file_size(path_file) << " octets" << "\n";
std::cout << "\n";
}
else if (word_in_title != "-")
{
std::size_t found_one = file_perso.find(word_in_title);
if (found_one != std::string::npos)
{
std::cout << path << file_perso << "\n";
std::cout << file.ftCreationTime << "\t" << ext_file << "\t" << get_file_size(path_file) << " octets" << "\n";
std::cout << "\n";
}
}
}
else if (get_file_size(path_file) < asking)
{
if (word_in_title == "-")
{
std::cout << path << file_perso << "\n";
std::cout << file.ftCreationTime << "\t" << ext_file << "\t" << get_file_size(path_file) << " octets" << "\n";
std::cout << "\n";
}
else if (word_in_title != "-")
{
std::size_t found_two = file_perso.find(word_in_title);
if (found_two != std::string::npos)
{
std::cout << path << file_perso << "\n";
std::cout << file.ftCreationTime << "\t" << ext_file << "\t" << get_file_size(path_file) << " octets" << "\n";
std::cout << "\n";
}
}
}
}
void find_file(std::string
const folder_name, std::string
const fmask, std::string asking_file_size, std::string word_in_title)
{
HANDLE finder;
WIN32_FIND_DATA file;
std::priority_queue<std::string, std::vector<std::string>, std::greater<std::string>> dirs;
dirs.push(folder_name);
do {
std::string path = dirs.top();
dirs.pop();
if (path[path.size() - 1] != '\\')
{
path += "\\";
}
std::string mask = path + fmask;
if (HNULL == (finder = FindFirstFile(mask.c_str(), &file)))
{
continue;
}
do {
if (!(file.dwFileAttributes &A_DIR))
{
process(path, file, asking_file_size, word_in_title);
}
} while (FindNextFile(finder, &file));
FindClose(finder);
if (HNULL == (finder = FindFirstFile((path + "*").c_str(), &file)))
{
continue;
}
do {
if ((file.dwFileAttributes &A_DIR) && (file.cFileName[0] != '.'))
{
dirs.push(path + file.cFileName);
}
} while (FindNextFile(finder, &file));
FindClose(finder);
} while (!dirs.empty());
}
int main(int argc, char **argv)
{
// usage : program_name.exe c:\ *.txt 300 -
// program_name.exe path ext file size word_to_find_in_file_title
if (argc > 4)
{
find_file(argv[1], argv[2], argv[3], argv[4]);
}
else
{
find_file("C:\\", "*", "0", "-");
}
return 0;
}
|