Advertisement
KAYOver

Untitled

Sep 10th, 2021
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. char text[255] = "",
  7.      type[255] = "",
  8.      path[255] = "E:\\command.txt",
  9.      pathFirst[255] = "",
  10.      pathSecond[255] = "";
  11.  
  12. void read() {
  13.     HANDLE file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  14.    
  15.     if(file == INVALID_HANDLE_VALUE) {
  16.         cout << "Failed to read command.";
  17.         exit(-1);
  18.     }
  19.  
  20.     DWORD bytesCount = 0;
  21.  
  22.     ReadFile(file, text, GetFileSize(file, NULL), &bytesCount, NULL);
  23.     CloseHandle(file);
  24. }
  25.  
  26. void parseText() {
  27.     char* nextToken = NULL;
  28.     char* part = strtok_s((char*)text, " ", &nextToken);
  29.  
  30.     int i = 1;
  31.     while(part != NULL && i < 4) {
  32.         switch(i) {
  33.             case 1: strcpy_s(type, part);       break;
  34.             case 2: strcpy_s(pathFirst, part);  break;
  35.             case 3: strcpy_s(pathSecond, part); break;
  36.         }
  37.        
  38.         part = strtok_s(NULL, " ", &nextToken);
  39.  
  40.         i++;
  41.     }
  42. }
  43.  
  44. void write() {
  45.     if(pathFirst == "" || pathSecond == "") {
  46.         cout << "Unable to use empty paths." << endl;
  47.         exit(-1);
  48.     }
  49.  
  50.     try {
  51.         if(!strcmp(type, "copy")) {
  52.             CopyFile(pathFirst, pathSecond, true);
  53.             cout << "Copied.";
  54.         }
  55.         if(!strcmp(type, "move")) {
  56.             MoveFile(pathFirst, pathSecond);
  57.             cout << "Moved.";
  58.         }
  59.     } catch(int error) {
  60.         cout << "Error while launching command: " << error << endl;
  61.     }
  62. }
  63.  
  64. int main()
  65. {
  66.     cout << "       Command path: " << path << endl;
  67.  
  68.     read();
  69.  
  70.     cout << "       Command text: " << text << endl;
  71.  
  72.     parseText();
  73.  
  74.     cout << "       Command type: " << type <<
  75.     endl << " Command first path: " << pathFirst <<
  76.     endl << "Command second path: " << pathSecond << endl;
  77.  
  78.     write();
  79.     exit(0);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement