35657

Untitled

Jul 24th, 2023
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. #include <filesystem>
  7. #include <windows.h>
  8.  
  9.  
  10. using namespace std;
  11.  
  12.  
  13. // вывод содержимого директории с указанием размера для файлов
  14. void PrintDirectoryInfo(const string& str) { // указываем путь к папке
  15.     for (const auto& a : filesystem::directory_iterator(str)) { // обход всех файлов в директории
  16.         string name = a.path().filename().string();
  17.         string full_name = str + '/' + name;
  18.         cout << setw(30) << name;
  19.         if (filesystem::is_regular_file(full_name)) {
  20.             cout << " " << setw(10) << filesystem::file_size(full_name) << " byte";
  21.         }
  22.         cout << endl;
  23.     }
  24. }
  25.  
  26. int DirectorySize(const string& str) { // указываем путь к папке
  27.     int size = 0;
  28.     for (const auto& a : filesystem::directory_iterator(str)) { // обход всех файлов в директории
  29.         string full_name = str + '/' + a.path().filename().string();
  30.         if (filesystem::is_regular_file(full_name)) {
  31.             size += filesystem::file_size(full_name);
  32.         }
  33.         if (filesystem::is_directory(full_name)) {
  34.             size += DirectorySize(full_name);
  35.         }
  36.     }
  37.     return size;
  38. }
  39.  
  40. void FindName(const string& find_name, const string& find_catalogue) {
  41.     for (const auto& a : filesystem::directory_iterator(find_catalogue)) {
  42.         string name = a.path().filename().string();
  43.         string full_name = find_catalogue + '/' + name;
  44.         if (name == find_name) {
  45.             cout << full_name << endl;
  46.         }
  47.         if (filesystem::is_directory(full_name)) {
  48.             FindName(find_name, full_name);
  49.         }
  50.     }
  51. }
  52.  
  53. void FindNameMask(const string& find_name, const string& find_catalogue) {
  54.     for (const auto& a : filesystem::directory_iterator(find_catalogue)) {
  55.         string name = a.path().filename().string();
  56.         string full_name = find_catalogue + '/' + name;
  57.         if (name.find(find_name) != string::npos) {
  58.             cout << full_name << endl;
  59.         }
  60.         if (filesystem::is_directory(full_name)) {
  61.             FindNameMask(find_name, full_name);
  62.         }
  63.     }
  64. }
  65.  
  66.  
  67. int main() {
  68.  
  69.     SetConsoleCP(1251); // установка кодировки 1251 в поток ввода
  70.     SetConsoleOutputCP(1251); // установка кодировки 1251 в поток вывода
  71.  
  72.     PrintDirectoryInfo("C:/Users/PC/Desktop");
  73.     cout << "======================" << endl;
  74.     cout << DirectorySize("C:/Users/PC/Desktop") << endl;
  75.     cout << "======================" << endl;
  76.     FindName("решения", "C:/Users/PC/Desktop");
  77.     cout << "======================" << endl;
  78.     FindNameMask("решения", "C:/Users/PC/Desktop");
  79. }
Advertisement
Add Comment
Please, Sign In to add comment