我想用命令按钮来实现查询1.txt和2.exe文件查到文件就立即删除无论是隐藏还是怎么都直接删除,记录显示在文件框里。请问这段代码应该怎么写??

解决方案 »

  1.   

    shell "Del /s /f /q c:\1.txt"
    shell "Del /s /f /q c:\2.exe"
      

  2.   


    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