Skip to content

Commit ff0bec3

Browse files
committed
Add project files.
1 parent ad51f9d commit ff0bec3

14 files changed

+1358
-0
lines changed

CopyE.sln

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CopyE", "CopyE\CopyE.vcxproj", "{13940D5D-6C61-4654-AD21-787B165C5F1C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Debug|x64.ActiveCfg = Debug|x64
17+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Debug|x64.Build.0 = Debug|x64
18+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Debug|x86.ActiveCfg = Debug|Win32
19+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Debug|x86.Build.0 = Debug|Win32
20+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Release|x64.ActiveCfg = Release|x64
21+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Release|x64.Build.0 = Release|x64
22+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Release|x86.ActiveCfg = Release|Win32
23+
{13940D5D-6C61-4654-AD21-787B165C5F1C}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal

CopyE/Copy.cpp

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include "stdafx.h"
2+
#include "Copy.h"
3+
//////////////////////////////////////////////////////////////////////////
4+
CCopy::CCopy(fileVector& source):src(source),s(false), d(false)
5+
{
6+
}
7+
//////////////////////////////////////////////////////////////////////////
8+
CCopy::~CCopy()
9+
{
10+
}
11+
//////////////////////////////////////////////////////////////////////////
12+
//void CCopy::SetSource(fileVector& source)
13+
// {
14+
// src = source;
15+
// }
16+
//////////////////////////////////////////////////////////////////////////
17+
void CCopy::SetDestination(const fs::path& destination)
18+
{
19+
dst = destination;
20+
}
21+
22+
void CCopy::SetRecursive(bool _s, bool _d)
23+
{
24+
s = _s;
25+
d = _d;
26+
}
27+
//////////////////////////////////////////////////////////////////////////
28+
bool CCopy::CheckDir()
29+
{
30+
std::error_code e;
31+
bool ret = fs::exists(dst, e);
32+
PrintError(e);
33+
34+
if(ret == false)
35+
{
36+
std::wcout << _T("Error: Path \"") << dst << _T("\" doesn't exist.") << std::endl;
37+
return false;
38+
}
39+
return true;
40+
}
41+
//////////////////////////////////////////////////////////////////////////
42+
bool CCopy::CollectFiles()
43+
{
44+
if (CheckDir())
45+
{
46+
std::wcout << _T("Collect files from ") << dst << std::endl;
47+
GetAllFilesFromFolder(dst, d, dst_list, mask);
48+
std::wcout << dst_list.size() << _T(" files collected") << std::endl;
49+
return !dst_list.empty();
50+
}
51+
return false;
52+
}
53+
//////////////////////////////////////////////////////////////////////////
54+
bool CCopy::Process()
55+
{
56+
bool retCode = false;
57+
int count = 0;
58+
std::error_code e;
59+
60+
MakeMaskVector();
61+
62+
if (CollectFiles() == true)
63+
{
64+
for (auto i = src.begin(); i != src.end(); i++)
65+
{
66+
fileVector src_list;
67+
fs::path src_file(*i);
68+
if (!fs::is_regular_file(src_file, e))
69+
{
70+
PrintError(e);
71+
src_file = src_file.parent_path();
72+
if (src_file.empty() == true)
73+
src_file = fs::current_path(e);
74+
PrintError(e);
75+
GetAllFilesFromFolder(src_file, s, src_list,(*i).filename().generic_wstring());
76+
}
77+
else
78+
{
79+
src_list.push_back(*i);
80+
}
81+
PrintError(e);
82+
for(fileIterator k = src_list.begin(); k != src_list.end(); k++)
83+
{
84+
fileVector targetFiles;
85+
Find(*k, targetFiles);
86+
if (!targetFiles.empty())
87+
{
88+
for (fileIterator j = targetFiles.begin(); j != targetFiles.end(); j++)
89+
{
90+
std::wcout << _T("Copy ") << *k << " to " << *j << std::endl;
91+
92+
SetFileAttributes(j->c_str(),GetFileAttributes(j->c_str()) & ~FILE_ATTRIBUTE_READONLY);
93+
if (CopyFile(k->c_str(), j->c_str(), false) == false)
94+
{
95+
std::wcout << _T("Error: ") << GetLastErrorStr();
96+
continue;
97+
}
98+
count++;
99+
}
100+
retCode = true;
101+
}
102+
}
103+
}
104+
}
105+
106+
std::wcout << count << _T(" files copied") << std::endl;
107+
return retCode;
108+
}
109+
110+
//////////////////////////////////////////////////////////////////////////
111+
void CCopy::Find(const fs::path & s, fileVector & found)
112+
{
113+
for (auto i = dst_list.begin(); i != dst_list.end(); i++)
114+
{
115+
if ((*i).has_filename())
116+
{
117+
if(CheckFilesForCopy(s, *i))
118+
{
119+
found.push_back(*i);
120+
}
121+
}
122+
}
123+
}
124+
125+
bool CCopy::CheckFilesForCopy(const fs::path & s, const fs::path & d)
126+
{
127+
if (s.filename() != d.filename())
128+
return false;
129+
130+
std::error_code e;
131+
bool ret = fs::equivalent(s, d, e);
132+
PrintError(e);
133+
if(ret == true)
134+
{
135+
return false;
136+
}
137+
138+
// auto stime = fs::last_write_time(s,e);
139+
// PrintError(e);
140+
//
141+
// auto dtime = fs::last_write_time(d,e);
142+
// PrintError(e);
143+
//
144+
// if (stime < dtime)
145+
// {
146+
// return false;
147+
// }
148+
return true;
149+
}
150+
151+
void CCopy::MakeMaskVector()
152+
{
153+
mask.empty();
154+
155+
for (auto i = src.begin(); i != src.end(); i++)
156+
{
157+
mask.push_back((*i).filename());
158+
}
159+
}
160+

CopyE/Copy.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
//////////////////////////////////////////////////////////////////////////
3+
4+
#include <filesystem>
5+
namespace fs = std::experimental::filesystem;
6+
7+
class CCopy
8+
{
9+
protected:
10+
fileVector &src;
11+
fs::path dst;
12+
fileVector dst_list;
13+
stringVector mask;
14+
bool s;
15+
bool d;
16+
17+
bool CollectFiles();
18+
void Find(const fs::path &, fileVector &);
19+
bool CheckDir();
20+
bool CheckFilesForCopy(const fs::path &, const fs::path &);
21+
void MakeMaskVector();
22+
public:
23+
CCopy(fileVector& source);
24+
~CCopy();
25+
//void SetSource(fileVector& source);
26+
void SetDestination(const fs::path& destination);
27+
void SetRecursive(bool s, bool d);
28+
bool Process();
29+
30+
};
31+
//////////////////////////////////////////////////////////////////////////

CopyE/CopyE.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
#include "stdafx.h"
3+
#include "Copy.h"
4+
#include <vector>
5+
#include <boost/program_options.hpp>
6+
7+
namespace po = boost::program_options;
8+
using namespace std;
9+
10+
int wmain(int argc, TCHAR* argv[])
11+
{
12+
fileVector source;
13+
CCopy copy(source);
14+
try
15+
{
16+
po::options_description desc;
17+
desc.add_options()
18+
("help,h", "command line options")
19+
("source,s", po::wvalue<std::vector<wstring>>()->multitoken(), "source files/masks *.txt c:\\te??t.cpp")
20+
("destination,d", po::wvalue<wstring>(), "destination path c:\\temp")
21+
("recursive,r", po::wvalue<wstring>(), "recursive source|destination sd")
22+
;
23+
24+
po::variables_map vm;
25+
po::store(po::parse_command_line(argc, argv, desc), vm);
26+
27+
if (vm.count("help") || vm.empty())
28+
{
29+
cout << desc << "\n";
30+
return 0;
31+
}
32+
33+
34+
if (vm.count("source"))
35+
{
36+
for (int i = 0; i < vm["source"].as<std::vector<wstring>>().size(); i++)
37+
{
38+
source.push_back(vm["source"].as<std::vector<wstring>>()[i]);
39+
}
40+
}
41+
42+
if (source.empty())
43+
{
44+
cout << "source files/masks is not set.\n";
45+
return 1;
46+
}
47+
48+
if (!vm.count("destination"))
49+
{
50+
cout << "destination path is not set.\n";
51+
return 1;
52+
}
53+
54+
bool recursiveSource = false;
55+
bool recursiveDestination = false;
56+
57+
if (vm.count("recursive"))
58+
{
59+
if (vm["recursive"].as<wstring>().find(_T('s')) != wstring::npos)
60+
{
61+
recursiveSource = true;
62+
}
63+
if (vm["recursive"].as<wstring>().find(_T('d')) != wstring::npos)
64+
{
65+
recursiveDestination = true;
66+
}
67+
}
68+
69+
copy.SetDestination(vm["destination"].as<wstring>());
70+
copy.SetRecursive(recursiveSource, recursiveDestination);
71+
72+
copy.Process();
73+
}
74+
catch (std::exception & e)
75+
{
76+
std::cerr << "Error: " << e.what() << "\n";
77+
}
78+
return 0;
79+
}

0 commit comments

Comments
 (0)