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 "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
.
' 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 "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.
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.
#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.
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.
' 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.