'------------------------------------ '定义win文件夹操作的函数(复制和删除,其它的(移动、改名)没用到) '删除文件夹的函数:KillPath(path) '复制文件夹的函数:CopyPath(mpath,tPath) '------------------------------------ Private Const FO_MOVE = &H1 Private Const FO_COPY = &H2 Private Const FO_DELETE = &H3 Private Const FO_RENAME = &H4 Private Const FOF_NOCONFIRMATION = &H10 Private Const FOF_SILENT = &H4 Private Const FOF_NOERRORUI = &H400 Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String 'only used if FOF_SIMPLEPROGRESS End Type'删除文件夹的函数:KillPath(path) Public Function KillPath(ByVal sPath As String) As Boolean On Error Resume Next Dim udtPath As SHFILEOPSTRUCT udtPath.hwnd = 0 udtPath.wFunc = FO_DELETE udtPath.pFrom = sPath udtPath.pTo = "" udtPath.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI KillPath = Not CBool(SHFileOperation(udtPath)) End Function'复制文件夹的函数:CopyPath(mpath,tPath) Public Function CopyPath(ByVal mPath As String, ByVal tPath As String) As Boolean On Error Resume Next Dim shfileop As SHFILEOPSTRUCT shfileop.hwnd = 0 shfileop.wFunc = FO_COPY shfileop.pFrom = mPath shfileop.pTo = tPath shfileop.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI CopyPath = Not CBool(SHFileOperation(shfileop)) End Function '------------------------ '------------------------
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String '只有在 FOF_SIMPLEPROGRESS 时用
End TypePrivate Declare Function SHFileOperation Lib _
"shell32.dll" Alias "SHFileOperationA" (lpFileOp _
As SHFILEOPSTRUCT) As Long'wFunc 常数
'FO_COPY 把 pFrom 文件拷贝到 pTo。
Const FO_COPY = &H2'fFlag 常数
'FOF_ALLOWUNDO 允许 Undo 。
Const FOF_ALLOWUNDO = &H40
'FOF_NOCONFIRMATION 不显示系统确认对话框。
Const FOF_NOCONFIRMATION = &H10
'FOF_NOCONFIRMMKDIR 不提示是否新建目录。
Const FOF_NOCONFIRMMKDIR = &H200
'FOF_SILENT 不显示进度对话框
Const FOF_SILENT = &H4'例子:从c:\tmp复制到d:\tmp
Dim SHFileOp As SHFILEOPSTRUCTSHFileOp.wFunc = FO_COPY
SHFileOp.pFrom = "c:\tmp\*.*"
SHFileOp.pTo = "d:\tmp\*.*"
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR
Call SHFileOperation(SHFileOp)
pFrom和pTo可以只写文件夹名,可以复制整个文件夹,包括子文件夹
fFlag 可以是常数任意组合的相加,比如加上
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_SILENT
就不显示复制进度对话框
'定义win文件夹操作的函数(复制和删除,其它的(移动、改名)没用到)
'删除文件夹的函数:KillPath(path)
'复制文件夹的函数:CopyPath(mpath,tPath)
'------------------------------------
Private Const FO_MOVE = &H1
Private Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_RENAME = &H4
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_SILENT = &H4
Private Const FOF_NOERRORUI = &H400
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String 'only used if FOF_SIMPLEPROGRESS
End Type'删除文件夹的函数:KillPath(path)
Public Function KillPath(ByVal sPath As String) As Boolean
On Error Resume Next
Dim udtPath As SHFILEOPSTRUCT
udtPath.hwnd = 0
udtPath.wFunc = FO_DELETE
udtPath.pFrom = sPath
udtPath.pTo = ""
udtPath.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI
KillPath = Not CBool(SHFileOperation(udtPath))
End Function'复制文件夹的函数:CopyPath(mpath,tPath)
Public Function CopyPath(ByVal mPath As String, ByVal tPath As String) As Boolean
On Error Resume Next
Dim shfileop As SHFILEOPSTRUCT
shfileop.hwnd = 0
shfileop.wFunc = FO_COPY
shfileop.pFrom = mPath
shfileop.pTo = tPath
shfileop.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI
CopyPath = Not CBool(SHFileOperation(shfileop))
End Function
'------------------------
'------------------------
我照搬2楼的源码 只复制过去一个文件夹 文件夹下的文件都没拷过去
而且也没有出现过所谓的复制进度对话框
*/
是pFrom和pTo有问题,应该是
SHFileOp.pFrom = "c:\tmp\*.*"
SHFileOp.pTo = "d:\tmp"
把c:\tmp下所有文件复制到d:\tmp
以下是把tmp整个文件夹复制到d:\tmp下面
SHFileOp.pFrom = "C:\tmp"
SHFileOp.pTo = "d:\tmp"没有出现过所谓的复制进度对话框,因为文件太小,复制一个需要一定时间的大文件试试