1 #include "stdafx.h"
2
3 // 利用Shell复制多个文件,以|分隔,末尾以两个|结束
4 // 返回 0 表示全部复制成功
5 // 否则表示操作被中止
6 LONG ShellCopyFile(__in LPCTSTR pszSrc, __in LPCTSTR pszDest)
7 {
8 size_t srclen = lstrlen(pszSrc)+2;
9 size_t destlen = lstrlen(pszDest)+2;
10 LPTSTR pszSrcBuffer = new TCHAR[srclen];
11 LPTSTR pszDestBuffer = new TCHAR[destlen];
12
13 ZeroMemory(pszSrcBuffer, srclen*sizeof(TCHAR));
14 ZeroMemory(pszDestBuffer, destlen*sizeof(TCHAR));
15
16 lstrcpy(pszSrcBuffer, pszSrc);
17 lstrcpy(pszDestBuffer, pszDest);
18
19 LPTSTR p;
20
21 p = pszSrcBuffer;
22 while (*p)
23 {
24 if (*p == _T('|'))
25 *p = 0;
26
27 ++p;
28 }
29
30 p = pszDestBuffer;
31 while (*p)
32 {
33 if (*p == _T('|'))
34 *p = 0;
35
36 ++p;
37 }
38
39 SHFILEOPSTRUCT shFileOperation;
40 ZeroMemory(&shFileOperation, sizeof(shFileOperation));
41 shFileOperation.fFlags = FOF_ALLOWUNDO|FOF_MULTIDESTFILES|FOF_NOCONFIRMATION;
42 shFileOperation.wFunc = FO_COPY;
43 shFileOperation.pFrom = pszSrcBuffer;
44 shFileOperation.pTo = pszDestBuffer;
45
46 SHFileOperation(&shFileOperation);
47
48 delete pszSrcBuffer;
49 delete pszDestBuffer;
50
51 return shFileOperation.fAnyOperationsAborted;
52 }
53
54 // 利用Shell删除文件(到 回收站)
55 // 删除多个文件用|分隔,末尾以两个|结束
56 // 返回值为 0 表示所有文件删除成功
57 LONG ShellDeleteFile(__in LPCTSTR pszSrc)
58 {
59 size_t len = lstrlen(pszSrc)+2;
60 LPTSTR pszSrcBuffer = new TCHAR[len];
61 ZeroMemory(pszSrcBuffer, len*sizeof(TCHAR));
62 lstrcpy(pszSrcBuffer, pszSrc);
63
64 LPTSTR p = pszSrcBuffer;
65 while (*p)
66 {
67 if (*p == _T('|'))
68 *p = 0;
69
70 ++p;
71 }
72
73 SHFILEOPSTRUCT shFileOperation;
74 ZeroMemory(&shFileOperation, sizeof(shFileOperation));
75 shFileOperation.wFunc = FO_DELETE;
76 shFileOperation.fFlags = FOF_MULTIDESTFILES|FOF_ALLOWUNDO|FOF_NOCONFIRMATION;
77 shFileOperation.pFrom = pszSrcBuffer;
78 SHFileOperation(&shFileOperation);
79 delete pszSrcBuffer;
80
81 return shFileOperation.fAnyOperationsAborted;
82 }
83
84 // 利用Shell移动文件或文件夹
85 // 返回 0 表示所有文件或文件夹操作成功
86 // 多个文件用|分隔,最后以两个|结束
87 LONG ShellMoveFile(__in LPCTSTR pszSrc, __in LPCTSTR pszDest)
88 {
89 size_t srclen = lstrlen(pszSrc)+2;
90 size_t destlen = lstrlen(pszDest)+2;
91
92 LPTSTR pszSrcBuffer = new TCHAR[srclen];
93 LPTSTR pszDestBuffer = new TCHAR[destlen];
94 ZeroMemory(pszSrcBuffer, srclen*sizeof(TCHAR));
95 ZeroMemory(pszDestBuffer, destlen*sizeof(TCHAR));
96 lstrcpy(pszSrcBuffer, pszSrc);
97 lstrcpy(pszDestBuffer, pszDest);
98
99 LPTSTR p = pszSrcBuffer;
100 while (*p)
101 {
102 if (*p == _T('|'))
103 *p = 0;
104
105 ++p;
106 }
107
108 p = pszDestBuffer;
109 while (*p)
110 {
111 if (*p == _T('|'))
112 *p = 0;
113
114 ++p;
115 }
116
117 SHFILEOPSTRUCT shFileOperation;
118 ZeroMemory(&shFileOperation, sizeof(shFileOperation));
119 shFileOperation.wFunc = FO_MOVE;
120 shFileOperation.fFlags = FOF_ALLOWUNDO|FOF_MULTIDESTFILES|FOF_NOCONFIRMATION;
121 shFileOperation.pFrom = pszSrcBuffer;
122 shFileOperation.pTo = pszDestBuffer;
123 SHFileOperation(&shFileOperation);
124
125 return shFileOperation.fAnyOperationsAborted;
126 }
127
128 int _tmain(int argc, _TCHAR* argv[])
129 {
130 // ShellMoveFile(_T("D:\\*.rar"), _T("E:\\"));
131 // ShellDeleteFile(_T("D:\\eHomeNet.rar"));
132 // ShellCopyFile(L"D:\\video\\AV\\*.*", L"E:\\");
133 return 0;
134 }
135