Advertisement
Baxram97

Untitled

Mar 10th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <io.h> // не робит
  4. #include <dirent.h>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. void renameFile();
  10.  
  11. void removeFile();
  12.  
  13. void Dir();
  14.  
  15.  
  16. void renameDirectory();
  17.  
  18. void removeDirectory();
  19.  
  20. void createDirectory();
  21.  
  22.  
  23. int main() {
  24.  
  25.     enum m {
  26.         RENAME = 1, REMOVE = 2, DIRECTORY = 3, EXIT = 4
  27.     };
  28.     int menu;
  29.     do {
  30.         system("clear");
  31.  
  32.         cout << "1. Rename file\n2. Remove file\n3. Show all files or directory\n4. Exit" << endl;
  33.         cout << "Enter item from menu: ";
  34.         cin >> menu;
  35.         switch (menu) {
  36.             case RENAME:
  37.                 renameFile();
  38.                 break;
  39.             case REMOVE:
  40.                 removeFile();
  41.                 break;
  42.             case DIRECTORY:
  43.                 Dir();
  44.             case EXIT:
  45.                 exit(1);
  46.             default:
  47.                 cout << "Error! " << endl;
  48.                 break;
  49.         }
  50.     } while (true);
  51.  
  52. }
  53.  
  54. void renameFile() {
  55.     char oldName[255], newName[255];
  56.     cout << "Enter old name: ";
  57.     cin >> oldName;
  58.  
  59.     cout << "Enter new name: ";
  60.     cin >> newName;
  61.     if (rename(oldName, newName) != 0) {
  62.         cout << "Couldn't rename file " << endl;
  63.         return;
  64.     }
  65.     cout << "Ok! \n";
  66.     system("pause");
  67. }
  68.  
  69. void removeFile() {
  70.     char name[255];
  71.     cout << "Enter name of file: ";
  72.     cin >> name;
  73.     if (remove(name) != 0) {
  74.         cout << "Couldn't remove file! \n";
  75.         return;
  76.     }
  77.     cout << "Ok... " << endl;
  78.     system("pause");
  79. }
  80.  
  81. void Dir() {
  82.     char path[255];
  83.     cout << "Enter path: ";
  84.     cin >> path;
  85.  
  86.     char mask[25];
  87.     cout << "Enter mask like *.* or *.txt etc...: ";
  88.     cin >> mask;
  89.  
  90.     strcat(path, mask);
  91.  
  92.     _finddata_t *fileinfo = new _finddata_t;
  93.  
  94.     long done = _findfirst(path, fileinfo);
  95.     int count{};
  96.     int isFile = done;
  97.     while (isFile != -1) {
  98.         count++;
  99.         cout << fileinfo->name << "\n\n";
  100.         isFile = _findnext(done, fileinfo);
  101.     }
  102.     cout << "Was found " << count << " file's in " << path;
  103.     system("pause");
  104.     _findclose(done);
  105.     delete fileinfo;
  106. }
  107.  
  108.  
  109. void renameDirectory() {
  110.     char oldName[255], newName[255];
  111.     cout << "Enter old name: ";
  112.     cin >> oldName;
  113.  
  114.     cout << "Enter new name: ";
  115.     cin >> newName;
  116.     if (rename(oldName, newName) != 0) {
  117.         cout << "Couldn't rename file " << endl;
  118.         return;
  119.     }
  120.     cout << "Ok! \n";
  121.     cin.ignore();
  122.  
  123. }
  124.  
  125. void removeDirectory() {
  126.     char name[255];
  127.     cout << "Enter name of directory: ";
  128.     cin >> name;
  129.     if (remove(name) != 0) {
  130.         cout << "Couldn't remove directory! \n";
  131.         return;
  132.     }
  133.     cout << "Ok... " << endl;
  134.     cin.ignore();
  135. }
  136.  
  137. void createDirectory() {
  138.     char name[255];
  139.     cout << "Enter name: ";
  140.     cin >> name;
  141.     if (_mkdir(name, 0) == -1) {
  142.         cout << "Couldn't remove directory! \n";
  143.         return;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement