Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <Windows.h>
- #include <string>
- #include <iomanip>
- #include <vector>
- #include <cmath>
- #include <istream>
- #include <ostream>
- #include <fstream>
- using namespace std;
- wstring del_dir(wstring str) {
- string str_to(str.begin(), str.end());
- int pos;
- for (int j = str_to.length() - 1; j > -1; j--) {
- if (str_to[j] == '\\')
- {
- pos = j;
- break;
- }
- }
- str_to.erase(pos, str_to.length() - pos);
- return (wstring(str_to.begin(), str_to.end()));
- }
- void cpy_file(string pathf, string patht, int len)
- {
- ifstream inf(pathf, ios::binary);
- ofstream outf(patht, ios::binary);
- char * buffer = new char[len];
- while (!inf.eof()) {
- inf.read(buffer, len);
- if (inf.gcount())
- outf.write(buffer, inf.gcount());
- }
- inf.close();
- outf.close();
- }
- void copy_dir(wstring from, wstring to, long dirSize, int &dirCount, long copySize = 0) {
- int i = 0;
- WIN32_FIND_DATA FindFileData, FindFileData1, FindNextFileData;
- HANDLE hFind;
- HANDLE hFind1 = FindFirstFile(to.c_str(), &FindFileData1);
- wstring path_to = to;
- wstring path_from = from;
- if (hFind1 == INVALID_HANDLE_VALUE)
- {
- printf("Ошибка (%d)\n", GetLastError());
- return;
- }
- if ((FindFileData1.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
- cout << "Второй параметр должен быть директорией!" << endl;
- return;
- }
- hFind = FindFirstFile(from.c_str(), &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
- {
- printf("Ошибка (%d)\n", GetLastError());
- return;
- }
- else
- {
- vector<HANDLE> handArr;
- while (true) {
- do {
- if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
- wstring bpath_from = path_from;
- if (i > 0)
- bpath_from = bpath_from + L"\\" + FindFileData.cFileName;
- string pathf(bpath_from.begin(), bpath_from.end());
- wstring buffto = path_to + L"\\" + FindFileData.cFileName;
- string patht(buffto.begin(), buffto.end());
- _tprintf(TEXT("Копируется с %s"), path_from.c_str());
- _tprintf(TEXT(" в %s\n"), buffto.c_str());
- copySize += FindFileData.nFileSizeLow;
- dirCount++;
- cpy_file(pathf, patht, 4096);
- double per = (copySize) / ((double)dirSize / 100);
- cout << "Скопировано " << dirCount << " файлов - " << per << " %" << endl;
- }
- else {
- if (i > 0)
- path_from = path_from + L"\\" + FindFileData.cFileName;
- path_to = path_to + L"\\" + FindFileData.cFileName;
- if (CreateDirectory((LPWSTR)path_to.c_str(), NULL))
- _tprintf(TEXT("Создана директория <%s>\n"), path_to.c_str());
- else
- {
- cout << "Невозможно создать директорию или она уже существует." << endl;
- return;
- }
- handArr.push_back(hFind);
- path_from = path_from + L"\\*";
- hFind = FindFirstFile(path_from.c_str(), &FindFileData);
- string chekStr(path_from.begin(), path_from.end());
- chekStr.erase(chekStr.length() - 2, 2);
- path_from = wstring(chekStr.begin(), chekStr.end());
- if (hFind == INVALID_HANDLE_VALUE)
- {
- printf("Ошибка (%d)\n", GetLastError());
- return;
- }
- FindNextFile(hFind, &FindFileData);
- i = 1;
- }
- } while (FindNextFile(hFind, &FindFileData) != 0);
- if (handArr.size() < 1)
- break;
- while (true) {
- hFind = handArr.at(handArr.size() - 1);
- path_to = del_dir(path_to);
- path_from = del_dir(path_from);
- handArr.pop_back();
- if (FindNextFile(hFind, &FindFileData) != 0)
- break;
- if (handArr.size() < 1)
- return;
- }
- }
- FindClose(hFind);
- }
- }
- //-----------------
- long count_dir_size(wstring from, int &dirCount, long size = 0) {
- int i = 0;
- WIN32_FIND_DATA FindFileData, FindFileData1, FindNextFileData;
- HANDLE hFind;
- wstring path_from = from;
- hFind = FindFirstFile(from.c_str(), &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
- {
- printf("Ошибка (%d)\n", GetLastError());
- return -1;
- }
- else
- {
- vector<HANDLE> handArr;
- while (true) {
- do {
- if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
- size += FindFileData.nFileSizeLow;
- dirCount++;
- }
- else {
- if (i > 0)
- path_from = path_from + L"\\" + FindFileData.cFileName;
- handArr.push_back(hFind);
- path_from = path_from + L"\\*";
- hFind = FindFirstFile(path_from.c_str(), &FindFileData);
- string chekStr(path_from.begin(), path_from.end());
- chekStr.erase(chekStr.length() - 2, 2);
- path_from = wstring(chekStr.begin(), chekStr.end());
- if (hFind == INVALID_HANDLE_VALUE)
- {
- printf("Ошибка (%d)\n", GetLastError());
- return -1;
- }
- FindNextFile(hFind, &FindFileData);
- i = 1;
- }
- } while (FindNextFile(hFind, &FindFileData) != 0);
- if (handArr.size() < 1)
- break;
- while (true) {
- hFind = handArr.at(handArr.size() - 1);
- path_from = del_dir(path_from);
- handArr.pop_back();
- if (FindNextFile(hFind, &FindFileData) != 0)
- break;
- if (handArr.size() < 1)
- return size;
- }
- }
- return size;
- FindClose(hFind);
- }
- }
- //----------------
- void dirCopy(wstring from, wstring to) {
- int dirCount = 0;
- cout << "Получение информации... " << endl;
- long dirSize = count_dir_size(from, dirCount);
- string strfrom(from.begin(), from.end());
- cout << "Файлов в директирии <" << strfrom << "> " << dirCount << " общий рамер: " << dirSize << endl;
- system("pause");
- dirCount = 0;
- copy_dir(from, to, dirSize, dirCount);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_CTYPE, ".1251");
- setlocale(LC_ALL, "Russian");
- string str;
- cout << "Введите путь к файлу или директории: ";
- cin >> str;
- wstring from(str.begin(), str.end());
- cout << "Введите путь директоии для копирования: ";
- cin >> str;
- wstring to(str.begin(), str.end());
- dirCopy(from, to);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement