VB6Parse / Library / File / curdir_dollar

VB6 Library Reference

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

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

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

  1. Directory context - Determine current working directory
  2. Path building - Construct full paths from relative paths
  3. Directory restoration - Save and restore directory state
  4. File operations - Locate files relative to current directory
  5. Logging - Record current directory for debugging
  6. Validation - Verify expected working directory
  7. 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
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

Best Practices

  1. Always save current directory before changing it
  2. Use error handling when changing directories
  3. Restore original directory in error handlers
  4. Use App.Path for application-relative paths instead of relying on current directory
  5. Check for trailing backslash when building paths
  6. Use CurDir$ instead of CurDir for better performance
  7. Be aware that current directory can be changed by other code
  8. Document assumptions about current directory in your code
  9. Use absolute paths when possible to avoid directory dependencies
  10. Test code with different working directories

Performance Considerations

Platform Notes

Error Conditions

Security Considerations

  1. Don't assume the current directory for security-sensitive operations
  2. Use absolute paths for configuration and data files
  3. Be aware that current directory can be manipulated by attackers
  4. Validate directory paths before changing to them
  5. Use App.Path for application resources rather than current directory

Limitations

← Back to File | View all functions