Delete A File - Snippet


Below is a very small VB6 code snippet. It allows you to delete a file from the hard drive. To try out this Visual Basic code snippet first go to your hard drive and create a new text file on the C drive. Call the file test.txt and fill it with some junk data. Now create a new Visual Basic program. On the main form add a command button. Double click the command button to get to the click event code. Add the following inside the click event handler

  1. Dim Filename As String
  2. Dim Answer
  3. Filename = "C:\Test.txt"
  4. Answer = MsgBox("Are you sure you want to delete " & Filename, vbYesNo)
  5. If Answer = vbYes Then
  6.     Kill Filename
  7. End If

Obviously in a real world application you would not hard code in the file name but for simplicity sake we did so here. They key to getting rid of the file is the Kill Filename command. This is left over from the old original dos days but it still works today.

This was a very basic example to give you a taste of what VB6 allows you to do in relation to File handling. We also have a whole list of VB6 File Handling Tutorials located on this site. Feel free to check them out to learn much more about dealing with files in VB.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

you should try this Kill

you should try this

Kill "(filename+extension)"

example : Kill "vb6.exe"

hope that thing works

I always wrap my Kill

I always wrap my Kill commands in on "error resume next / on error goto 0" blocks. There's always the chance the file may get deleted before your kill command can execute.

There is another one....

There is another one.... It's more simple than this code.

I thought it was Kill

I thought it was Kill "filename" right?

Delete A File - Snippet

I try this code but there's an error..

Kill Filename is that a command?? because when i try the code above the Kill Filename is error