ZetCode

FreeBasic CurDir Keyword

last modified June 16, 2025

The FreeBasic CurDir keyword returns the current working directory as a string. It's essential for file operations and path management in FreeBasic programs.

Basic Definition

In FreeBasic, CurDir is a function that returns the current directory path where the program is executing. The path is returned as a string without a trailing backslash or slash.

The current directory is important for relative file operations. It serves as the base path when opening files without full paths. CurDir works on all supported platforms (Windows, Linux, DOS).

Getting Current Directory

This basic example shows how to retrieve and display the current directory.

curdir_basic.bas
Dim currentPath As String
currentPath = CurDir()

Print "Current directory: "; currentPath

Here we call CurDir without parameters to get the current directory path. The function returns a string that we store and print. The path format depends on the operating system (backslashes or slashes).

Changing and Checking Directory

This example demonstrates changing directories and verifying the change.

curdir_change.bas
Print "Original directory: "; CurDir()

ChDir ".."  ' Move up one directory level
Print "After ChDir: "; CurDir()

ChDir "docs"  ' Move into docs subdirectory
Print "After second ChDir: "; CurDir()

We first print the original directory, then use ChDir to change it. After each change, we verify with CurDir. Note that ChDir affects the current process's working directory.

Combining with File Operations

CurDir is often used with file operations to build full paths.

curdir_files.bas
Dim filePath As String
filePath = CurDir() + "/test.txt"

Print "Attempting to create file at: "; filePath

Open filePath For Output As #1
Print #1, "This is a test file."
Close #1

Print "File created successfully"

Here we combine CurDir with a filename to create a full path. This ensures the file is created in the current directory regardless of where the program runs from. The path separator is hardcoded as forward slash.

Platform-Specific Path Handling

This example shows platform-aware path construction using CurDir.

curdir_platform.bas
Dim pathSep As String
Dim fullPath As String

#ifdef __FB_WIN32__
    pathSep = "\"
#else
    pathSep = "/"
#endif

fullPath = CurDir() + pathSep + "data" + pathSep + "config.ini"

Print "Platform-specific path: "; fullPath

We detect the platform to use the correct path separator. Then we build a path combining CurDir with subdirectories and a filename. This creates correct paths on any operating system.

Comparing Directories

This example compares the current directory with a target directory.

curdir_compare.bas
Dim targetDir As String = "/home/user/projects"
Dim currentDir As String = CurDir()

If currentDir = targetDir Then
    Print "Already in target directory"
Else
    Print "Current: "; currentDir
    Print "Target:  "; targetDir
    Print "Need to change directories"
End If

We store a target directory path and compare it with CurDir. The comparison is case-sensitive on Unix-like systems. This technique is useful for directory validation in scripts.

Error Handling

This example demonstrates error handling when working with directories.

curdir_errors.bas
On Error Goto ErrorHandler

Dim newDir As String = "/nonexistent"
ChDir newDir
Print "Current directory: "; CurDir()
End

ErrorHandler:
Print "Error "; Err; ": "; Error(Err)
Print "Could not change to "; newDir
Print "Staying in "; CurDir()

We attempt to change to a non-existent directory and handle the error. The error handler reports the issue while CurDir shows we remain in the original directory. Always check directory operations.

Relative Path Resolution

This example shows how CurDir helps resolve relative paths.

curdir_relative.bas
Print "Current directory: "; CurDir()

Dim relPath As String = "./subdir/../config"
Dim absPath As String = CurDir() + "/" + relPath

Print "Relative path: "; relPath
Print "Absolute path: "; absPath

We combine CurDir with a relative path containing parent directory references. The result shows how relative paths are interpreted relative to the current directory. This is crucial for path manipulation.

Best Practices

This tutorial covered the FreeBasic CurDir keyword with practical examples showing directory operations and path 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.