ZetCode

FreeBasic ChDir Keyword

last modified June 16, 2025

The FreeBasic ChDir keyword changes the current working directory. It allows programs to navigate the file system and access files in different locations. This is essential for file operations in specific directories.

Basic Definition

ChDir is a FreeBasic statement that changes the current working directory to the specified path. The path can be absolute or relative to the current directory. It affects subsequent file operations in the program.

The statement requires a valid directory path string. If the path doesn't exist, a runtime error occurs. Always verify directory existence before using ChDir for robust programs.

Changing to an Absolute Directory

This example demonstrates changing to an absolute directory path.

chdir_absolute.bas
Print "Current directory: "; CurDir

' Change to an absolute path
ChDir "C:\Users\Public\Documents"

Print "New directory: "; CurDir

The code first prints the current directory using CurDir. Then it changes to a specific absolute path using ChDir. Finally, it verifies the change by printing the new current directory.

Changing to a Relative Directory

This example shows how to navigate using relative paths.

chdir_relative.bas
Print "Current directory: "; CurDir

' Change to a subdirectory
ChDir "subfolder"

Print "After moving down: "; CurDir

' Move back up
ChDir ".."

Print "After moving up: "; CurDir

The program first moves into a subdirectory called "subfolder". Then it moves back to the parent directory using "..". Relative paths are useful when you know the directory structure relative to your program.

Error Handling with ChDir

This example demonstrates proper error handling when changing directories.

chdir_error.bas
On Error Goto ErrorHandler

Dim newPath As String = "C:\nonexistent\folder"

Print "Attempting to change to: "; newPath
ChDir newPath
Print "Successfully changed directory"

Exit Sub

ErrorHandler:
Print "Error: "; Err
Print "Could not change to directory: "; newPath

The code attempts to change to a non-existent directory. The On Error statement catches any runtime errors. The error handler displays the error number and a descriptive message about the failure.

ChDir with User Input

This example shows interactive directory changing based on user input.

chdir_interactive.bas
Print "Current directory: "; CurDir
Input "Enter directory path to change to: ", path$

On Error Goto InputError
ChDir path$
Print "Successfully changed to: "; CurDir
Exit Sub

InputError:
Print "Invalid directory: "; path$

The program prompts the user for a directory path. It then attempts to change to that directory. If successful, it confirms the change. If not, it displays an error message. This demonstrates dynamic directory navigation.

ChDir in Combination with File Operations

This example shows practical use of ChDir with file operations.

chdir_fileops.bas
' Save original directory
Dim originalDir As String = CurDir

' Change to target directory
ChDir "C:\Temp"

' Create and write to a file in the new directory
Open "testfile.txt" For Output As #1
Print #1, "This file was created after ChDir"
Close #1

' Return to original directory
ChDir originalDir

Print "File created in: C:\Temp\testfile.txt"
Print "Restored to original directory: "; CurDir

The code first saves the original directory. It then changes to a temporary directory and creates a file there. Finally, it returns to the original directory. This pattern is useful for temporary directory changes.

ChDir with Multiple Directory Levels

This example demonstrates navigating through multiple directory levels.

chdir_multilevel.bas
Print "Starting directory: "; CurDir

' Change to first level
ChDir "subfolder1"
Print "Level 1: "; CurDir

' Change to second level
ChDir "subfolder2"
Print "Level 2: "; CurDir

' Jump back to starting point
ChDir "..\.."
Print "Back to start: "; CurDir

The program navigates down two directory levels, printing the current directory at each step. Then it uses a relative path with multiple ".." to return to the starting directory in one operation.

ChDir with Network Paths

This example shows how to work with network shared folders.

chdir_network.bas
On Error Goto NetError

Dim networkPath As String = "\\server\share\folder"

Print "Attempting to access network path..."
ChDir networkPath
Print "Successfully changed to network directory: "; CurDir

' Perform file operations here...

Exit Sub

NetError:
Print "Failed to access network path: "; networkPath
Print "Error: "; Err

The code attempts to change to a network shared folder. Network paths require proper permissions and connectivity. The example includes error handling for cases where the network resource is unavailable.

Best Practices

This tutorial covered the FreeBasic ChDir keyword with practical examples showing directory navigation in different scenarios.

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.