1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <shlwapi.h>
#include <iostream>
#include <string>
#pragma comment(lib, "shlwapi.lib")
using namespace std;
#define LETS_PRETEND
inline bool IsDots(const char* path)
{
return ((0 == strcmp(path, ".")) || (0 == strcmp(path, "..")));
}
void synch(const string sTarget, const string sDest)
{
// using Path API PathIsDirectory rather than custom function
#ifndef LETS_PRETEND
if (PathIsDirectory(sDest.c_str()) && PathIsDirectory(sTarget.c_str()))
#endif
{
WIN32_FIND_DATA search_data;
// now using wildcard
HANDLE handle = FindFirstFile((sTarget + "\\*.*").c_str() , &search_data);
if (handle != INVALID_HANDLE_VALUE)
{
do
{
//if (GetFileAttributes(search_data.cFileName) == FILE_ATTRIBUTE_DIRECTORY)
if (search_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// guard against processing this folder again, or the parent
if( !IsDots(search_data.cFileName) )
{
#ifdef LETS_PRETEND
std::cout << "CreateDirectory(" << (sDest + "\\" + search_data.cFileName) << ", NULL);" << std::endl;
#else
CreateDirectory((sDest + "\\" + search_data.cFileName).c_str(), NULL);
#endif
// to be consistent with other calls, add sTarger + "\\" prefix
//synch(search_data.cFileName, sDest + "\\" + search_data.cFileName);
synch(sTarget + "\\" + search_data.cFileName, sDest + "\\" + search_data.cFileName);
}
}
else
{
#ifdef LETS_PRETEND
std::cout << "CopyFile(" << (sTarget + "\\" + search_data.cFileName) << ", " << (sDest + "\\" + search_data.cFileName) << ", FALSE))" << endl;
#else
if (!CopyFile((sTarget + "\\" + search_data.cFileName).c_str(), (sDest + "\\" + search_data.cFileName).c_str(), FALSE))
{
MessageBox(NULL, ("ERROR backing up \"" + (string)search_data.cFileName + "\"").c_str(), "MySynch Message", MB_OK | MB_ICONERROR);
}
#endif
}
} while (FindNextFile(handle, &search_data));
FindClose(handle);
}
}
#ifndef LETS_PRETEND
else
{
MessageBox(NULL, ("ERROR finding directory \"" + sTarget + "\" or \"" + sDest + "\"").c_str(), "MySynch Message", MB_OK | MB_ICONERROR);
}
#endif
}
int main()
{
synch("C:\\test", "C:\\temp");
return 0;
}
|