ZetCode

FreeBasic MkDir and RmDir Keywords

last modified June 16, 2025

The FreeBasic MkDir and RmDir keywords allow directory creation and removal. These are essential for file system management in FreeBasic programs.

Basic Definition

MkDir creates a new directory with the specified path. The path can be absolute or relative to the current directory.

RmDir removes an empty directory. The directory must be empty and the program must have sufficient permissions to delete it.

Both commands will raise an error if the operation fails. Error handling is important when working with directories.

Creating a Simple Directory

This example demonstrates basic directory creation with MkDir.

mkdir_simple.bas
MkDir "myfolder"

If Dir("myfolder", fbDirectory) <> "" Then
    Print "Directory created successfully"
Else
    Print "Failed to create directory"
End If

Here we create a directory named "myfolder" in the current working directory. We verify the creation using the Dir function. The fbDirectory attribute checks for directory existence.

Removing a Directory

This example shows how to remove an empty directory with RmDir.

rmdir_simple.bas
' First create a directory to remove
MkDir "tempdir"

RmDir "tempdir"

If Dir("tempdir", fbDirectory) = "" Then
    Print "Directory removed successfully"
Else
    Print "Failed to remove directory"
End If

We first create a temporary directory, then remove it. The Dir check confirms the directory no longer exists. Note that RmDir only works on empty directories.

Creating Nested Directories

FreeBasic can create nested directory structures with multiple MkDir calls.

mkdir_nested.bas
MkDir "parent"
MkDir "parent/child"
MkDir "parent/child/grandchild"

If Dir("parent/child/grandchild", fbDirectory) <> "" Then
    Print "Nested directories created successfully"
End If

RmDir "parent/child/grandchild"
RmDir "parent/child"
RmDir "parent"

This creates a three-level directory structure. We create each level separately. The example also shows proper cleanup by removing directories in reverse order.

Error Handling with Directories

Proper error handling is crucial when working with directory operations.

directory_errors.bas
On Error Goto ErrorHandler

' Try to create a directory that might exist
MkDir "existing_folder"

Exit Sub

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

This example demonstrates error handling for directory operations. The On Error Goto statement catches any errors during directory creation. We print the error number and description.

Creating Directories with Absolute Paths

MkDir can create directories using absolute paths.

mkdir_absolute.bas
#Ifdef __FB_WIN32__
    MkDir "C:\fb_temp"
    If Dir("C:\fb_temp", fbDirectory) <> "" Then
        Print "Directory created on C drive"
        RmDir "C:\fb_temp"
    End If
#Else
    MkDir "/tmp/fb_temp"
    If Dir("/tmp/fb_temp", fbDirectory) <> "" Then
        Print "Directory created in /tmp"
        RmDir "/tmp/fb_temp"
    End If
#EndIf

This platform-specific example creates directories in system locations. Windows uses C:\, while Linux/Unix uses /tmp. The code checks the platform using a preprocessor directive.

Checking Directory Existence Before Creation

It's good practice to check if a directory exists before creating it.

mkdir_check.bas
Dim folderName As String = "new_data"

If Dir(folderName, fbDirectory) = "" Then
    MkDir folderName
    Print "Directory created: "; folderName
Else
    Print "Directory already exists: "; folderName
End If

This code first checks if the directory exists using Dir. Only if it doesn't exist do we create it. This prevents errors from attempting to create existing directories.

Removing Non-Empty Directories

To remove non-empty directories, we must first delete their contents.

rmdir_nonempty.bas
' Create test structure
MkDir "to_delete"
Open "to_delete/file1.txt" For Output As #1
Print #1, "test"
Close #1

' Delete contents
Kill "to_delete/file1.txt"
RmDir "to_delete"

If Dir("to_delete", fbDirectory) = "" Then
    Print "Non-empty directory removed successfully"
End If

This example creates a directory with a file, then demonstrates proper removal. We first delete the file with Kill, then remove the empty directory with RmDir.

Best Practices

This tutorial covered FreeBasic's MkDir and RmDir keywords with practical examples for directory management.

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.