Results 1 to 1 of 1

Thread: Classic VB - How can I delete a file?

Threaded View

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Classic VB - How can I delete a file?

    Method 1: Use the built-in VB command Kill, note that this permananently deletes the file.
    VB Code:
    1. Kill "C:\Your_Folder\Yourfile.EXE"

    Method 2: Using the File System Object (an external reference); note that this also permanently deletes the file.
    VB Code:
    1. Dim fso As Object
    2. Set fso = CreateObject("Scripting.FileSystemObject")
    3. fso.Deletefile ("c:\windows\desktop\doc1.doc")

    Method 3: Move the file to the recycle bin
    VB Code:
    1. 'Paste this into the General Declarations section of your form/module:
    2. Private Type SHFILEOPSTRUCT
    3.     hWnd As Long
    4.     wFunc As Long
    5.     pFrom As String
    6.     pToAs String
    7.     fFlagsAs Integer
    8.     fAborted As Boolean
    9.     hNameMaps As Long
    10.     sProgress As String
    11. End Type
    12. Private Const FO_DELETE = &H3
    13. Private Const FOF_ALLOWUNDO = &H40
    14.  
    15. Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
    16.  "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    17.  
    18. Public Function ShellDelete(ParamArray vntFileName() As Variant) As Long
    19.     Dim I As Integer
    20.     Dim sFileNames As String
    21.     Dim SHFileOp As SHFILEOPSTRUCT
    22.  
    23.     For I = LBound(vntFileName) To UBound(vntFileName)
    24.         sFileNames = sFileNames & vntFileName(I) & vbNullChar
    25.     Next
    26.     sFileNames = sFileNames & vbNullChar
    27.  
    28.     With SHFileOp
    29.         .wFunc = FO_DELETE
    30.         .pFrom = sFileNames
    31.         .fFlags = FOF_ALLOWUNDO
    32.     End With
    33.     ShellDelete = SHFileOperation(SHFileOp)
    34. End Function
    35.  
    36.  
    37.     'Usage:
    38. Dim lResult as Long
    39.      'Delete a single file
    40.   lResult = ShellDelete("DELETE.ME")
    41.      'Delete several files
    42.   lResult = ShellDelete("DELETE.ME", "LOVE_LTR.DOC", "COVERUP.TXT")
    Last edited by si_the_geek; Jul 19th, 2005 at 05:40 PM. Reason: added explanation & tidied up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width