-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDirectory.cpp
518 lines (434 loc) · 12.3 KB
/
CDirectory.cpp
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "stdafx.h"
#include "CDirectory.h"
CDirectory::CDirectory()
{
}
CDirectory::~CDirectory()
{
}
TCHAR *CDirectory::GetCurrentDirectory(TCHAR *pszPath, ULONG ulSize)
{
memset(pszPath, 0, sizeof(TCHAR) * ulSize);
TCHAR szBuf[MAX_PATH] = {0};
GetModuleFileName(NULL,szBuf,sizeof(szBuf)/sizeof(TCHAR));
int ulCount = _tcslen(szBuf);
while(--ulCount >= 0)
{
if(szBuf[ulCount] == TEXT('//'))
{
break;
}
else
{
continue;
}
}
if(ulSize > (DWORD)ulCount)
{
_tcsncpy(pszPath,szBuf,(ulCount + 1));
}
return pszPath;
}
void CDirectory::CreateDirectorySerial(const TSTRING &strPath)
{
TSTRING SourcePath = strPath;
if(strPath.size() >= MAX_PATH)
return;
if(SourcePath[SourcePath.size() -1 ] == TEXT('//'))
{
SourcePath[SourcePath.size() -1 ] = TEXT('/0');
}
if(CheckDirectoryExist(strPath))
return;
TSTRING::size_type idx = SourcePath.find(TEXT('//'));
while(idx !=TSTRING::npos)
{
TSTRING strCreateDirPath;
strCreateDirPath.assign(SourcePath,0,idx);
if(!CheckDirectoryExist(strCreateDirPath))
{
CreateDirectory(strCreateDirPath.c_str(),NULL);
}
idx = SourcePath.find(TEXT('//'),idx + 1);
}
CreateDirectory(SourcePath.c_str(),NULL);
}
void CDirectory::DeleteDirectory(const TSTRING &strPath)
{
TSTRING strDirPath = strPath;
#ifdef _WIN32_WCE
if(strDirPath[0] != TEXT('//'))
{
return;
}
#endif
if(strDirPath[strDirPath.size() - 1] == TEXT('//'))
{
strDirPath += TEXT("*.*");
}
else
{
strDirPath += TEXT("//*.*");
}
WIN32_FIND_DATA fd;
HANDLE hdFind;
hdFind = FindFirstFile(strDirPath.c_str(),&fd);
if(hdFind != INVALID_HANDLE_VALUE)
{
do{
//At winXP system the "." means current directory, the ".."means parent directory.
if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
{
//It is directory
TSTRING strNextDir = strPath;
if(strNextDir[strNextDir.size() -1] != TEXT('//'))
strNextDir += TEXT("//");
strNextDir += fd.cFileName;
DeleteDirectory(strNextDir);
RemoveDirectory(strNextDir.c_str());
}
else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
//It is file
TSTRING strPathFile = strPath;
if(strPathFile[strPathFile.size() - 1] !=TEXT( '//'))
strPathFile += TEXT("//");
strPathFile += fd.cFileName;
DeleteFile(strPathFile.c_str());
}
}while(FindNextFile(hdFind,&fd));
}
FindClose(hdFind);
RemoveDirectory(strPath.c_str());
}
int CDirectory::FindString(const TCHAR *szSource, const TCHAR *szFind, const int iBeginPos)
{
int iLenSource = _tcslen(szSource);
int iLenFind = _tcslen(szFind);
if(iLenSource - 1 < iBeginPos)
{
return -1;
}
int iCount = 0;
int iFindCount = 0;
BOOL bPair = FALSE;
for(iCount = 0; iCount < iLenSource - iBeginPos; iCount++)
{
if(szSource[iCount + iBeginPos] == szFind[iFindCount])
{
if(iFindCount == iLenFind - 1)
{
bPair = TRUE;
break;
}
iFindCount++;
}
else
{
iFindCount = 0;
}
}
int iFindPos ;
if(bPair == FALSE)
{
iFindPos = -1;
}
else
{
iFindPos = iCount + iBeginPos - iLenFind + 1;
}
return iFindPos;
}
DWORD CDirectory::GetDirectorySize(const TSTRING &strPath)
{
DWORD dSize = 0;
//查找路径下的文件夹和所需文件
TSTRING strDirPath = strPath;
#ifdef _WIN32_WCE
if(strDirPath[0] != TEXT('//'))
{
return FALSE;
}
#endif
if(strDirPath[strDirPath.size() - 1] == TEXT('//'))
{
strDirPath += TEXT("*.*");
}
else
{
strDirPath += TEXT("//*.*");
}
WIN32_FIND_DATA fd;
HANDLE hdFind;
hdFind = FindFirstFile(strDirPath.c_str(),&fd);
if(hdFind != INVALID_HANDLE_VALUE)
{
do{
//At winXP system the "." means current directory, the ".."means parent directory.
if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
{
//It is directory
TSTRING strNextDir = strPath;
if(strNextDir[strNextDir.size() -1] != TEXT('//'))
strNextDir += TEXT("//");
strNextDir += fd.cFileName;
DWORD dDirectorySize = GetDirectorySize(strNextDir);
dSize += dDirectorySize;
}
else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
//It is file
TSTRING strPathFile = strPath;
if(strPathFile[strPathFile.size() - 1] != TEXT('//'))
strPathFile += TEXT("//");
strPathFile += fd.cFileName;
HANDLE hFile = CreateFile(strPathFile.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
dSize += ::GetFileSize(hFile,NULL);
CloseHandle(hFile);
}
}while(FindNextFile(hdFind,&fd));
}
FindClose(hdFind);
return dSize;
}
BOOL CDirectory::CheckDirectoryExist(const TSTRING &strPath)
{
BOOL bReturn = FALSE;
if(strPath.size() >= MAX_PATH)
{
return FALSE;
}
WIN32_FIND_DATA fd;
HANDLE hdFind = FindFirstFile(strPath.c_str(),&fd);
if(hdFind != INVALID_HANDLE_VALUE)
{
if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
bReturn = TRUE;
}
}
FindClose(hdFind);
return bReturn;
}
BOOL CDirectory::CopyDirectory(const TSTRING &strDestinationPath,const TSTRING &strSourcePath)
{
TSTRING strDirPath = strSourcePath;
BOOL bResult = TRUE;
#ifdef _WIN32_WCE
if(strDirPath[0] != '//')
{
return FALSE;
}
#endif
if(strDirPath[strDirPath.size() - 1] == '//')
{
strDirPath += TEXT("*.*");
}
else
{
strDirPath += TEXT("//*.*");
}
WIN32_FIND_DATA fd;
HANDLE hdFind;
hdFind = FindFirstFile(strDirPath.c_str(),&fd);
if(hdFind != INVALID_HANDLE_VALUE)
{
do{
if(!CheckDirectoryExist(strDestinationPath))
{
CreateDirectory(strDestinationPath.c_str(),NULL);
}
//At winXP system the "." means current directory, the ".."means parent directory.
if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
{
//It is directory
TSTRING strSourceNextDir = strSourcePath;
TSTRING strDestinationNextDir = strDestinationPath;
if(strSourceNextDir[strSourceNextDir.size() -1] != TEXT('//'))
{
strSourceNextDir += TEXT("//");
}
if(strDestinationNextDir[strDestinationNextDir.size() -1] != TEXT('//'))
{
strDestinationNextDir += TEXT("//");
}
strSourceNextDir += fd.cFileName;
strDestinationNextDir += fd.cFileName;
CreateDirectory(strDestinationNextDir.c_str(),NULL);
CopyDirectory(strDestinationNextDir,strSourceNextDir);
}
else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
//It is file
TSTRING strSourceFilePath = strSourcePath;
TSTRING strDestinationFilePath = strDestinationPath;
if(strSourceFilePath[strSourceFilePath.size() - 1] != TEXT('//'))
{
strSourceFilePath += TEXT("//");
}
if(strDestinationFilePath[strDestinationFilePath.size() - 1] != TEXT('//'))
{
strDestinationFilePath += TEXT("//");
}
strSourceFilePath += fd.cFileName;
strDestinationFilePath += fd.cFileName;
//The file is read-only ,cancle the attributes of read-only
if(GetFileAttributes(strSourceFilePath.c_str()) & FILE_ATTRIBUTE_READONLY)
SetFileAttributes(strSourceFilePath.c_str(), FILE_ATTRIBUTE_NORMAL);
if(GetFileAttributes(strDestinationFilePath.c_str()) &FILE_ATTRIBUTE_READONLY)
SetFileAttributes(strDestinationFilePath.c_str(), FILE_ATTRIBUTE_NORMAL);
if(CopyFile(strSourceFilePath.c_str(),strDestinationFilePath.c_str(),FALSE)==FALSE)
{
#ifdef _WIN32_WCE
swprintf(TEXT("Copy File %s Failed ! Error Code : 0x%x/r/n"),strSourceFilePath.c_str(),GetLastError());
ASSERT(FALSE);
#else
TSTRING strInfo;
strInfo = TEXT("Copy File/ ");
strInfo += strSourceFilePath;
strInfo += TEXT("/ Failed !");
OutputDebugString(strInfo.c_str());
#endif
return FALSE;
}
}
}while(FindNextFile(hdFind,&fd));
}
else
{
bResult = FALSE;
}
FindClose(hdFind);
return bResult;
}
BOOL CDirectory::FindFileFromDirectory(const TSTRING &DirectoryPath,const TSTRING &SuffixName,std::vector<TSTRING> &vFilePathList)
{
if(DirectoryPath.size() >= MAX_PATH)
{
return FALSE;
}
TSTRING FindDirPath = DirectoryPath;
if(FindDirPath[FindDirPath.size() - 1] == TEXT('//'))
{
FindDirPath += TEXT("*.*");
}
else
{
FindDirPath += TEXT("//*.*");
}
WIN32_FIND_DATA fd;
HANDLE hdFind;
hdFind = FindFirstFile(FindDirPath.c_str(),&fd);
if(hdFind != INVALID_HANDLE_VALUE)
{
do{
//At winXP system the "." means current directory, the ".."means parent directory.
if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
{
//it must be directory
TSTRING strFindNextDir = FindDirPath;
if(strFindNextDir[strFindNextDir.size() -1] != TEXT('//'))
strFindNextDir += TEXT("//");
strFindNextDir += fd.cFileName;
FindFileFromDirectory(strFindNextDir,SuffixName,vFilePathList);
}
else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
//it is file
TSTRING strFindPathFile = DirectoryPath;
if(strFindPathFile[strFindPathFile.size() - 1] != TEXT('//'))
strFindPathFile += TEXT("//");
strFindPathFile += fd.cFileName;
if(CheckFileSuffix(fd.cFileName,SuffixName) == TRUE)
{
vFilePathList.push_back(strFindPathFile);
}
}
}while(FindNextFile(hdFind,&fd));
}
FindClose(hdFind);
return TRUE;
}
BOOL CDirectory:: CheckFileSuffix(const TSTRING &FileName,const TSTRING &SuffixName)
{
if(SuffixName.empty())
{
return FALSE;
}
if(FileName.empty())
{
return FALSE;
}
if(SuffixName[0] != TEXT('*'))
{
return (FileName.compare(SuffixName) == 0) ? TRUE : FALSE;
}
else
{
if(FileName.rfind(&SuffixName[1]) != TSTRING::npos)
return TRUE;
}
return FALSE;
}
#ifndef _WIN32_WCE
void CDirectory::GetDiskPartition(std::multimap<FileDeviceType,std::wstring> &mDiskParition)
{
HANDLE hDevice;
std::wstring strBaseDisk = TEXT("////.//");
TCHAR BaseNumber = TEXT('A');
//The Disk partition from'A' to 'Z'
for(int i = 0; i<26;i++)
{
std::wstring strDiskNumber;
strDiskNumber += (BaseNumber + i);
strDiskNumber += TEXT(':');
std::wstring strDiskName = strBaseDisk;
strDiskName +=strDiskNumber;
hDevice = CreateFile(strDiskName.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
if(hDevice != INVALID_HANDLE_VALUE)
{
strDiskNumber += TEXT("//");
if(DRIVE_CDROM == GetDriveType(strDiskNumber.c_str()))
{
mDiskParition.insert(make_pair(CDROM,strDiskNumber));
}else if(DRIVE_REMOVABLE == GetDriveType(strDiskNumber.c_str()))
{
mDiskParition.insert(make_pair(DISK_REMOVABLE,strDiskNumber));
}
else if(DRIVE_FIXED == GetDriveType(strDiskNumber.c_str()))
{
mDiskParition.insert(make_pair(DISK_FIXED,strDiskNumber));
}
}
CloseHandle(hDevice);
}
}
#endif
// CDirector DirOperator;
// //根据绝对路径创建文件夹,如果绝对路径名有不存在的目录将被创建。如在C盘下有个Dir的文件//夹,但是在Dir子目录文件夹中没有A文件夹通过下面的调用:
// DirOperator. CreateDirectorySerial(TEXT("c://Dir//A//B"));
// //将会为Dir创建一个A的子目录同时也会为为A创建一个B的子目录。
// //获取指定文件夹的大小,将返回B文件夹的大小
// DirOperator. GetDirectorySize(TEXT("c://Dir//A//B"));
// //检查目录是否存在,如果B目录存在将返回TRUE,否则返回FALSE
// if(CheckDirectoryExist(TEXT("c://Dir//A//B")))
// {
// RETAILMSG(TRUE,(TEXT("Directory exist!/r/n")));
// }
// else
// {
// RETAILMSG(TRUE,(TEXT("Directory does not exist!/r/n")));
// }
// //拷贝文件夹,将C盘下的B目录下的所有文件拷贝到D下的C文件夹下,如果目标路径中有不存在//的文件夹将被创建
// DirOperator. CopyDirectory (TEXT("D://Dir//A//C"),TEXT("c://Dir//A//B")):
//
// //在文件夹中查找文件, vFilePathList中存储了B文件夹下所有File.exe路径名
// std::vector<TSTRING> vFilePathList
// DirOperator.FindFileFromDirectory(TEXT("c://Dir//A//B"),TEXT("File.exe"),vFilePathList);
// // vFilePathList中存储了B文件夹下所有扩展名为“.exe”路径名
// DirOperator.FindFileFromDirectory(TEXT("c://Dir//A//B"),TEXT("*.exe"),vFilePathList);
// //删除文件夹,将删除文件下的所有文件
// DirOperator. DeleteDirectorye(TEXT("c://Dir"));