'将SHFILEOPSTRUCT的fFlags设置为FOF_NOCONFIRMATION就可以去掉确认提示框 '代码如下,且经过严格测试:Option Explicit 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 End TypePrivate Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End Type Private Const SEE_MASK_INVOKEIDLIST = &HC Private Const SEE_MASK_NOCLOSEPROCESS = &H40 Private Const SEE_MASK_FLAG_NO_UI = &H400Private Const FOF_ALLOWUNDO = &H40 Private Const FOF_CONFIRMMOUSE = &H2 Private Const FOF_FILESONLY = &H80 ' on *.*, do only files Private Const FOF_MULTIDESTFILES = &H1 Private Const FOF_NOCONFIRMATION = &H10 ' Don't prompt the user. Private Const FOF_NOCONFIRMMKDIR = &H200 ' don't confirm making any needed dirs Private Const FOF_RENAMEONCOLLISION = &H8 Private Const FOF_SILENT = &H4 ' don't create progress/report Private Const FOF_SIMPLEPROGRESS = &H100 ' means don't show names of files Private Const FOF_WANTMAPPINGHANDLE = &H20 ' Fill in SHFILEOPSTRUCT.hNameMappings Private Const FOF_NOERRORUI = &O400Private Declare Function SHFileOperation Lib "shell32.dll" Alias _ "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongFunction RecycleBin(sFileNames As String) As Boolean Dim lThisFile As Long, lRetVal As Long Dim tSHFileOp As SHFILEOPSTRUCT, sFileName As String
Const FO_DELETE = &H3, FOF_ALLOWUNDO = &H40 On Error GoTo ErrFailed 'Build up Delete String If Len(sFileNames) Then sFileNames = sFileNames & vbNullChar With tSHFileOp .wFunc = FO_DELETE .pFrom = sFileNames .fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION End With lRetVal = SHFileOperation(tSHFileOp) End If Exit Function ErrFailed: RecycleBin = True End FunctionPrivate Sub Command1_Click() Call RecycleBin("d:\1.htm" & vbNullChar) End Sub
应该调用DOS中的DEL命令,但为了达到楼主要的效果,要带两个参数运行 DEL /S /Q其中: /S 从所有子目录删除指定文件(可达到删除目录数的效果); /Q 安静模式,不要求确认。楼主可以用在VB中使用SHELL语句调用DOS功能~ 如: x=shell("command.com /c DEL /S /Q",vbhide)说明: 你可以把双引号之间的字符串存储在一个字符串变量中。 command.com的作用不用我说了吧?
dim chfile as string chfile =dir(path+iif(len(path)=3,"","\") & "*.*" do until chfile="" kill chfile chfile =dir(path+iif(len(path)=3,"","\") & "*.*" loop
删除一个指定的文件夹和它的内容。
'代码如下,且经过严格测试:Option Explicit
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
End TypePrivate Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Private Const SEE_MASK_INVOKEIDLIST = &HC
Private Const SEE_MASK_NOCLOSEPROCESS = &H40
Private Const SEE_MASK_FLAG_NO_UI = &H400Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_CONFIRMMOUSE = &H2
Private Const FOF_FILESONLY = &H80 ' on *.*, do only files
Private Const FOF_MULTIDESTFILES = &H1
Private Const FOF_NOCONFIRMATION = &H10 ' Don't prompt the user.
Private Const FOF_NOCONFIRMMKDIR = &H200 ' don't confirm making any needed dirs
Private Const FOF_RENAMEONCOLLISION = &H8
Private Const FOF_SILENT = &H4 ' don't create progress/report
Private Const FOF_SIMPLEPROGRESS = &H100 ' means don't show names of files
Private Const FOF_WANTMAPPINGHANDLE = &H20 ' Fill in SHFILEOPSTRUCT.hNameMappings
Private Const FOF_NOERRORUI = &O400Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongFunction RecycleBin(sFileNames As String) As Boolean
Dim lThisFile As Long, lRetVal As Long
Dim tSHFileOp As SHFILEOPSTRUCT, sFileName As String
Const FO_DELETE = &H3, FOF_ALLOWUNDO = &H40
On Error GoTo ErrFailed
'Build up Delete String
If Len(sFileNames) Then
sFileNames = sFileNames & vbNullChar
With tSHFileOp
.wFunc = FO_DELETE
.pFrom = sFileNames
.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
End With
lRetVal = SHFileOperation(tSHFileOp)
End If
Exit Function
ErrFailed:
RecycleBin = True
End FunctionPrivate Sub Command1_Click()
Call RecycleBin("d:\1.htm" & vbNullChar)
End Sub
DEL /S /Q其中:
/S 从所有子目录删除指定文件(可达到删除目录数的效果);
/Q 安静模式,不要求确认。楼主可以用在VB中使用SHELL语句调用DOS功能~
如:
x=shell("command.com /c DEL /S /Q",vbhide)说明:
你可以把双引号之间的字符串存储在一个字符串变量中。
command.com的作用不用我说了吧?
chfile =dir(path+iif(len(path)=3,"","\") & "*.*"
do until chfile=""
kill chfile
chfile =dir(path+iif(len(path)=3,"","\") & "*.*"
loop