VB6Parse / Library / File / curdir

VB6 Library Reference

CurDir Function

Returns a String representing the current path for the specified drive or the default drive.

Syntax

CurDir[(drive)]

Parameters

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:

Important Characteristics:

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

Performance Considerations

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

Limitations

← Back to File | View all functions