ZetCode

FreeBasic Kill Keyword

last modified June 16, 2025

The FreeBasic Kill keyword is used to delete files from the filesystem. It provides a simple way to remove files programmatically. This command is powerful and should be used with caution.

Basic Definition

The Kill statement in FreeBasic permanently deletes one or more files specified by a path. The path can include wildcards (* and ?) to match multiple files. Once deleted, files cannot be recovered.

The Kill statement follows the syntax: Kill "filepath". The filepath can be absolute or relative. FreeBasic will raise an error if the file doesn't exist or can't be deleted.

Deleting a Single File

This example demonstrates how to delete a single file using the Kill statement.

kill_single.bas
' Create a temporary file for demonstration
Open "tempfile.txt" For Output As #1
Print #1, "This is a temporary file"
Close #1

Print "File exists before deletion: "; FileExists("tempfile.txt")
Kill "tempfile.txt"
Print "File exists after deletion: "; FileExists("tempfile.txt")

First, we create a temporary file and verify its existence. Then we use Kill to delete it. The FileExists function checks if the file exists before and after deletion. This shows the basic usage.

Deleting Multiple Files with Wildcards

The Kill statement supports wildcards to delete multiple files at once.

kill_wildcards.bas
' Create multiple temporary files
For i As Integer = 1 To 5
    Open "temp_" & i & ".txt" For Output As #1
    Print #1, "File number "; i
    Close #1
Next

Print "Files created: "; Dir("temp_*.txt")
Kill "temp_*.txt"
Print "Files after deletion: "; Dir("temp_*.txt")

This code creates five temporary files with numbered names. The Kill statement with the *.txt wildcard deletes all matching files. The Dir function lists files before and after deletion.

Error Handling with Kill

Since Kill can raise errors, proper error handling is important.

kill_error.bas
On Error Goto ErrorHandler

' Try to delete a non-existent file
Kill "nonexistent.txt"
Print "This line won't be reached"
Exit Sub

ErrorHandler:
Print "Error "; Err; " occurred: "; Error(Err)
Resume Next

This example shows how to handle errors when using Kill. We attempt to delete a non-existent file, which triggers an error. The error handler catches this and displays the error information. Always include error handling with Kill.

Deleting Files in a Different Directory

Kill can delete files in any directory with proper path specification.

kill_directory.bas
' Create a file in a subdirectory
MkDir "tempdir"
Open "tempdir/testfile.txt" For Output As #1
Print #1, "Test content"
Close #1

Print "File in directory exists: "; FileExists("tempdir/testfile.txt")
Kill "tempdir/testfile.txt"
Print "File after deletion: "; FileExists("tempdir/testfile.txt")

' Clean up directory
RmDir "tempdir"

Here we create a subdirectory and a file within it. We then delete the file using a relative path. Finally, we remove the empty directory. Note that Kill only deletes files, not directories.

Checking File Existence Before Deletion

It's good practice to check if a file exists before attempting to delete it.

kill_check.bas
Dim filename As String = "checkfile.txt"

' Create file only if it doesn't exist
If Not FileExists(filename) Then
    Open filename For Output As #1
    Print #1, "Sample content"
    Close #1
End If

' Safe deletion
If FileExists(filename) Then
    Kill filename
    Print "File deleted successfully"
Else
    Print "File doesn't exist"
End If

This example demonstrates defensive programming with Kill. We first check if the file exists before creating it. Then we check again before deletion. This prevents errors and makes the code more robust.

Deleting Read-Only Files

Read-only files require special handling when using Kill.

kill_readonly.bas
' Create a read-only file
Open "readonly.txt" For Output As #1
Print #1, "Read-only content"
Close #1

' Set read-only attribute
SetAttr "readonly.txt", FB_NORMAL Or FB_READONLY

On Error Goto ErrorHandler
Kill "readonly.txt"
Print "File deleted successfully"
Exit Sub

ErrorHandler:
Print "Error deleting file: "; Error(Err)
' Remove read-only attribute and try again
SetAttr "readonly.txt", FB_NORMAL
Kill "readonly.txt"
Print "File deleted after removing read-only attribute"
Resume Next

This code creates a read-only file and attempts to delete it. The first attempt fails, so we catch the error, remove the read-only attribute, and try again. This shows how to handle special file attributes with Kill.

Best Practices

This tutorial covered the FreeBasic Kill keyword with practical examples showing its usage in different scenarios. Use this powerful command responsibly.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.