#include "StdAfx.h"
|
#include "FolderMonitoring.h"
|
|
CFolderMonitoring::CFolderMonitoring( DWORD dwPeriod/*=100*/ ) : CTimerThreadPools(dwPeriod, 1)
|
{
|
|
}
|
|
CFolderMonitoring::~CFolderMonitoring(void)
|
{
|
|
}
|
|
void CFolderMonitoring::SetMonitorParam( const SFolderMonitorParam& monitorParam )
|
{
|
CSingleLock localLock(&m_csMonioring);
|
localLock.Lock();
|
|
m_sMonitorParam = monitorParam;
|
m_vecMonitorResult.clear();
|
}
|
|
BOOL CFolderMonitoring::StartMonitoring()
|
{
|
return CreateTimerThread(this);
|
}
|
|
void CFolderMonitoring::StopMonitoring()
|
{
|
CTimerThreadPools::StopThread();
|
}
|
|
void CFolderMonitoring::TimerThreadProcess( PVOID pParameter )
|
{
|
CSingleLock localLock(&m_csMonioring);
|
localLock.Lock();
|
|
if (m_sMonitorParam.strMonitorFolderPath.GetLength()==0) return;
|
if (m_sMonitorParam.strMonitorFileExt.GetLength()==0) return;
|
if (m_sMonitorParam.strProcessFolderPath.GetLength()==0) return;
|
|
VecString vecFilePath;
|
|
if (MainProcessing(vecFilePath, m_sMonitorParam)==FALSE)
|
{
|
return;
|
}
|
|
if (PostProcessing(vecFilePath, m_sMonitorParam)==FALSE)
|
{
|
return;
|
}
|
|
return;
|
}
|
|
|
|
BOOL CFolderMonitoring::MainProcessing(VecString& vecFilePath, const SFolderMonitorParam& sMonitorParam)
|
{
|
CString strFindFile = sMonitorParam.strMonitorFolderPath + _T("\\*.") + sMonitorParam.strMonitorFileExt;
|
|
// step1. ã±â ¼öÇà.
|
CFileFind finder;
|
BOOL bFind = finder.FindFile(strFindFile);
|
if (bFind==FALSE) return FALSE;
|
|
// step2. ãÀº ÆÄÀϰæ·Î¸í ÀúÀå
|
CString strResult = _T("");
|
while(bFind)
|
{
|
bFind = finder.FindNextFile();
|
if (finder.IsDots()) continue;
|
if (finder.IsDirectory()) continue;
|
|
strResult = finder.GetFileName();
|
|
// step3. Áö¿ìÁö ¸»¾Æ¾ßÇϸé ó¸®Çß´ÂÁö¸¦ Á¶È¸Çغ¸°í
|
if (sMonitorParam.nProcessType==MonitorProcessType_Copy)
|
{
|
if (IsProcessedFile(strResult)==FALSE)
|
{
|
vecFilePath.push_back(strResult);
|
}
|
}
|
else
|
{
|
vecFilePath.push_back(strResult);
|
}
|
};
|
finder.Close();
|
|
return (BOOL) vecFilePath.size();
|
}
|
|
BOOL CFolderMonitoring::PostProcessing(const VecString& vecFilePath, const SFolderMonitorParam& sMonitorParam)
|
{
|
for (constVecStringIt it=vecFilePath.begin(); it!=vecFilePath.end(); it++)
|
{
|
CString strExistFileName = sMonitorParam.strMonitorFolderPath + _T("\\") + *it;
|
|
// 1. ÆÄÀÏ Å©±â°¡ º¯ÇÏ´ÂÁö È®ÀÎ
|
BOOL bFind = FALSE;
|
ULONGLONG nStartLength = -1;
|
ULONGLONG nCurLength = 0;
|
do
|
{
|
CFileFind finder;
|
BOOL bFind = finder.FindFile(strExistFileName);
|
bFind = finder.FindNextFile();
|
nCurLength = finder.GetLength();
|
if ( (nStartLength!=-1) && ((nCurLength-nStartLength)==0) )
|
{
|
break;
|
}
|
nStartLength = nCurLength;
|
::Sleep(50);
|
} while (TRUE);
|
|
// 2. ÆÄÀÏÀ» ÈÄó¸® ÇÔ.
|
CString strNewFileName = sMonitorParam.strProcessFolderPath + _T("\\") + *it;
|
|
switch(sMonitorParam.nProcessType)
|
{
|
case MonitorProcessType_Copy: // copy
|
if (CopyFile(strExistFileName, strNewFileName, FALSE))
|
{
|
// º¹»ç½Ã¿¡´Â °á°ú ¹éÅÍ¿¡ ÆÄÀÏÀ̸§ ÀúÀåÇÔ.
|
SFolderMonitorResult result;
|
result.strProcessFileName = *it;
|
m_vecMonitorResult.push_back(result);
|
}
|
break;
|
|
case MonitorProcessType_Move: // move
|
if (MoveFile(strExistFileName, strNewFileName))
|
{
|
|
}
|
break;
|
}
|
|
::Sleep(10);
|
}
|
|
return TRUE;
|
}
|
|
|
BOOL CFolderMonitoring::IsProcessedFile(const CString& strFilename)
|
{
|
for (constVecFolderMonitorResultIt it=m_vecMonitorResult.begin(); it!=m_vecMonitorResult.end(); it++)
|
{
|
if (it->strProcessFileName.CompareNoCase(strFilename)==0)
|
{
|
return TRUE;
|
}
|
}
|
return FALSE;
|
}
|