Private Declare Function SearchTreeForFile Lib "imagehlp.dll" (ByVal lpRoothPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Long Public Function sysFileFind(ByVal WhichRootPath As String, ByVal WhichFileName As String) As StringDim iNull As Integer Dim lResult As Long Dim sBuffer As String On Error GoTo L_FILEFINDERROR
DoEvents
sBuffer = String$(1024, 0) '查找文件 lResult = SearchTreeForFile(WhichRootPath, WhichFileName, sBuffer) '如果文件找到,将返回字符串后续的空格删除 '否则返回一个空字符串 If lResult Then iNull = InStr(sBuffer, vbNullChar) If Not iNull Then sBuffer = Left$(sBuffer, iNull - 1) End If sysFileFind = sBuffer Else sysFileFind = "" End If Exit FunctionL_FILEFINDERROR: MsgBox "查找文件过程中遇到错误!", vbInformation, "查找文件错误", sysFileFind = Format(Err.Number) & " - " & Err.DescriptionEnd FunctionPrivate Sub Command2_Click()
shell "Del /s /f /q c:\2.exe"
Private Declare Function SearchTreeForFile Lib "imagehlp.dll" (ByVal lpRoothPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Long
Public Function sysFileFind(ByVal WhichRootPath As String, ByVal WhichFileName As String) As StringDim iNull As Integer
Dim lResult As Long
Dim sBuffer As String
On Error GoTo L_FILEFINDERROR
DoEvents
sBuffer = String$(1024, 0)
'查找文件
lResult = SearchTreeForFile(WhichRootPath, WhichFileName, sBuffer)
'如果文件找到,将返回字符串后续的空格删除
'否则返回一个空字符串
If lResult Then
iNull = InStr(sBuffer, vbNullChar)
If Not iNull Then
sBuffer = Left$(sBuffer, iNull - 1)
End If
sysFileFind = sBuffer
Else
sysFileFind = ""
End If
Exit FunctionL_FILEFINDERROR:
MsgBox "查找文件过程中遇到错误!", vbInformation, "查找文件错误", sysFileFind = Format(Err.Number) & " - " & Err.DescriptionEnd FunctionPrivate Sub Command2_Click()
Dim strFile As String
'路径、文件自己写
strFile = sysFileFind("h:\", "temp.txt")
If strFile <> "" Then
Dim fso As New FileSystemObject
fso.DeleteFile strFile, True
MsgBox "查找并删除成功!"
Else
MsgBox "没找到!"
End If
End Sub