使用APIPrivate Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
具体代码:Public Const FO_MOVE As Long = &H1 Public Const FO_COPY As Long = &H2 Public Const FO_DELETE As Long = &H3 Public Const FO_RENAME As Long = &H4 Public Const FOF_MULTIDESTFILES As Long = &H1 Public Const FOF_CONFIRMMOUSE As Long = &H2 Public Const FOF_SILENT As Long = &H4 Public Const FOF_RENAMEONCOLLISION As Long = &H8 Public Const FOF_NOCONFIRMATION As Long = &H10 Public Const FOF_WANTMAPPINGHANDLE As Long = &H20 Public Const FOF_CREATEPROGRESSDLG As Long = &H0 Public Const FOF_ALLOWUNDO As Long = &H40 Public Const FOF_FILESONLY As Long = &H80 Public Const FOF_SIMPLEPROGRESS As Long = &H100 Public Const FOF_NOCONFIRMMKDIR As Long = &H200Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Long fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String End TypeDeclare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongPublic Function FileCopyTo(str1 As String, str2 As String) As Long Dim result As Long, fileop As SHFILEOPSTRUCT With fileop .hwnd = 0 .wFunc = FO_COPY .pFrom = str1 & vbNullChar & vbNullChar .pTo = str2 & vbNullChar & vbNullChar .fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY End With result = SHFileOperation(fileop)End FunctionPublic Function FileMoveTo(str1 As String, str2 As String) As Long Dim result As Long, fileop As SHFILEOPSTRUCT With fileop .hwnd = 0 .wFunc = FO_MOVE .pFrom = str1 & vbNullChar & vbNullChar .pTo = str2 & vbNullChar & vbNullChar .fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY End With result = SHFileOperation(fileop)End Function
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 '只有在 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 'FO_DELETE 删除 pFrom 中的文件(pTo 忽略)。 Const FO_DELETE = &H3 'FO_MOVE 把 pFrom 文件移动到 pTo。 Const FO_MOVE = &H1'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 = &H4Private Sub Command1_Click() ' 拷贝文件夹D:\test到E:\ Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_COPY SHFileOp.pFrom = "D:\test" SHFileOp.pTo = "E:\" SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_SILENT Call SHFileOperation(SHFileOp) End Sub
不用shell没有别的办法吗?
Public Const FO_COPY As Long = &H2
Public Const FO_DELETE As Long = &H3
Public Const FO_RENAME As Long = &H4
Public Const FOF_MULTIDESTFILES As Long = &H1
Public Const FOF_CONFIRMMOUSE As Long = &H2
Public Const FOF_SILENT As Long = &H4
Public Const FOF_RENAMEONCOLLISION As Long = &H8
Public Const FOF_NOCONFIRMATION As Long = &H10
Public Const FOF_WANTMAPPINGHANDLE As Long = &H20
Public Const FOF_CREATEPROGRESSDLG As Long = &H0
Public Const FOF_ALLOWUNDO As Long = &H40
Public Const FOF_FILESONLY As Long = &H80
Public Const FOF_SIMPLEPROGRESS As Long = &H100
Public Const FOF_NOCONFIRMMKDIR As Long = &H200Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Long
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End TypeDeclare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongPublic Function FileCopyTo(str1 As String, str2 As String) As Long Dim result As Long, fileop As SHFILEOPSTRUCT
With fileop
.hwnd = 0
.wFunc = FO_COPY
.pFrom = str1 & vbNullChar & vbNullChar
.pTo = str2 & vbNullChar & vbNullChar
.fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY
End With
result = SHFileOperation(fileop)End FunctionPublic Function FileMoveTo(str1 As String, str2 As String) As Long Dim result As Long, fileop As SHFILEOPSTRUCT
With fileop
.hwnd = 0
.wFunc = FO_MOVE
.pFrom = str1 & vbNullChar & vbNullChar
.pTo = str2 & vbNullChar & vbNullChar
.fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY
End With
result = SHFileOperation(fileop)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
'FO_DELETE 删除 pFrom 中的文件(pTo 忽略)。
Const FO_DELETE = &H3
'FO_MOVE 把 pFrom 文件移动到 pTo。
Const FO_MOVE = &H1'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 = &H4Private Sub Command1_Click()
' 拷贝文件夹D:\test到E:\
Dim SHFileOp As SHFILEOPSTRUCT
SHFileOp.wFunc = FO_COPY
SHFileOp.pFrom = "D:\test"
SHFileOp.pTo = "E:\"
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_SILENT
Call SHFileOperation(SHFileOp)
End Sub
其中
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_SILENT
这一句,可以任意选择所需的常数相加
结帖
/e是复制空目录
/c是出现复制错误时不提示
/y是不提示用户覆盖文件
/h复制系统和隐藏文件哈哈,我也来抢了,楼主多少给点吧~~
2>用FSO
我觉得用第一种方法好,可以不用添加引用,节省内存
其实用Shell也不错
其实这就是Windows本身复制文件所用的API函数,你用Shell其实最终还是调用的这个
sorry,我说错了,Xcopy好像是一个Dos下的可执行文件,和Windows本身的复制不同的//他们写的不如shell好用,速度快,代码简洁,不用调用API,复制我的不用打字,不会改错
我晕,复制只要动动鼠标,用不到打字吧
XCOPY要依赖外部文件的,如果这个文件不存在呢?