用什么命令可以删除某个文件夹下的一个文件

解决方案 »

  1.   

    Kill 语句
          从磁盘中删除文件。语法Kill pathname必要的 pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。说明在 Microsoft Windows 中,Kill 支持多字符 (*) 和单字符 (?) 的统配符来指定多重文件。
      

  2.   

    Kill 语句
          从磁盘中删除文件。语法Kill pathname必要的 pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。说明在 Microsoft Windows 中,Kill 支持多字符 (*) 和单字符 (?) 的统配符来指定多重文件。
    Kill 语句示例
    本示例使用 Kill 语句将磁盘中的文件删除。' 假设 TESTFILE是一数据文件。
    Kill "TestFile"   ' 删除。' 将当前目录下所有 *.TXT 文件全部删除。
    Kill "*.TXT"  
      

  3.   

    楼上的函数每一个都不好用,如果不是特殊情况可以使用,但是我强烈建议不要用vb自己带的函数,因为我试过几次有时候都没有成功
    Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    用这个删除保证不出错
    用法:DeleteFile 文件名
      

  4.   

    如果要删除的文件正在使用当中就会引起错误,所以一般要在 kill 文件之前捕捉异常
    on error goto kErr
    kill "c:\test.txt"
    exit sub
    kErr:
    msgbox "kill error"
    或者用 windows 的删除到回收站的 shell 函数Public Enum ShellFileOperaType
        FO_MOVE = &H1&   'Moves the files specified in pFrom to the location specified in pTo.
        FO_COPY = &H2&   'Copies the files specified in the pFrom member to the location specified in the pTo member.
        FO_DELETE = &H3& 'Deletes the files specified in pFrom (pTo is ignored.)
        FO_RENAME = &H4& 'Renames the files specified in pFrom.
    End EnumPrivate Const FOF_ALLOWUNDO = &H40&         'Preserve Undo information.
    Private Const FOF_CONFIRMMOUSE = &H2&       'Not currently implemented.
    Private Const FOF_CREATEPROGRESSDLG = &H0&  'handle to the parent 'window for the 'progress dialog box.
    Private Const FOF_FILESONLY = &H80&         'Perform the operation 'on files only if a 'wildcard file name '(*.*) is specified.
    Private Const FOF_MULTIDESTFILES = &H1&     'The pTo member 'specifies multiple destination files (one'for each source file) 'rather than one directory where all source files are 'to be deposited.
    Private Const FOF_NOCONFIRMATION = &H10&    'Respond with Yes to 'All for any dialog box 'that is displayed.
    Private Const FOF_NOCONFIRMMKDIR = &H200&   'Does not confirm the 'creation of a new 'directory if the 'operation requires one 'to be created.
    Private Const FOF_RENAMEONCOLLISION = &H8&  'Give the file being 'operated on a new name 'in a move, copy, or 'rename operation if a 'file with the target 'name already exists.
    Private Const FOF_SILENT = &H4&             'Does not display a 'progress dialog box.
    Private Const FOF_SIMPLEPROGRESS = &H100&   'Displays a progress 'dialog box but does 'not show the 'file names.
    Private Const FOF_WANTMAPPINGHANDLE = &H20& 'If FOF_RENAMEONCOLLISION is specified, 'the hNameMappings member will be filled 'in if any files were renamed.
                              
    '//The SHFILOPSTRUCT is not double-word aligned. If no steps are
    '//taken, the last 3 variables will not be passed correctly. This
    '//has no impact unless the progress title needs to be changed.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 Declare Sub CopyMemory Lib "kernel32" _
        Alias "RtlMoveMemory" _
        (hpvDest As Any, _
        hpvSource As Any, _
        ByVal cbCopy As Long)Private Declare Function SHFileOperation Lib "shell32.dll" _
        Alias "SHFileOperationA" _
        (lpFileOp As Any) As Long
    Public Function DeleteFile(sfiles() As String, Optional ByVal hWnd As Long = 0) As Boolean
    '//delete file to recycbin
    '// !!! param "sfiles()" is 0-base array
    '// !!! shell operation is not allow the "\" at the end of filepath
        Dim desFiles(1)  As String
        DeleteFile = CopyFile(sfiles(), desFiles(), hWnd, FO_DELETE, False, "")
    End Function
    Public Function CopyFile(srcFiles() As String, desFiles() As String, _
                             Optional ByVal hWnd As Long = 0, _
                             Optional bOpera As ShellFileOperaType = FO_COPY, _
                             Optional bUseWildchar As Boolean = False, _
                             Optional sDlgTitle As String = "") As Boolean
    '// !!! param "srcFiles()" is 0-base array
    '// !!! param "desFiles()" is 0-base array
    '// !!! shell operation is not allow the "\" at the end of filepath    Dim result      As Long
        Dim lenFileop   As Long
        Dim foBuf()     As Byte
        
        Dim fileop      As SHFILEOPSTRUCT
        
        lenFileop = LenB(fileop)    ' double word alignment increase
        ReDim foBuf(1 To lenFileop) ' the size of the structure.
        
        Dim sFrom       As String
        Dim sTarget     As String
        
        '//make the source file string
        Dim i           As Long
        For i = 0 To UBound(srcFiles) - 1
            sFrom = sFrom & srcFiles(i) & vbNullChar
        Next
        sFrom = sFrom & vbNullChar
        
        '//make the destin file string
        For i = 0 To UBound(desFiles) - 1
            sTarget = sTarget & desFiles(i) & vbNullChar
        Next
        sTarget = sTarget & vbNullChar
        
        
        '//fill structure
        With fileop
            .hWnd = hWnd
        
            .wFunc = bOpera
            .pFrom = sFrom
            .pTo = sTarget
            
            If bOpera = FO_DELETE Or bOpera = FO_MOVE Or bOpera = FO_RENAME Then .fFlags = FOF_ALLOWUNDO
            
            If Len(sDlgTitle) > 0 Then
                .fFlags = .fFlags Or FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_NOCONFIRMMKDIR
                .lpszProgressTitle = sDlgTitle & vbNullChar & vbNullChar
            End If
            
            If bUseWildchar Then .fFlags = .fFlags Or FOF_FILESONLY
        End With
        
        '//Now we need to copy the structure into a byte array
        Call CopyMemory(foBuf(1), fileop, lenFileop)
        
        '//Next we move the last 12 bytes by 2 to byte align the data
        Call CopyMemory(foBuf(19), foBuf(21), 12)
        result = SHFileOperation(foBuf(1))
        
        If result <> 0 Then
            '//MsgBox Err.LastDllError '//Operation failed
        Else
            If fileop.fAnyOperationsAborted <> 0 Then
                '//MsgBox "Operation Failed"
            Else
                CopyFile = True
            End If
        End If
        
    End Function