Dim fso As Object Dim TargetPath As String TargetPath = "D:\Test" Set fso = CreateObject("Scripting.FileSystemObject") fso.DeleteFolder TargetPath, True Set fso = Nothing MsgBox "删除成功"
嗯,FileSystemObject一般的机器上都可以用
不一定,如果机器上没有安装Windows Scripting Runtime就不能运行。
不会是先选择目录然后按“DEL”吧?
Private Type SHFILEOPSTRUCT hWnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAborted As Boolean hNameMaps As Long sProgress As String End Type
Private Const FO_MOVE As Long = &H1 Private Const FO_COPY As Long = &H2 Private Const FO_DELETE As Long = &H3 Private Const FO_RENAME As Long = &H4Private Const FOF_SILENT As Long = &H4 Private Const FOF_RENAMEONCOLLISION As Long = &H8 Private Const FOF_NOCONFIRMATION As Long = &H10 Private Const FOF_SIMPLEPROGRESS As Long = &H100 Private Const FOF_ALLOWUNDO As Long = &H40Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nSize As Long, ByVal lpBuffer As String) As LongPrivate Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
'we'll use Brad's Browse For Folders Dialog code to 'enable the user to pick the source and destination folders.Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Private Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type
Private Const ERROR_SUCCESS As Long = 0 Private Const CSIDL_DESKTOP As Long = &H0 Private Const BIF_RETURNONLYFSDIRS As Long = &H1 Private Const BIF_STATUSTEXT As Long = &H4 Private Const BIF_RETURNFSANCESTORS As Long = &H8 Private Sub Command1_Click() Dim l As Long Dim s As SHFILEOPSTRUCT Dim sou As String Dim msg As String msg = "请选择文件夹" sou = GetBrowseFolder(msg) With s .hWnd = Me.hWnd .fFlags = FOF_SILENT .pFrom = sou .wFunc = FO_DELETE End With l = SHFileOperation(s) End SubPrivate Function GetBrowseFolder(msg) As String Dim pidl As Long Dim pos As Integer Dim path As String Dim bi As BROWSEINFO
'Fill the BROWSEINFO structure with the needed data, 'show the browse dialog, and if the returned value 'indicates success (1), retrieve the user's 'selection contained in pidl With bi .hOwner = Me.hWnd .pidlRoot = CSIDL_DESKTOP .lpszTitle = msg .ulFlags = BIF_RETURNONLYFSDIRS End With pidl = SHBrowseForFolder(bi)
path = Space$(512)
If SHGetPathFromIDList(ByVal pidl, ByVal path) = 1 Then pos = InStr(path, Chr$(0)) GetBrowseFolder = Left(path, pos - 1) End IfEnd Function参考一下吧
我调试时发生 实时错误"453" 找不到dll入口,SHFileOperationA in shell32.dll
可能是你粘铁的时候,没粘好,检查一下SHFileOperation 函数的声明部分,看看SHFileOperationA附近,有没有多了空格之类的,现在它是找不到这个函数,说明你的声明部分有问题,好好检查一下,或者用你自己的apiview重新弄一个SHFileOperation 函数的声明,试试看Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Dim SHf As SHFILEOPSTRUCT
With SHf .wFunc = FO_DELETE .pFrom = "C:\w.txt" & Chr(0) .fFlags = FOF_ALLOWUNDO .fFlags = &H10 Or &H100 'or &H End With Call SHFileOperation(SHf) 请问我这么写有什么问题吗? 为什么文件删不掉呢?
如果不是上面那个朋友的问题,那你先rename一下好了。把它扩展名改掉再删:)
Dim TargetPath As String
TargetPath = "D:\Test"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder TargetPath, True
Set fso = Nothing
MsgBox "删除成功"
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_MOVE As Long = &H1
Private Const FO_COPY As Long = &H2
Private Const FO_DELETE As Long = &H3
Private Const FO_RENAME As Long = &H4Private Const FOF_SILENT As Long = &H4
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_NOCONFIRMATION As Long = &H10
Private Const FOF_SIMPLEPROGRESS As Long = &H100
Private Const FOF_ALLOWUNDO As Long = &H40Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nSize As Long, ByVal lpBuffer As String) As LongPrivate Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
'we'll use Brad's Browse For Folders Dialog code to
'enable the user to pick the source and destination folders.Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Const ERROR_SUCCESS As Long = 0
Private Const CSIDL_DESKTOP As Long = &H0
Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_STATUSTEXT As Long = &H4
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Sub Command1_Click()
Dim l As Long
Dim s As SHFILEOPSTRUCT
Dim sou As String
Dim msg As String
msg = "请选择文件夹"
sou = GetBrowseFolder(msg)
With s
.hWnd = Me.hWnd
.fFlags = FOF_SILENT
.pFrom = sou
.wFunc = FO_DELETE
End With
l = SHFileOperation(s)
End SubPrivate Function GetBrowseFolder(msg) As String Dim pidl As Long
Dim pos As Integer
Dim path As String
Dim bi As BROWSEINFO
'Fill the BROWSEINFO structure with the needed data,
'show the browse dialog, and if the returned value
'indicates success (1), retrieve the user's
'selection contained in pidl
With bi
.hOwner = Me.hWnd
.pidlRoot = CSIDL_DESKTOP
.lpszTitle = msg
.ulFlags = BIF_RETURNONLYFSDIRS
End With pidl = SHBrowseForFolder(bi)
path = Space$(512)
If SHGetPathFromIDList(ByVal pidl, ByVal path) = 1 Then
pos = InStr(path, Chr$(0))
GetBrowseFolder = Left(path, pos - 1)
End IfEnd Function参考一下吧
实时错误"453"
找不到dll入口,SHFileOperationA in shell32.dll
With SHf
.wFunc = FO_DELETE
.pFrom = "C:\w.txt" & Chr(0)
.fFlags = FOF_ALLOWUNDO
.fFlags = &H10 Or &H100 'or &H
End With Call SHFileOperation(SHf)
请问我这么写有什么问题吗? 为什么文件删不掉呢?