How do you delete files using VB???
Thanx for any help!!
Printable View
How do you delete files using VB???
Thanx for any help!!
you use the kill statement ... like:
kill "c:\filename.exe"
Thats what I was trying but I had one slight problem:
I did this:
I thought it was a boolean function which would say if it worked or if it failed.Code:Msgbox FileSystem.Kill ("C:\Temp123456789.txt")
If it didn't work it will return an error.
You would have to do something like this:
Code:On Error Goto ErrHandle
Kill ("C:\Temp123456789.txt")
Exit Sub
ErrHandle:
Msgbox "There was an error."
This thread may be of interest to you as well.
You can also use the API to get some extra effects....
Also a MessageBox will be automatically displayed if you try to delete a file that is currently in use.Code:
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 Long
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40 ' if this flag included files will be sent to the recycling bin
Private Const FOF_SILENT = &H4 ' if this flag is included then the progress screen will not be displayed
Private Const FOF_NOCONFIRMATION = &H10 ' if this flag is included then the user will not be asked to confirm the files to be deleted
Public Sub cmdDelete_Click()
Dim lRet As Long
Dim fFileStruct As SHFILEOPSTRUCT
With fFileStruct
.wFunc = FO_DELETE
' You can also delete multiple files by passing a string variable in this format "C:\Windows\text.txt" & vbNullChar & "C:\Windows\text2.txt"
.pFrom = "C:\Windows\Text.txt"
'Include next line of code if you want to send files to the recycling bin
'.fFlags = .fFlags Or FOF_ALLOWUNDO
'Include the next line of code if you do not want the progress of the files being deleted
'.fFlags = .fFlags Or FOF_SILENT
'Include the next line of code if you want a screen that says "Are you sure you want to delete this file" to come up
'.fFlags = .fFlags Or FOF_NOCONFIRMATION
End With
lRet = SHFileOperation(fFileStruct)
if lRet = 0 then
MsgBox "File(s) not deleted"
end if
End Sub
[Edited by YoungBuck on 11-25-2000 at 05:29 PM]