1 | 项目中一小段代码 |
01 | //------------------------------------------------- |
02 | // 复制文件 |
03 | //------------------------------------------------- |
04 | void CVideoDemoDlg::Copy_file() |
05 | { |
06 | TCHAR path[200]; |
07 | CString strSrcPath; |
08 | CString strDesPath; |
09 | //设置要复制的到文件夹路径 |
10 | strDesPath = "E:\\" ; |
11 | //设置当前工作路径 |
12 | SetCurrentDirectory( "D:\\" ); |
13 | GetCurrentDirectory(200,path); |
14 | CFileFind finder; |
15 | BOOL bWorking = finder.FindFile( "*.*" ); |
16 | while (bWorking) |
17 | { |
18 | bWorking = finder.FindNextFile(); |
19 | strSrcPath = "D:\\" ; |
20 | CString filename= finder.GetFileName(); |
21 | //得到文件或文件夹名 |
22 | if (filename==savanewfilename || filename == "." || filename == ".." || filename == "Recycled" || filename == "System Volume Information" ) |
23 | { //对于文件系统提供的文件不fj |
24 | } |
25 | else |
26 | { |
27 | strSrcPath = strSrcPath + filename; |
28 | CopyDirectory(strSrcPath,strDesPath); |
29 | //复制目录 |
30 | } |
31 | } |
32 | } |
01 | //------------------------------------------------- |
02 | // 供Copy_file()使用 |
03 | //------------------------------------------------- |
04 | void CVideoDemoDlg::CopyDirectory(CString pTo,CString pFrom) |
05 | { |
06 | char buf[1024]; |
07 | char buf1[1024]; |
08 | SHFILEOPSTRUCT fo; |
09 | memset (buf,0, sizeof (buf)); |
10 | memset (buf1,0, sizeof (buf1)); |
11 | memset (&fo,0, sizeof (fo)); |
12 | strcpy (buf,pTo); |
13 | strcpy (buf1,pFrom); |
14 | fo.wFunc=FO_COPY; |
15 | //复制是FO_COPY,删除是FO_DELETE; 移动FO_MOVE |
16 | fo.pFrom=buf; |
17 | fo.pTo=buf1; |
18 | fo.fFlags=FOF_NOERRORUI|FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION; |
19 | SHFileOperation(&fo); |
20 | } |
21 | //------------------------------------------------- |
22 | // 删除文件 |
23 | //------------------------------------------------- |
24 | void CVideoDemoDlg::Delete_file() |
25 | { |
26 | TCHAR path[200]; |
27 | SetCurrentDirectory( "D:\\" ); |
28 | //设置当前目录 |
29 | GetCurrentDirectory(200,path); |
30 | //得到当前目录路径 |
31 | CFileFind finder; |
32 | BOOL bWorking = finder.FindFile( "*.*" ); |
33 | while (bWorking) |
34 | { |
35 | bWorking = finder.FindNextFile(); |
36 | CString filename= finder.GetFileName(); |
37 | //得到文件名 |
38 | if (filename==savanewfilename || filename == "." || filename == ".." || filename == "Recycled" || filename == "System Volume Information" ) |
39 | { |
40 | //如果是当前在存储的文件夹或根目录或上及目录以及磁盘回收站则不人任何处理 |
41 | } |
42 | else |
43 | { |
44 | DeleteDirectory(filename); |
45 | //删除目录 |
46 | } |
47 | } |
48 | } |
49 | //------------------------------------------------- |
50 | // 供Delete_file()调用 |
51 | //------------------------------------------------- |
52 | BOOL CVideoDemoDlg::DeleteDirectory( LPCTSTR DirName) |
53 | { |
54 | CFileFind tempFind; |
55 | char tempFileFind[200]; |
56 | sprintf (tempFileFind, "%s\\*.* " ,DirName); |
57 | BOOL IsFinded=( BOOL )tempFind.FindFile(tempFileFind); |
58 | //判断文件夹 |
59 | while (IsFinded) |
60 | { |
61 | IsFinded=( BOOL )tempFind.FindNextFile(); |
62 | //寻找下一个目录 |
63 | if (tempFileFind == str) |
64 | { |
65 | //如果等于当前正在存储的文件夹就不进行任保操作 |
66 | } |
67 | if (!tempFind.IsDots()) |
68 | { |
69 | char foundFileName[200]; |
70 | strcpy (foundFileName,tempFind.GetFileName().GetBuffer(200)); |
71 | if (tempFind.IsDirectory()) |
72 | { |
73 | char tempDir[200]; |
74 | sprintf (tempDir, "%s\\%s " ,DirName,foundFileName); |
75 | DeleteDirectory(tempDir); |
76 | //删除文件夹 |
77 | } |
78 | else |
79 | { |
80 | char tempFileName[200]; |
81 | sprintf (tempFileName, "%s\\%s " ,DirName,foundFileName); |
82 | DeleteFile(tempFileName); |
83 | //删除文件 |
84 | } |
85 | } |
86 | } |
87 | tempFind.Close(); |
88 | //当寻找文件的事件结束就结束寻找功能 |
89 | RemoveDirectory(DirName); |
90 | //删除这个目录 |
91 | return TRUE; |
92 | } |
1 | 以上代码是用于文件复制和删除,但是其特殊性,是由其需求确认,正在写的当前文件不能复制和删除 |