CodeItBetter Programming Another VB Programming Blog

How to delete the given file (in four ways)

Posted on July 12, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'File/Folder Handling - How to delete the given file (in four ways)
'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
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.