Advertisement
Konark

Untitled

Apr 5th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <Windows.h>
  6. #include <string>
  7. #include <iomanip>
  8. #include <vector>
  9. #include <cmath>
  10. #include <istream>
  11. #include <ostream>
  12. #include <fstream>
  13.  
  14. using namespace std;
  15.  
  16. wstring del_dir(wstring str) {
  17.     string str_to(str.begin(), str.end());
  18.     int pos;
  19.     for (int j = str_to.length() - 1; j > -1; j--) {
  20.         if (str_to[j] == '\\')
  21.         {
  22.             pos = j;
  23.             break;
  24.         }
  25.     }
  26.     str_to.erase(pos, str_to.length() - pos);
  27.     return (wstring(str_to.begin(), str_to.end()));
  28. }
  29.  
  30. void cpy_file(string pathf, string patht, int len)
  31. {
  32.     ifstream inf(pathf, ios::binary);
  33.     ofstream outf(patht, ios::binary);
  34.     char * buffer = new char[len];
  35.     while (!inf.eof()) {
  36.         inf.read(buffer, len);
  37.         if (inf.gcount())
  38.             outf.write(buffer, inf.gcount());
  39.     }
  40.     inf.close();
  41.     outf.close();
  42. }
  43.  
  44. void copy_dir(wstring from, wstring to, long dirSize, int &dirCount, long copySize = 0) {
  45.     int i = 0;
  46.     WIN32_FIND_DATA FindFileData, FindFileData1, FindNextFileData;
  47.     HANDLE hFind;
  48.     HANDLE hFind1 = FindFirstFile(to.c_str(), &FindFileData1);
  49.     wstring path_to = to;
  50.     wstring path_from = from;
  51.     if (hFind1 == INVALID_HANDLE_VALUE)
  52.     {
  53.         printf("Ошибка (%d)\n", GetLastError());
  54.         return;
  55.     }
  56.     if ((FindFileData1.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
  57.         cout << "Второй параметр должен быть директорией!" << endl;
  58.         return;
  59.     }
  60.     hFind = FindFirstFile(from.c_str(), &FindFileData);
  61.     if (hFind == INVALID_HANDLE_VALUE)
  62.     {
  63.         printf("Ошибка (%d)\n", GetLastError());
  64.         return;
  65.     }
  66.     else
  67.     {
  68.         vector<HANDLE> handArr;
  69.         while (true) {
  70.             do {
  71.                 if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
  72.                     wstring bpath_from = path_from;
  73.                     if (i > 0)
  74.                         bpath_from = bpath_from + L"\\" + FindFileData.cFileName;
  75.                     string pathf(bpath_from.begin(), bpath_from.end());
  76.                     wstring buffto = path_to + L"\\" + FindFileData.cFileName;
  77.                     string patht(buffto.begin(), buffto.end());
  78.                     _tprintf(TEXT("Копируется с %s"), path_from.c_str());
  79.                     _tprintf(TEXT(" в %s\n"), buffto.c_str());
  80.                     copySize += FindFileData.nFileSizeLow;
  81.                     dirCount++;
  82.                     cpy_file(pathf, patht, 4096);
  83.                     double per = (copySize) / ((double)dirSize / 100);
  84.                     cout << "Скопировано " << dirCount << " файлов - " << per << " %" << endl;
  85.                 }
  86.                 else {
  87.                     if (i > 0)
  88.                         path_from = path_from + L"\\" + FindFileData.cFileName;
  89.                     path_to = path_to + L"\\" + FindFileData.cFileName;
  90.                     if (CreateDirectory((LPWSTR)path_to.c_str(), NULL))
  91.                         _tprintf(TEXT("Создана директория <%s>\n"), path_to.c_str());
  92.                     else
  93.                     {
  94.                         cout << "Невозможно создать директорию или она уже существует." << endl;
  95.                         return;
  96.                     }
  97.                     handArr.push_back(hFind);
  98.                     path_from = path_from + L"\\*";
  99.                     hFind = FindFirstFile(path_from.c_str(), &FindFileData);
  100.                     string chekStr(path_from.begin(), path_from.end());
  101.                     chekStr.erase(chekStr.length() - 2, 2);
  102.                     path_from = wstring(chekStr.begin(), chekStr.end());
  103.                     if (hFind == INVALID_HANDLE_VALUE)
  104.                     {
  105.                         printf("Ошибка (%d)\n", GetLastError());
  106.                         return;
  107.                     }
  108.                     FindNextFile(hFind, &FindFileData);
  109.                     i = 1;
  110.                 }
  111.             } while (FindNextFile(hFind, &FindFileData) != 0);
  112.             if (handArr.size() < 1)
  113.                 break;
  114.             while (true) {
  115.                 hFind = handArr.at(handArr.size() - 1);
  116.                 path_to = del_dir(path_to);
  117.                 path_from = del_dir(path_from);
  118.                 handArr.pop_back();
  119.                 if (FindNextFile(hFind, &FindFileData) != 0)
  120.                     break;
  121.                 if (handArr.size() < 1)
  122.                     return;
  123.             }
  124.         }
  125.         FindClose(hFind);
  126.     }
  127. }
  128. //-----------------
  129. long count_dir_size(wstring from, int &dirCount, long size = 0) {
  130.     int i = 0;
  131.     WIN32_FIND_DATA FindFileData, FindFileData1, FindNextFileData;
  132.     HANDLE hFind;
  133.     wstring path_from = from;
  134.     hFind = FindFirstFile(from.c_str(), &FindFileData);
  135.     if (hFind == INVALID_HANDLE_VALUE)
  136.     {
  137.         printf("Ошибка (%d)\n", GetLastError());
  138.         return -1;
  139.     }
  140.     else
  141.     {
  142.         vector<HANDLE> handArr;
  143.         while (true) {
  144.             do {
  145.                 if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
  146.                     size += FindFileData.nFileSizeLow;
  147.                     dirCount++;
  148.                 }
  149.                 else {
  150.                     if (i > 0)
  151.                         path_from = path_from + L"\\" + FindFileData.cFileName;
  152.                     handArr.push_back(hFind);
  153.                     path_from = path_from + L"\\*";
  154.                     hFind = FindFirstFile(path_from.c_str(), &FindFileData);
  155.                     string chekStr(path_from.begin(), path_from.end());
  156.                     chekStr.erase(chekStr.length() - 2, 2);
  157.                     path_from = wstring(chekStr.begin(), chekStr.end());
  158.                     if (hFind == INVALID_HANDLE_VALUE)
  159.                     {
  160.                         printf("Ошибка (%d)\n", GetLastError());
  161.                         return -1;
  162.                     }
  163.                     FindNextFile(hFind, &FindFileData);
  164.                     i = 1;
  165.                 }
  166.             } while (FindNextFile(hFind, &FindFileData) != 0);
  167.             if (handArr.size() < 1)
  168.                 break;
  169.             while (true) {
  170.                 hFind = handArr.at(handArr.size() - 1);
  171.                 path_from = del_dir(path_from);
  172.                 handArr.pop_back();
  173.                 if (FindNextFile(hFind, &FindFileData) != 0)
  174.                     break;
  175.                 if (handArr.size() < 1)
  176.                     return size;
  177.             }
  178.         }
  179.         return size;
  180.         FindClose(hFind);
  181.     }
  182. }
  183. //----------------
  184.  
  185. void dirCopy(wstring from, wstring to) {
  186.     int dirCount = 0;
  187.     cout << "Получение информации... " << endl;
  188.     long dirSize = count_dir_size(from, dirCount);
  189.     string strfrom(from.begin(), from.end());
  190.     cout << "Файлов в директирии <" << strfrom << "> " << dirCount << " общий рамер: " << dirSize << endl;
  191.     system("pause");
  192.     dirCount = 0;
  193.     copy_dir(from, to, dirSize, dirCount);
  194. }
  195.  
  196. int _tmain(int argc, _TCHAR* argv[])
  197. {
  198.     setlocale(LC_CTYPE, ".1251");
  199.     setlocale(LC_ALL, "Russian");
  200.     string str;
  201.     cout << "Введите путь к файлу или директории: ";
  202.     cin >> str;
  203.     wstring from(str.begin(), str.end());
  204.     cout << "Введите путь директоии для копирования: ";
  205.     cin >> str;
  206.     wstring to(str.begin(), str.end());
  207.     dirCopy(from, to);
  208.     system("pause");
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement