Public 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 Long
End Type
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
'//wFunc
Public Const FO_MOVE = &H1
Public Const FO_COPY = &H2
Public Const FO_DELETE = &H3
Public Const FO_RENAME = &H4
'//fFlags
Public Const FOF_ALLOWUNDO = &H40 '//允许反悔
Public Const FOF_FILESONLY = &H80 '//只允许文件操作,不允许目录操作
Public Const FOF_MULTIDESTFILES = &H1 '//允许多个目的目录或文件
Public Const FOF_NOCONFIRMATION = &H10 '//不做预先征询使用者
Public Const FOF_RENAMEONCOLLISION = &H8 '//需要建立新目录时,不预先征询使用者
Public Const FOF_SILENT = &H4 '//不显示进度对话框
Private Sub Command1_Click()
Dim strPathName As String
strPathName = ""
strPathName = InputBox("请输入需要删除的文件夹名称∶", "删除文件夹")
If strPathName = "" Then Exit Sub
On Error GoTo ErrorHandle
SetAttr strPathName, vbNormal '此行主要是为了检查文件夹名称的有效性
RecurseTree strPathName
Label1.Caption = "文件夹" & strPathName & "已经删除!"
Exit Sub
ErrorHandle:
MsgBox "无效的文件夹名称:" & strPathName
End Sub
Sub RecurseTree(CurrPath As String)
Dim sFileName As String
Dim newPath As String
Dim sPath As String
Static oldPath As String
sPath = CurrPath & "\"
sFileName = Dir(sPath, 31) '31的含义∶31=vbNormal+vbReadOnly+vbHidden+vbSystem+vbVolume+vbDirectory
Do While sFileName <> ""
If sFileName <> "." And sFileName <> ".." Then
If GetAttr(sPath & sFileName) And vbDirectory Then '如果是目录和文件夹
newPath = sPath & sFileName
RecurseTree newPath
sFileName = Dir(sPath, 31)
Else
SetAttr sPath & sFileName, vbNormal
Kill (sPath & sFileName)
Label1.Caption = sPath & sFileName '显示删除过程
sFileName = Dir
End If
Else
sFileName = Dir
End If
DoEvents
Loop
SetAttr CurrPath, vbNormal
RmDir CurrPath
Label1.Caption = CurrPath
End Sub
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
'wFunc 的设定值
'FO_COPY Copies the files specified by pFrom to the location specified by pTo.
'FO_DELETE Deletes the files specified by pFrom (pTo is ignored).
'FO_MOVE Moves the files specified by pFrom to the location specified by pTo.
'FO_RENAME Renames the files specified by pFrom.
'fFlag的设定
'FOF_ALLOWUNDO Preserves undo information, if possible.
'FOF_FILESONLY Performs the operation only on files if a wildcard filename
' (*.*) is specified.
'FOF_MULTIDESTFILES Indicates that the pTo member specifies multiple destination
' files (one for each source file) rather than one directory where
' all source files are to be deposited.
'FOF_NOCONFIRMATION Responds with "yes to all" for any dialog box that is displayed.
'FOF_NOCONFIRMMKDIR Does not confirm the creation of a new directory if
' the operation requires one to be created.
'FOF_RENAMEONCOLLISION Gives the file being operated on a new name (such as
' "Copy #1 of...") in a move, copy, or rename operation
' if a file of the target name already exists.
'FOF_SILENT Does not display a progress dialog box.
'FOF_SIMPLEPROGRESS Displays a progress dialog box, but does not show the
' filenames.
'FOF_WANTMAPPINGHANDLE Fills in the hNameMappings member. The handle must be
' freed by using the SHFreeNameMappings function.