본문 바로가기
C++ 200제/코딩 IT 정보

CFileFind FindFile, GetFilePath 등 예제 소스, 윈CE 코드

by vicddory 2017. 4. 18.

CFileFind FindFile, GetFilePath 등 예제 소스, 윈CE 코드


소스 코드 다운로드



윈도우즈 CE에선 기본적으로 CFileFind를 제공하지 않고 있습니다. 그래서 별도로 해당 클래스를 구현해서 사용해야 합니다. 다행히, 어느 능력자 분이 직접 구현을 하셨네요.


윈CE 파일 찾기 CFileFind[윈CE 파일 찾기]


바로 아래는 CeFileFind.cpp와 헤더 파일을 압축한 것이고 그 아래는 전체 소스입니다. 이 소스 코드는 윈도우 XP 이상에서 지원하는 CFileFind API의 모든 기능을 포함하고 있습니다.

CFileFind 예제 소스 코드 헤더 header 파일


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
.h
 
#if !defined _CEFILEFIND_H_
#define _CEFILEFIND_H_
 
#include <afxwin.h>
 
class CCeFileFind : public CWnd
{
public:
    CCeFileFind( );
 
public:
    void Close();
    virtual BOOL FindNextFile( );
    virtual BOOL FindFile( LPCTSTR pstrName = NULL);
 
public:
    //Gets the length of the found file, in bytes. 
    DWORD    GetLength() const;
                
    //Gets the name, including the extension, of the found file 
    CString GetFileName() const;                
 
    //Gets the whole path of the found file. 
    CString GetFilePath() const;            
 
    //Gets the whole path of the found file. 
    CString GetRoot() const;            
 
    // to get the time the specified file was created
    virtual BOOL GetCreationTime( FILETIME* pFileTime ) const;
    virtual BOOL GetCreationTime( CTime& refTime ) const;
 
    //Gets the time that the file was last accessed. 
    virtual BOOL GetLastAccessTime( CTime& refTime ) const;
    virtual BOOL GetLastAccessTime( FILETIME* pFileTime ) const;
 
    //Gets the time the file was last changed and saved. 
    virtual BOOL GetLastWriteTime( FILETIME* pFileTime ) const;
    virtual BOOL GetLastWriteTime( CTime& refTime ) const;
 
    //Indicates the desired file attributes of the file to be found. 
    virtual BOOL MatchesMask( DWORD dwMask ) const;
    
    //CFileFind Determines if the name of the found file has the name "." or "..", 
    //indicating that is actually a directory. 
    virtual BOOL IsDots( ) const;
    
    //Determines if the found file is read-only. 
    BOOL IsReadOnly( ) const;
 
    //Determines if the found file is a directory. 
    BOOL IsDirectory( ) const;
                
    //Determines if the found file is compressed. 
    BOOL IsCompressed( ) const;
 
    //Determines if the found file is a system file. 
    BOOL IsSystem( ) const;
 
    //Determines if the found file is hidden. 
    BOOL IsHidden( ) const;
 
    //Determines if the found file is temporary. 
    BOOL IsTemporary( ) const;
 
    //Determines if the found file is normal (in other words, has no other attributes). 
    BOOL IsNormal( ) const;
 
    //Determines if the found file is archived. 
    BOOL IsArchived( ) const;
 
public:
    virtual ~CCeFileFind();
 
private:
 
    LPWIN32_FIND_DATA m_pfiledata;
    LPWIN32_FIND_DATA m_pNextdata;
    CString    m_csRoot;
    HANDLE m_hFileHandle;
 
    char    m_chDirSeparator;
 
    void AssertDoneNext() const;
};
 
#endif // !defined _CEFILEFIND_H_
cs

CFileFind 예제 소스 코드 CPP 파일


FindFile, GetFilePath CFileFind[윈CE 파일 찾기]


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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
.cpp
 
#include "stdafx.h"
#include "CeFileFind.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
#define DELETE_POINTER(ptr)        if( ptr != NULL )    \
                                {                    \
                                    delete ptr;        \
                                    ptr = NULL;        \
                                }
 
#define DIR_SEPERATOR        '\\'
 
CCeFileFind::CCeFileFind()
            :m_hFileHandle(NULL), // initialize to NULL
            m_pfiledata(NULL) { }
 
CCeFileFind::~CCeFileFind() { Close(); }
 
BOOL CCeFileFind::FindFile(LPCTSTR pstrName)
{
    Close();
 
    // if NULL , wild card search 
    ifNULL == pstrName )
    {
        m_csRoot = DIR_SEPERATOR;
        pstrName = _T("\\*.*");
        
    }
    else
    {
        m_csRoot = pstrName;
        int nPos = m_csRoot.ReverseFind( '\\' );
 
        if( nPos == 0 )
            m_csRoot = '\\';
        else
            m_csRoot = m_csRoot.Left( nPos );
    
    }
 
    m_pNextdata = new WIN32_FIND_DATA;
    // search for file
    m_hFileHandle = FindFirstFile( pstrName, m_pNextdata );
    
    if(m_hFileHandle == INVALID_HANDLE_VALUE)
    {
        Close();
        return FALSE;
    }
 
    // file was found
    return TRUE;
}
 
BOOL CCeFileFind::FindNextFile()
{
    ASSERT(m_hFileHandle != NULL);
 
    if (m_hFileHandle == NULL)
        return FALSE;
 
    if (m_pfiledata == NULL)
        m_pfiledata = new WIN32_FIND_DATA;
 
    AssertDoneNext();
 
    LPWIN32_FIND_DATA pTemp = m_pfiledata;
    m_pfiledata = m_pNextdata;
    m_pNextdata = pTemp;
 
    return ::FindNextFile(m_hFileHandle, m_pNextdata);
}
 
void CCeFileFind::Close()
{
//     CFileFind DELETE_POINTER( m_pfiledata );
//     DELETE_POINTER( m_pNextdata );
 
    if( m_hFileHandle!= NULL && m_hFileHandle != INVALID_HANDLE_VALUE )
    {
        ::FindClose( m_hFileHandle );
        m_hFileHandle = NULL;
    }
}
 
BOOL CCeFileFind::MatchesMask(DWORD dwMask) const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if ( m_pfiledata != NULL)    return (!!(m_pfiledata->dwFileAttributes & dwMask) );
    else                        return FALSE;
}
 
 
CString CCeFileFind::GetRoot() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)    return m_csRoot;
    else                        return _T("");
 
}
 
BOOL CCeFileFind::GetLastAccessTime(FILETIME* pTimeStamp) const
{
    ASSERT(m_hFileHandle != NULL);
    ASSERT(pTimeStamp != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL && pTimeStamp != NULL)
    {
        *pTimeStamp = m_pfiledata -> ftLastAccessTime;
        return TRUE;
    }
    else
        return FALSE;
}
 
BOOL CCeFileFind::GetLastWriteTime(FILETIME* pTimeStamp) const
{
    ASSERT(m_hFileHandle != NULL);
    ASSERT(pTimeStamp != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL && pTimeStamp != NULL)
    {
        *pTimeStamp = m_pfiledata -> ftLastWriteTime;
        return TRUE;
    }
    else
        return FALSE;
}
 
BOOL CCeFileFind::GetCreationTime(FILETIME* pTimeStamp) const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL && pTimeStamp != NULL)
    {
        *pTimeStamp = m_pfiledata -> ftCreationTime;
        return TRUE;
    }
    else
        return FALSE;
}
 
BOOL CCeFileFind::GetLastAccessTime(CTime& refTime) const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        refTime = CTime( m_pfiledata -> ftLastAccessTime );
        return TRUE;
    }
    else
        return FALSE;
}
 
BOOL CCeFileFind::GetLastWriteTime(CTime& refTime) const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        refTime = CTime( m_pfiledata -> ftLastWriteTime );
        return TRUE;
    }
    else
        return FALSE;
}
 
BOOL CCeFileFind::GetCreationTime(CTime& refTime) const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        refTime = CTime( m_pfiledata -> ftCreationTime );
        return TRUE;
    }
    else
        return FALSE;
}
 
BOOL CCeFileFind::IsDots() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    // CFileFind - return TRUE if the file name is "." or ".." and
    // the file is a directory
    BOOL bResult = FALSE;
    if (m_pfiledata != NULL && IsDirectory())
    {
        LPWIN32_FIND_DATA pFindData = m_pfiledata;
        if (pFindData->cFileName[0== '.')
        {
            if (pFindData->cFileName[1== '\0' ||
                (pFindData->cFileName[1== '.' &&
                 pFindData->cFileName[2== '\0'))
            {
                bResult = TRUE;
            }
        }
    }
 
    return bResult;
}
 
BOOL CCeFileFind::IsArchived( ) const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
 
}
 
BOOL CCeFileFind::IsCompressed() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes == FILE_ATTRIBUTE_COMPRESSED )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
 
BOOL CCeFileFind::IsDirectory() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
BOOL CCeFileFind::IsHidden() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
 
BOOL CCeFileFind::IsNormal() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_NORMAL )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
BOOL CCeFileFind::IsReadOnly() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_READONLY )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
BOOL CCeFileFind::IsSystem() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_SYSTEM )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
 
BOOL CCeFileFind::IsTemporary() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
    {
        if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY )
            return TRUE;
        else
            return FALSE;
    }
    return FALSE;
}
 
 
CString CCeFileFind::GetFilePath() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    CString csResult = m_csRoot;
 
    if (csResult[csResult.GetLength()-1!= DIR_SEPERATOR )
        csResult += DIR_SEPERATOR;
    csResult += GetFileName();
    return csResult;
}
 
 
CString CCeFileFind::GetFileName() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    CString ret;
 
    if (m_pfiledata != NULL)
        ret = m_pfiledata->cFileName;
    return ret;
}
 
DWORD CCeFileFind::GetLength() const
{
    ASSERT(m_hFileHandle != NULL);
    AssertDoneNext();
 
    if (m_pfiledata != NULL)
        return m_pfiledata -> nFileSizeLow;
    else
        return 0;
}
 
 
void CCeFileFind::AssertDoneNext() const
{
    // CFileFind if you trip the ASSERT in the else side, you've called
    // a Get() function without having done at least one
    // FindNextFile() call
 
    if (m_hFileHandle == NULL)
        ASSERT( m_pfiledata == NULL && m_pNextdata == NULL);
    else
        ASSERT( m_pfiledata != NULL && m_pNextdata != NULL);
}
cs


CFileFind FindFile, GetFilePath 등 예제 소스, 윈CE 코드

댓글