Last modified April 20, 2026
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.
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 does not
exist, a runtime error occurs. Always verify directory existence before using
ChDir for robust programs.
This example demonstrates changing to an absolute directory path.
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.
This example shows how to navigate using relative paths.
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.
This example demonstrates proper error handling when changing directories.
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.
This example shows interactive directory changing based on user input.
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.
This example shows practical use of ChDir with file operations.
' 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.
This example demonstrates navigating through multiple directory levels.
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.
This example shows how to work with network shared folders.
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.
This tutorial covered the FreeBasic ChDir keyword with practical
examples showing directory navigation in different scenarios.
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.