-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileprocess.cpp
More file actions
390 lines (342 loc) · 12.1 KB
/
Copy pathfileprocess.cpp
File metadata and controls
390 lines (342 loc) · 12.1 KB
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
#include "stdafx.h"
#include "FileManager.h"
#include "fileprocess.h"
#include <fstream>
bool OpenDirectory(HWND owner, CString& path)
{
std::string shell;
shell="/e,"+path;
//打开指定目录 "/e,C:\\Windows"
if(ShellExecute(owner, _T("open"), _T("explorer.exe"), shell.c_str(), NULL, SW_SHOWNORMAL)<=(HINSTANCE)32)
{
AfxMessageBox(theApp.LoadString(IDS_OpenDirectoryFailed)); //打开指定目录失败!
return false;
}
return true;
}
bool OpenDirectorySelectFile(HWND owner, CString& pathname)
{
std::string shell;
shell="/select,"+pathname;
//打开指定目录并选择指定文件 "/select,C:\\Windows\\desktop.ini"
if(ShellExecute(owner, _T("open"), _T("explorer.exe"), shell.c_str(), NULL, SW_SHOWNORMAL)<=(HINSTANCE)32)
{
AfxMessageBox(theApp.LoadString(IDS_OpenDirectoryFailed)); //打开指定目录失败!
return false;
}
return true;
}
CString ShowFolderBrowse(HWND owner)
{
char szPath[MAX_PATH]; //存放选择的目录路径
ZeroMemory(szPath, sizeof(szPath));
BROWSEINFO bi;
bi.hwndOwner = owner;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = "";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
//弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder(&bi);
CString str("");
if(lp && SHGetPathFromIDList(lp, szPath))
str = szPath;
return str;
}
/*
lpPath 指定欲遍历的路径(文件夹)
bRecursion 指定是否递归处理子目录
bEnumFiles 指定是枚举文件还是枚举子目录
pFunc 为用户回调函数,枚举过程中每遇到一个文件或子目录,都会调用它,并传入这个文件或子目录的完整路径
pUserData 为用户任意指定的数据,它也将被传入用户回调函数。用户回调函数(EnumerateFunc)有两个参数,一个是文件或子目录的完整路径(lpFileOrPath),一个是用户自定义数据(pUserData),它被自动调用,用户需在此函数中编码处理代码。
*/
void doFileEnumeration(LPSTR lpPath, BOOL bRecursion, BOOL bEnumFiles, EnumerateFunc pFunc, void* pUserData)
{
static BOOL s_bUserBreak = FALSE;
try
{
//-------------------------------------------------------------------------
if(s_bUserBreak) return;
int len = strlen(lpPath);
if(lpPath==NULL || len<=0) return;
char path[MAX_PATH];
strcpy(path, lpPath);
if(lpPath[len-1] != '\\') strcat(path, "\\");
strcat(path, "*");
WIN32_FIND_DATA fd;
HANDLE hFindFile = FindFirstFile(path, &fd);
if(hFindFile == INVALID_HANDLE_VALUE)
{
::FindClose(hFindFile); return;
}
char tempPath[MAX_PATH]; BOOL bUserReture=TRUE; BOOL bIsDirectory;
BOOL bFinish = FALSE;
while(!bFinish)
{
strcpy(tempPath, lpPath);
if(lpPath[len-1] != '\\') strcat(tempPath, "\\");
strcat(tempPath, fd.cFileName);
bIsDirectory = ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
//如果是.或..
if( bIsDirectory
&& (strcmp(fd.cFileName, ".")==0 || strcmp(fd.cFileName, "..")==0))
{
bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
continue;
}
if(pFunc && bEnumFiles!=bIsDirectory)
{
bUserReture = pFunc(tempPath, pUserData);
if(bUserReture==FALSE)
{
s_bUserBreak = TRUE; ::FindClose(hFindFile); return;
}
}
if(bIsDirectory && bRecursion) //是子目录
{
doFileEnumeration(tempPath, bRecursion, bEnumFiles, pFunc, pUserData);
}
bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
}
::FindClose(hFindFile);
//-------------------------------------------------------------------------
}catch(...){ ASSERT(0); return; }
}
int doEmptyFolderEnumeration(CString lpPath, EmptyFolderEnumerateFunc pFunc, void* pUserData)
{
lpPath+="\\*.*";
int filenum=0;
CFileFind findfile;
BOOL m_flag = findfile.FindFile(lpPath);
while(m_flag)
{
m_flag = findfile.FindNextFile();
if(findfile.IsDirectory()) //如果是目录跳过
{
if((!findfile.IsDots()))
{
CString filename = findfile.GetFilePath();
int num = doEmptyFolderEnumeration(filename, pFunc, pUserData);
if(num==0)
pFunc(filename, findfile.IsSystem(), findfile.IsReadOnly(), pUserData);
filenum+=num;
}
continue;
}
else
filenum++;
}
return filenum;
}
int doEmptyFolderEnumeration(CString lpPath, std::list<EmptyFolderInfo*>& m_empty_file_list)
{
lpPath+="\\*.*";
int filenum=0;
CFileFind findfile;
BOOL m_flag = findfile.FindFile(lpPath);
while(m_flag)
{
m_flag = findfile.FindNextFile();
if(findfile.IsDirectory()) //如果是目录跳过
{
if((!findfile.IsDots()))
{
CString filename = findfile.GetFilePath();
int num = doEmptyFolderEnumeration(filename, m_empty_file_list);
if(num==0)
{
EmptyFolderInfo* info = new EmptyFolderInfo(filename, findfile.IsSystem(), findfile.IsReadOnly());
m_empty_file_list.push_back(info);//pFunc(filename, findfile.IsSystem(), findfile.IsReadOnly(), /*findfile.IsTemporary(),*/ pUserData);
}
filenum+=num;
}
continue;
}
else
filenum++;
}
return filenum;
}
void ExportFoldersToFile(CString& path, CString& FolderName, CString& FullPath, CString& SystemFile, CString& ReadOnly, CString& Yes, CString& No,
std::list<EmptyFolderInfo*>& empty_file_list, ExportFoldersToFileFunc pFunc, void* pUserData)
{
std::fstream file(path, std::fstream::out);
file<<FolderName<<','<<FullPath<<','<<SystemFile<<','<<ReadOnly<<"\n";
for (auto list_Iter = empty_file_list.begin(); list_Iter != empty_file_list.end(); ++list_Iter)
{
if((*list_Iter)->IsDisplay)
{
int pos = (*list_Iter)->FolderPath.ReverseFind('\\');
file<<(*list_Iter)->FolderPath.Right((*list_Iter)->FolderPath.GetLength()-pos-1)<<','\
<<(*list_Iter)->FolderPath<<','\
<<((*list_Iter)->IsSystem? Yes:No)<<','\
<<((*list_Iter)->IsReadOnly? Yes:No)<<"\n";
pFunc((*list_Iter)->FolderPath, pUserData);
}
}
}
int doEmptyFileEnumeration(CString lpPath, EmptyFileEnumerateFunc pFunc, void* pUserData)
{
lpPath+="\\*.*";
CFileFind findfile;
BOOL m_flag = findfile.FindFile(lpPath);
while(m_flag)
{
m_flag = findfile.FindNextFile();
if(findfile.IsDirectory()) //如果是目录跳过
{
if((!findfile.IsDots()))
{
CString filename = findfile.GetFilePath();
doEmptyFileEnumeration(filename, pFunc, pUserData);
}
continue;
}
else
{
if(findfile.GetLength()==0)
{
CTime CreateTime;
CTime AccessTime;
CTime WriteTime;
findfile.GetCreationTime(CreateTime);
findfile.GetLastAccessTime(AccessTime);
findfile.GetLastWriteTime(WriteTime);
pFunc(findfile.GetFilePath(), 0, CreateTime, AccessTime, WriteTime, findfile.IsSystem(), findfile.IsReadOnly(), pUserData);
}
}
}
return 0;
}
void ExportFilesToFile(CString& path,
CString& FolderName, CString& FullPath, CString& FileSize, CString& FileExt, CString& CreateTime, CString& AccessTime, CString& WriteTime,
CString& SystemFile, CString& ReadOnly, CString& Yes, CString& No,
std::list<EmptyFileInfo*>& empty_file_list, ExportFilesToFileFunc pFunc, void* pUserData)
{
std::fstream file(path, std::fstream::out);
file<<FolderName<<','<<FullPath<<','<<FileSize<<','<<FileExt<<','<<CreateTime<<','<<AccessTime<<','<<WriteTime<<','<<SystemFile<<','<<ReadOnly<<"\n";
for (auto list_Iter = empty_file_list.begin(); list_Iter != empty_file_list.end(); ++list_Iter)
{
if((*list_Iter)->IsDisplay)
{
int pos = (*list_Iter)->FilePath.ReverseFind('\\');
int pos_ext = (*list_Iter)->FilePath.ReverseFind('.');
file<<(*list_Iter)->FilePath.Right((*list_Iter)->FilePath.GetLength()-pos-1)<<','\
<<(*list_Iter)->FilePath<<','\
<<(*list_Iter)->FileSize<<"B,"\
<<(*list_Iter)->FilePath.Right((*list_Iter)->FilePath.GetLength()-pos_ext-1)<<','\
<<CTime2String((*list_Iter)->CreateTime)<<','\
<<CTime2String((*list_Iter)->AccessTime)<<','\
<<CTime2String((*list_Iter)->WriteTime)<<','\
<<((*list_Iter)->IsSystem? Yes:No)<<','\
<<((*list_Iter)->IsReadOnly? Yes:No)<<"\n";
pFunc((*list_Iter)->FilePath, pUserData);
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------
int doTempFileEnumeration(CString lpPath, CString& ext, std::list<EmptyFileInfo*>& file_list, TempFileEnumerateFunc pFunc, void* pUserData)
{
lpPath+="\\*.*";
CFileFind findfile;
BOOL m_flag = findfile.FindFile(lpPath);
while(m_flag)
{
m_flag = findfile.FindNextFile();
if(findfile.IsDirectory()) //如果是目录跳过
{
if((!findfile.IsDots()))
{
CString filename = findfile.GetFilePath();
doTempFileEnumeration(filename, ext, file_list, pFunc, pUserData);
}
continue;
}
else
{
CString filename = findfile.GetFileName();
int pos = filename.ReverseFind('.');
filename = filename.Right(filename.GetLength()-pos-1);
if(filename==ext)
{
CTime CreateTime;
CTime AccessTime;
CTime WriteTime;
findfile.GetCreationTime(CreateTime);
findfile.GetLastAccessTime(AccessTime);
findfile.GetLastWriteTime(WriteTime);
EmptyFileInfo* info = new EmptyFileInfo(findfile.GetFilePath(), findfile.GetLength(), CreateTime, AccessTime, WriteTime, findfile.IsSystem(), findfile.IsReadOnly());
file_list.push_back(info);
pFunc(findfile.GetFilePath(), pUserData);
}
}
}
return 0;
}
int doTempFileEnumeration(std::list<TempCleanerInfo*>& cleaner_list, TempFileEnumerateFunc pFunc, void* pUserData)
{
for (auto list_Iter = cleaner_list.begin(); list_Iter != cleaner_list.end(); ++list_Iter)
{
if((*list_Iter)->m_check)
doTempFileEnumeration((*list_Iter)->m_folder_path, (*list_Iter)->m_ext, (*list_Iter)->m_file_list, pFunc, pUserData);
}
return 0;
}
void ExportTempCleanerToFile(CString& path,
CString& FolderName, CString& FullPath, CString& FileSize, CString& CreateTime, CString& AccessTime, CString& WriteTime,
CString& SystemFile, CString& ReadOnly, CString& Yes, CString& No,
std::list<TempCleanerInfo*>& cleaner_list, ExportTempCleanerToFileFunc pFunc, void* pUserData)
{
std::fstream file(path, std::fstream::out);
file<<FolderName<<','<<FullPath<<','<<FileSize<<','<<CreateTime<<','<<AccessTime<<','<<WriteTime<<','<<SystemFile<<','<<ReadOnly<<"\n";
for (auto cleaner_list_Iter = cleaner_list.begin(); cleaner_list_Iter != cleaner_list.end(); ++cleaner_list_Iter)
{
for (auto list_Iter = (*cleaner_list_Iter)->m_file_list.begin(); list_Iter != (*cleaner_list_Iter)->m_file_list.end(); ++list_Iter)
{
if((*list_Iter)->IsDisplay)
{
int pos = (*list_Iter)->FilePath.ReverseFind('\\');
file<<(*list_Iter)->FilePath.Right((*list_Iter)->FilePath.GetLength()-pos-1)<<','\
<<(*list_Iter)->FilePath<<','\
<<FileSize2String((*list_Iter)->FileSize)<<','\
<<CTime2String((*list_Iter)->CreateTime)<<','\
<<CTime2String((*list_Iter)->AccessTime)<<','\
<<CTime2String((*list_Iter)->WriteTime)<<','\
<<((*list_Iter)->IsSystem? Yes:No)<<','\
<<((*list_Iter)->IsReadOnly? Yes:No)<<"\n";
pFunc((*list_Iter)->FilePath, pUserData);
}
}
}
}
CString CTime2String(CTime time)
{
CString str;
str.Format("%04d/%02d/%02d %02d:%02d:%02d",time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(), time.GetSecond());
return str;
}
CString FileSize2String(unsigned int FileSize)
{
CString str;
if(FileSize>=1024*1024)
str.Format("%0.2fMB",FileSize/1024.0/1024.0);
else if(FileSize>=1024)
str.Format("%0.2fKB",FileSize/1024.0);
else
str.Format("%dB",FileSize);
return str;
}
unsigned int String2FileSize(CString str)
{
char *pbuf=str.GetBuffer();
double num=atof(pbuf);
str.ReleaseBuffer();
if((pbuf[str.GetLength()-2]=='K'))
num*=1000;
else if((pbuf[str.GetLength()-2]=='M'))
num*=1000000;
return num;
}