CurDir$ Function
Returns a String representing the current path for the specified drive or the default drive.
The dollar sign suffix ($) explicitly indicates that this function returns a String type
(not a Variant).
Syntax
CurDir$[(drive)]
Parameters
drive: Optional.Stringexpression that specifies an existing drive. If no drive is specified or if drive is a zero-length string (""),CurDir$returns the path for the current drive. The drive parameter can be just the drive letter (e.g., "C") or include a colon (e.g., "C:").
Return Value
Returns a String containing the current directory path for the specified drive. The returned
path does not include a trailing backslash unless the current directory is the root directory.
The return value is always a String type (never Variant).
Remarks
- The
CurDir$function always returns aString, whileCurDir(without$) can return aVariant. - Without arguments, returns current directory of current drive.
- With drive specified, returns current directory of that drive.
- Does not include trailing backslash (except for root directory).
- Drive parameter is case-insensitive ("C" and "c" are equivalent).
- Each drive maintains its own current directory in Windows.
- On Windows, returns full path (e.g., "C:\Windows\System32").
- Root directory returns drive with backslash (e.g., "C:\").
- For better performance when you know the result is a string, use
CurDir$instead ofCurDir.
Drive Specification
The drive parameter can be specified in several ways:
- CurDir$() - Current drive
- CurDir$("") - Current drive
- CurDir$("C") - Drive C
- CurDir$("C:") - Drive C
- CurDir$("D") - Drive D
Typical Uses
- Directory context - Determine current working directory
- Path building - Construct full paths from relative paths
- Directory restoration - Save and restore directory state
- File operations - Locate files relative to current directory
- Logging - Record current directory for debugging
- Validation - Verify expected working directory
- Multi-drive operations - Work with multiple drives simultaneously
Basic Examples
' Example 1: Get current directory
Dim currentDir As String
currentDir = CurDir$()
' Example 2: Get current directory of specific drive
Dim cDrive As String
cDrive = CurDir$("C")
' Example 3: Check if in expected directory
If CurDir$() = "C:\MyApp" Then
MsgBox "In correct directory"
End If
' Example 4: Display current directory
MsgBox "Current directory: " & CurDir$()
Common Patterns
Save and Restore Directory
Sub ProcessInDifferentDirectory(targetDir As String)
Dim savedDir As String
' Save current directory
savedDir = CurDir$()
On Error GoTo ErrorHandler
' Change to target directory
ChDir targetDir
' Do work in target directory
ProcessFiles
' Restore original directory
ChDir savedDir
Exit Sub
ErrorHandler:
' Always restore directory even on error
ChDir savedDir
Err.Raise Err.Number, , Err.Description
End Sub
Building Full Path from Relative Path
Function GetFullPath(relativePath As String) As String
Dim currentDir As String
currentDir = CurDir$()
' Check if current dir already ends with backslash
If Right$(currentDir, 1) = "\" Then
GetFullPath = currentDir & relativePath
Else
GetFullPath = currentDir & "\" & relativePath
End If
End Function
Ensuring Correct Working Directory
Sub EnsureWorkingDirectory(expectedDir As String)
Dim currentDir As String
currentDir = CurDir$()
If UCase$(currentDir) <> UCase$(expectedDir) Then
ChDir expectedDir
End If
End Sub
Multi-Drive Directory Tracking
Function GetAllDriveDirs() As Collection
Dim drives() As String
Dim result As New Collection
Dim i As Integer
drives = Array("C", "D", "E", "F")
For i = LBound(drives) To UBound(drives)
On Error Resume Next
result.Add CurDir$(drives(i)), drives(i)
On Error GoTo 0
Next i
Set GetAllDriveDirs = result
End Function
Path Validation
Function IsValidDirectory(dirPath As String) As Boolean
Dim savedDir As String
Dim result As Boolean
savedDir = CurDir$()
result = False
On Error Resume Next
ChDir dirPath
If Err.Number = 0 Then
result = True
ChDir savedDir
End If
On Error GoTo 0
IsValidDirectory = result
End Function
Log File Path Construction
Function GetLogFilePath() As String
Dim logDir As String
Dim logFile As String
logDir = CurDir$()
logFile = "application_" & Format$(Now, "yyyymmdd") & ".log"
If Right$(logDir, 1) = "\" Then
GetLogFilePath = logDir & logFile
Else
GetLogFilePath = logDir & "\" & logFile
End If
End Function
Working Directory Report
Sub ReportCurrentDirectories()
Dim report As String
report = "Current Directory: " & CurDir$() & vbCrLf
On Error Resume Next
report = report & "C: Drive: " & CurDir$("C") & vbCrLf
report = report & "D: Drive: " & CurDir$("D") & vbCrLf
On Error GoTo 0
MsgBox report, vbInformation, "Directory Report"
End Sub
Relative File Search
Function FindFileInCurrentDir(filename As String) As String
Dim fullPath As String
Dim currentDir As String
currentDir = CurDir$()
If Right$(currentDir, 1) = "\" Then
fullPath = currentDir & filename
Else
fullPath = currentDir & "\" & filename
End If
If Dir$(fullPath) <> "" Then
FindFileInCurrentDir = fullPath
Else
FindFileInCurrentDir = ""
End If
End Function
Directory Depth Calculator
Function GetDirectoryDepth() As Integer
Dim path As String
Dim depth As Integer
Dim i As Integer
path = CurDir$()
depth = 0
For i = 1 To Len(path)
If Mid$(path, i, 1) = "\" Then
depth = depth + 1
End If
Next i
GetDirectoryDepth = depth - 1 ' Subtract 1 for root backslash
End Function
Safe Directory Operation Wrapper
Function ExecuteInDirectory(dirPath As String, operation As String) As Boolean
Dim savedDir As String
Dim success As Boolean
savedDir = CurDir$()
success = False
On Error GoTo ErrorHandler
' Change to target directory
ChDir dirPath
' Execute operation based on parameter
Select Case operation
Case "CLEANUP"
DeleteTempFiles
Case "BACKUP"
BackupFiles
Case "SCAN"
ScanFiles
End Select
success = True
ErrorHandler:
' Always restore directory
ChDir savedDir
ExecuteInDirectory = success
End Function
Related Functions
CurDir: Returns current directory asVariantinstead ofStringChDir: Changes the current directoryChDrive: Changes the current driveApp.Path: Returns the path where the application executable is locatedDir$: Returns files or directories matching a patternMkDir: Creates a new directoryRmDir: Removes an empty directory
Best Practices
- Always save current directory before changing it
- Use error handling when changing directories
- Restore original directory in error handlers
- Use
App.Pathfor application-relative paths instead of relying on current directory - Check for trailing backslash when building paths
- Use
CurDir$instead ofCurDirfor better performance - Be aware that current directory can be changed by other code
- Document assumptions about current directory in your code
- Use absolute paths when possible to avoid directory dependencies
- Test code with different working directories
Performance Considerations
CurDir$is slightly more efficient thanCurDirbecause it avoidsVariantoverhead- Directory queries are fast system calls
- Cache the result if you need to use it multiple times in quick succession
- Avoid excessive directory changes as they affect all operations
Platform Notes
- Windows maintains separate current directories for each drive
- Network paths (UNC paths) are supported (e.g., "\\server\share\folder")
- Long path names (> 260 characters) may cause issues in VB6
- Current directory is process-specific and thread-safe
- Some operations (like file dialogs) may change the current directory
Error Conditions
- If the specified drive does not exist, an error occurs (Error 68: Device unavailable)
- If the drive is not ready (e.g., no disk in drive), an error occurs (Error 71: Disk not ready)
- Network drives that are disconnected will cause errors
Security Considerations
- Don't assume the current directory for security-sensitive operations
- Use absolute paths for configuration and data files
- Be aware that current directory can be manipulated by attackers
- Validate directory paths before changing to them
- Use
App.Pathfor application resources rather than current directory
Limitations
- Cannot set the current directory (use
ChDirfor that) - Path is limited to
MAX_PATHcharacters (typically 260) in VB6 - Does not resolve symbolic links or junctions
- Drive letter parameter is Windows-specific