FreeBasic MkDir and RmDir Keywords
Last modified October 18, 2026
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 function fails (returns -1)
if the directory is not empty, and the program must have sufficient
permissions to delete it.
Both MkDir and RmDir are functions. They return
zero (0) on success and negative one (-1) on failure; they do not raise a
runtime error that can be trapped with On Error. Checking the
return value is important when working with directories.
Creating a Simple Directory
This example demonstrates basic directory creation with MkDir.
#Include Once "dir.bi"
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.
#Include Once "dir.bi"
' 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.
#Include Once "dir.bi"
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.
#Include Once "dir.bi"
' Try to create a directory that might exist
If MkDir("existing_folder") <> 0 Then
Print "Could not create directory: existing_folder"
Else
Print "Directory created: existing_folder"
End If
RmDir "existing_folder"
This example demonstrates error handling for directory operations.
MkDir returns zero on success and negative one on failure,
so we check its return value and report the failure. The example also
cleans up the directory afterwards.
Creating Directories with Absolute Paths
MkDir can create directories using absolute paths.
#Include Once "dir.bi"
#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 is good practice to check if a directory exists before creating it.
#Include Once "dir.bi"
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 does not 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.
#Include Once "dir.bi"
' 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
- Error Handling: Always implement error handling for directory operations.
- Existence Checks: Verify directory existence before creation/removal.
- Cleanup: Remove temporary directories when no longer needed.
- Permissions: Ensure proper permissions for target directories.
- Platform Awareness: Consider path separator differences across platforms.
This tutorial covered FreeBasic's MkDir and RmDir
keywords with practical examples for directory management.
Author
List all FreeBasic Tutorials.