CurDir Function
Returns a String representing the current path for the specified drive or the default drive.
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 (""),CurDirreturns 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.
Remarks
The CurDir function returns the current working directory for a specified drive. This is
useful for:
- Determining the current directory before changing it
- Building relative file paths
- Saving and restoring directory context
- File path validation
- Log file location determination
Important Characteristics:
- 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
- Each drive maintains its own current directory
- On Windows, returns full path (e.g., "C:\Windows\System32")
- Root directory returns drive with backslash (e.g., "C:\")
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
Examples
Basic Usage
' Get current directory of current drive
Dim currentDir As String
currentDir = CurDir() ' Returns something like "C:\Users\Username\Documents"
' Get current directory of specific drive
Dim cDrive As String
cDrive = CurDir("C") ' Returns current directory on C: drive
Dim dDrive As String
dDrive = CurDir("D:") ' Returns current directory on D: drive
Save and Restore Directory
Sub ProcessInDifferentDirectory(targetDir As String)
Dim savedDir As String
' Save current directory
savedDir = CurDir()
' Change to target directory
ChDir targetDir
' Do work in target directory
ProcessFiles
' Restore original directory
ChDir savedDir
End Sub
Building Relative Paths
Function GetFullPath(relativePath As String) As String
' Combine current directory with relative path
If Right(CurDir(), 1) = "\" Then
GetFullPath = CurDir() & relativePath
Else
GetFullPath = CurDir() & "\" & relativePath
End If
End Function
Common Patterns
Check if at Root Directory
Function IsRootDirectory() As Boolean
Dim currentPath As String
currentPath = CurDir()
' Root directory ends with backslash (e.g., "C:\")
IsRootDirectory = (Len(currentPath) = 3 And Right(currentPath, 1) = "\")
End Function
Get Current Drive Letter
Function GetCurrentDrive() As String
Dim currentPath As String
currentPath = CurDir()
' Extract drive letter (first character)
GetCurrentDrive = Left(currentPath, 1)
End Function
Ensure Trailing Backslash
Function EnsureTrailingBackslash(path As String) As String
If Right(path, 1) <> "\" Then
EnsureTrailingBackslash = path & "\"
Else
EnsureTrailingBackslash = path
End If
End Function
' Usage
Dim dirPath As String
dirPath = EnsureTrailingBackslash(CurDir())
Directory Context Manager
Type DirectoryContext
SavedDirectory As String
End Type
Function PushDirectory(newDir As String) As DirectoryContext
Dim ctx As DirectoryContext
ctx.SavedDirectory = CurDir()
ChDir newDir
PushDirectory = ctx
End Function
Sub PopDirectory(ctx As DirectoryContext)
ChDir ctx.SavedDirectory
End Sub
' Usage
Dim ctx As DirectoryContext
ctx = PushDirectory("C:\Temp")
' Do work...
PopDirectory ctx
Multi-Drive Path Tracking
Function GetAllDrivePaths() As Collection
Dim paths As New Collection
Dim drives() As String
Dim i As Integer
drives = Array("C", "D", "E", "F")
On Error Resume Next
For i = LBound(drives) To UBound(drives)
paths.Add CurDir(drives(i)), drives(i)
Next i
On Error GoTo 0
Set GetAllDrivePaths = paths
End Function
Log File in Current Directory
Function GetLogFilePath() As String
Dim currentDir As String
Dim logFile As String
currentDir = CurDir()
logFile = "application.log"
If Right(currentDir, 1) = "\" Then
GetLogFilePath = currentDir & logFile
Else
GetLogFilePath = currentDir & "\" & logFile
End If
End Function
Temporary Directory Operations
Sub ProcessInTempDirectory()
Dim originalDir As String
Dim tempDir As String
originalDir = CurDir()
tempDir = Environ("TEMP")
On Error GoTo Cleanup
ChDir tempDir
' Process files in temp directory
ProcessTempFiles
Cleanup:
ChDir originalDir
End Sub
Validate Relative Path
Function IsRelativePath(path As String) As Boolean
' Check if path is relative (doesn't start with drive letter)
IsRelativePath = (InStr(path, ":") = 0)
End Function
Function ResolveRelativePath(relativePath As String) As String
If IsRelativePath(relativePath) Then
ResolveRelativePath = CurDir() & "\" & relativePath
Else
ResolveRelativePath = relativePath
End If
End Function
Directory Breadcrumb Trail
Function GetDirectoryParts() As String()
Dim currentDir As String
Dim parts() As String
currentDir = CurDir()
' Remove drive letter and colon
If InStr(currentDir, ":") > 0 Then
currentDir = Mid(currentDir, 3)
End If
' Remove leading backslash
If Left(currentDir, 1) = "\" Then
currentDir = Mid(currentDir, 2)
End If
' Split by backslash
If Len(currentDir) > 0 Then
parts = Split(currentDir, "\")
End If
GetDirectoryParts = parts
End Function
Advanced Usage
Cross-Drive File Operations
Sub CopyFileToAnotherDrive(sourceFile As String, targetDrive As String)
Dim sourceDrive As String
Dim targetPath As String
' Get current directory on target drive
On Error Resume Next
targetPath = CurDir(targetDrive)
If Err.Number = 0 Then
' Build target file path
If Right(targetPath, 1) <> "\" Then
targetPath = targetPath & "\"
End If
FileCopy sourceFile, targetPath & Dir(sourceFile)
End If
End Sub
Directory Stack Implementation
Private dirStack As Collection
Sub InitDirectoryStack()
Set dirStack = New Collection
End Sub
Sub PushDir(Optional newDir As String)
If dirStack Is Nothing Then InitDirectoryStack
' Save current directory
dirStack.Add CurDir()
' Change to new directory if specified
If Len(newDir) > 0 Then
ChDir newDir
End If
End Sub
Sub PopDir()
If dirStack Is Nothing Then Exit Sub
If dirStack.Count = 0 Then Exit Sub
' Restore previous directory
ChDir dirStack(dirStack.Count)
dirStack.Remove dirStack.Count
End Sub
Smart Path Concatenation
Function CombinePaths(ParamArray paths() As Variant) As String
Dim result As String
Dim i As Integer
Dim part As String
If UBound(paths) < LBound(paths) Then
' No paths provided, return current directory
CombinePaths = CurDir()
Exit Function
End If
result = CStr(paths(LBound(paths)))
For i = LBound(paths) + 1 To UBound(paths)
part = CStr(paths(i))
' Remove leading backslash from part
If Left(part, 1) = "\" Then part = Mid(part, 2)
' Add backslash if needed
If Right(result, 1) <> "\" Then result = result & "\"
result = result & part
Next i
CombinePaths = result
End Function
Error Handling
Function GetCurrentDirectorySafe(Optional drive As String = "") As String
On Error GoTo ErrorHandler
If Len(drive) = 0 Then
GetCurrentDirectorySafe = CurDir()
Else
GetCurrentDirectorySafe = CurDir(drive)
End If
Exit Function
ErrorHandler:
Select Case Err.Number
Case 68 ' Device unavailable
MsgBox "Drive " & drive & " is not available.", vbExclamation
Case 71 ' Disk not ready
MsgBox "Drive " & drive & " is not ready.", vbExclamation
Case Else
MsgBox "Error getting current directory: " & Err.Description, vbCritical
End Select
GetCurrentDirectorySafe = ""
End Function
Common Errors
- Error 68 (Device unavailable): Specified drive does not exist
- Error 71 (Disk not ready): Drive exists but is not ready (e.g., no CD in drive)
- Error 5 (Invalid procedure call): Invalid drive specification
Performance Considerations
CurDiris a fast function with minimal overhead- Results can be cached if directory won't change during execution
- Accessing network drives may have latency
- For frequently used paths, cache the result in a variable
Best Practices
Always Restore Directory
Sub SafeDirectoryOperation()
Dim savedDir As String
savedDir = CurDir()
On Error GoTo Cleanup
' Change directory and do work
ChDir "C:\Temp"
ProcessFiles
Cleanup:
ChDir savedDir
End Sub
Use Absolute Paths When Possible
' Instead of relying on current directory:
Open "data.txt" For Input As #1 ' Depends on CurDir
' Use absolute paths:
Open "C:\MyApp\Data\data.txt" For Input As #1 ' Explicit path
Validate Drive Before Use
Function IsDriveAvailable(drive As String) As Boolean
On Error Resume Next
Dim test As String
test = CurDir(drive)
IsDriveAvailable = (Err.Number = 0)
On Error GoTo 0
End Function
Platform Considerations
- Windows: Returns paths with backslashes (e.g., "C:\Windows")
- Drive letters: Windows-specific concept
- Network paths:
UNCpaths (\server\share) not supported byCurDir - Long paths: Paths longer than 260 characters may cause issues
- Case sensitivity: Windows file system is case-insensitive
Limitations
- Returns only local drive paths, not
UNCnetwork paths - Cannot set the current directory (use
ChDirfor that) - Drive must be available and ready
- Does not validate that the returned path still exists
- Each drive remembers its own current directory independently
- Does not work with drives that don't have current directory concept
Related Functions
ChDir: Changes the current directoryChDrive: Changes the current driveDir: Returns files/directories matching a patternMkDir: Creates a new directoryRmDir: Removes an empty directoryApp.Path: Returns the path where the application started