Created
January 17, 2012 00:19
-
-
Save gradbot/1623769 to your computer and use it in GitHub Desktop.
Monitor a directory and call SyncToy if something changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This requires Windows XP or newer. | |
#include <stdio.h> | |
#include <tchar.h> | |
#include <windows.h> | |
unsigned int flags = FILE_NOTIFY_CHANGE_LAST_WRITE | | |
FILE_NOTIFY_CHANGE_SIZE | | |
FILE_NOTIFY_CHANGE_DIR_NAME | | |
FILE_NOTIFY_CHANGE_FILE_NAME; | |
void execute(LPCTSTR program, LPTSTR paramaters) | |
{ | |
STARTUPINFO s; | |
PROCESS_INFORMATION p; | |
ZeroMemory(&s, sizeof(s)); | |
s.cb = sizeof(s); | |
ZeroMemory(&p, sizeof(p)); | |
if (!CreateProcess(program, paramaters, NULL, NULL, true, 0, NULL, NULL, &s, &p)) | |
{ | |
printf("CreateProcess failed (%d)\n", GetLastError()); | |
} | |
WaitForSingleObject(p.hProcess, INFINITE); | |
CloseHandle(p.hProcess); | |
CloseHandle(p.hThread); | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
if (argc < 2) { | |
printf("Please include the file path to monitor."); | |
return 0; | |
} | |
LPCTSTR monitor = argv[1]; | |
bool watchSubTree = true; | |
LPCTSTR program = _T("C:\\Program Files\\SyncToy 2.1\\SyncToyCmd.exe"); | |
LPTSTR paramters = _T(" -R"); | |
HANDLE hNotify = FindFirstChangeNotification(monitor, watchSubTree, flags); | |
while (true) | |
{ | |
if (WaitForSingleObject(hNotify, INFINITE) == WAIT_OBJECT_0) | |
{ | |
printf("Change Detected.\n\n"); | |
execute(program, paramters); | |
printf("Done\n\n"); | |
FindNextChangeNotification(hNotify); | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Monitor a directory and call SyncToy if something changes. | |
#include <stdio.h> | |
#include <tchar.h> | |
#include <windows.h> | |
unsigned int flags = FILE_NOTIFY_CHANGE_LAST_WRITE | | |
FILE_NOTIFY_CHANGE_SIZE | | |
FILE_NOTIFY_CHANGE_DIR_NAME | | |
FILE_NOTIFY_CHANGE_FILE_NAME; | |
void execute(LPCTSTR program, LPTSTR paramaters) | |
{ | |
STARTUPINFO s; | |
PROCESS_INFORMATION p; | |
ZeroMemory(&s, sizeof(s)); | |
s.cb = sizeof(s); | |
ZeroMemory(&p, sizeof(p)); | |
//if (!CreateProcess(program, paramaters, NULL, NULL, true, 0, NULL, NULL, &s, &p)) | |
if (!CreateProcess(program, paramaters, NULL, NULL, true, CREATE_NO_WINDOW, NULL, NULL, &s, &p)) | |
{ | |
printf("CreateProcess failed (%d)\n", GetLastError()); | |
} | |
WaitForSingleObject(p.hProcess, INFINITE); | |
CloseHandle(p.hProcess); | |
CloseHandle(p.hThread); | |
} | |
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) | |
//int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
if (strlen(lpCmdLine) < 1) { | |
printf("Please include the file path to monitor."); | |
return 0; | |
} | |
WCHAR pWideString[1000]; | |
MultiByteToWideChar(CP_ACP, 0, lpCmdLine, -1, pWideString, 1000); | |
LPCTSTR monitor = pWideString; | |
bool watchSubTree = true; | |
LPCTSTR program = _T("C:\\Program Files\\SyncToy 2.1\\SyncToyCmd.exe"); | |
LPTSTR paramters = _T(" -R"); | |
HANDLE hNotify = FindFirstChangeNotification(monitor, watchSubTree, flags); | |
while (true) | |
{ | |
if (WaitForSingleObject(hNotify, INFINITE) == WAIT_OBJECT_0) | |
{ | |
printf("Change Detected.\n\n"); | |
execute(program, paramters); | |
printf("Done\n\n"); | |
FindNextChangeNotification(hNotify); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment