SDC C-Project CF Review 프로그램
LYW
2021-06-28 cf6a4019e9efbc0503bd3fbcf6f951565d028972
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#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;
}