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:
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:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.Deletefile ("c:\windows\desktop\doc1.doc")
Method 3: Move the file to the recycle bin
VB Code:
'Paste this into the General Declarations section of your form/module:
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pToAs String
fFlagsAs Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Function ShellDelete(ParamArray vntFileName() As Variant) As Long
Dim I As Integer
Dim sFileNames As String
Dim SHFileOp As SHFILEOPSTRUCT
For I = LBound(vntFileName) To UBound(vntFileName)
sFileNames = sFileNames & vntFileName(I) & vbNullChar
Next
sFileNames = sFileNames & vbNullChar
With SHFileOp
.wFunc = FO_DELETE
.pFrom = sFileNames
.fFlags = FOF_ALLOWUNDO
End With
ShellDelete = SHFileOperation(SHFileOp)
End Function
'Usage:
Dim lResult as Long
'Delete a single file
lResult = ShellDelete("DELETE.ME")
'Delete several files
lResult = ShellDelete("DELETE.ME", "LOVE_LTR.DOC", "COVERUP.TXT")