Home > How-To Library > File/Folders Handling
How to delete the given file (in four ways)
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 'Using Kill: Sub DeleteFileUsingKill() ' All file operations should be protected against errors. ' None of these functions works on open files. On Error Resume Next ' Delete one or more files--Kill also supports wildcards. Kill "d:\temporary.*" End Sub 'Using API: Private Declare Function DeleteFile Lib "kernel32.dll" Alias "DeleteFileA" _ (ByVal lpFileName As String) As Long Sub DeleteFileUsingAPI() Dim intResult As Long ' return value intResult = DeleteFile("C:\Temp.txt") 'Doesn't go to Recycle Bin! If intResult = 1 Then MsgBox "File deleted." Else MsgBox "Encountered Error. Couldn't Delete!" End If End End Sub 'Using FSO: Public Sub FileDeleteUsingFSO(ByVal sFileName As String) 'Set a reference to "Microsoft Scripting Runtime" Dim FSO As Scripting.FileSystemObject Set FSO = New Scripting.FileSystemObject Call FSO.DeleteFile(sFileName, True) Set FSO = Nothing End Sub 'Move to Recycle Bin: http://codeitbetter.co.nr/vb6/code/Vb6Code00106.html 'Using Dos Command Del: Public Sub FileDeleteUsingDel(ByVal sFileName As String) Dim sCommand As String sCommand = "Cmd.exe /C Del /Q /F " & ChrW$(34) & sFileName & ChrW$(34) Shell sCommand End Sub
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.