我一般都是用fso来操作,比较直观, 比如: dim fso as new filesystemobject dim myfolder as folderfso.movefolder fso.deleterfolder fso.copyfolder (没上机调试,手工打的,可能有错误,还有,记得引用microsoft scripting runtime
Private Const FO_COPY = &H1 Private Const FO_MOVE = &H2 Private Const FO_DELETE = &H3 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 TypePublic Function FileOpt(ByVal sSourceFile As String,ByVal sDestFile As String) As Boolean Dim udtPath As SHFILEOPSTRUCT udtPath.hWnd = 0 udtPath.wFunc = FO_COPY '拷贝文件 FO_MOVE 移动文件 FO_DELETE 删除文件 udtPath.pFrom = sSourceFile udtPath.pTo = sDestFile udtPath.fFlags = FOF_NOCONFIRMATION FileOpt = Not CBool(SHFileOperation(udtPath)) End Function
Private Declare Function SHFileOperation Lib _ "shell32.dll" (ByRef lpFileOp As _ SHFILEOPSTRUCT) As LongPrivate Const ERROR_SUCCESS = 0& Private Const FO_COPY = &H2 Private Const FO_DELETE = &H3 Private Const FO_MOVE = &H1 Private Const FO_RENAME = &H4 Private Const FOF_ALLOWUNDO = &H40 Private Const FOF_CONFIRMMOUSE = &H2 Private Const FOF_FILESONLY = &H80 Private Const FOF_MULTIDESTFILES = &H1 Private Const FOF_NOCONFIRMATION = &H10 Private Const FOF_NOCONFIRMMKDIR = &H200 Private Const FOF_RENAMEONCOLLISION = &H8 Private Const FOF_SILENT = &H4 Private Const FOF_SIMPLEPROGRESS = &H100 Private Const FOF_WANTMAPPINGHANDLE = &H20Private 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
所有操作都可以通过API函数SHFileOperation实现
比如:
dim fso as new filesystemobject
dim myfolder as folderfso.movefolder
fso.deleterfolder
fso.copyfolder
(没上机调试,手工打的,可能有错误,还有,记得引用microsoft scripting runtime
Private Const FO_MOVE = &H2
Private Const FO_DELETE = &H3
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 TypePublic Function FileOpt(ByVal sSourceFile As String,ByVal sDestFile As String) As Boolean
Dim udtPath As SHFILEOPSTRUCT
udtPath.hWnd = 0
udtPath.wFunc = FO_COPY '拷贝文件 FO_MOVE 移动文件 FO_DELETE 删除文件
udtPath.pFrom = sSourceFile
udtPath.pTo = sDestFile
udtPath.fFlags = FOF_NOCONFIRMATION
FileOpt = Not CBool(SHFileOperation(udtPath))
End Function
"shell32.dll" (ByRef lpFileOp As _
SHFILEOPSTRUCT) As LongPrivate Const ERROR_SUCCESS = 0&
Private Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_MOVE = &H1
Private Const FO_RENAME = &H4
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_CONFIRMMOUSE = &H2
Private Const FOF_FILESONLY = &H80
Private Const FOF_MULTIDESTFILES = &H1
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_NOCONFIRMMKDIR = &H200
Private Const FOF_RENAMEONCOLLISION = &H8
Private Const FOF_SILENT = &H4
Private Const FOF_SIMPLEPROGRESS = &H100
Private Const FOF_WANTMAPPINGHANDLE = &H20Private 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
Kill 删除
Name 移动
(真丢人,俺竟然忘了这种方法 ^_^ )