vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - curdir - File">
    <title>curdir - File - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/file/index.html">File</a> / curdir</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="curdir-function">CurDir Function</h1>
<p>Returns a <code>String</code> representing the current path for the specified drive or the default drive.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">CurDir[(drive)]</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong><code>drive</code></strong>: Optional. <code>String</code> expression that specifies an existing drive. If no drive is
  specified or if drive is a zero-length string (""), <code>CurDir</code> 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:").</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> 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.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>CurDir</code> 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
<strong>Important Characteristics:</strong>
- 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:\")</p>
<h2 id="drive-specification">Drive Specification</h2>
<p>The drive parameter can be specified in several ways:
- <code>CurDir()</code> - Current drive
- <code>CurDir("")</code> - Current drive
- <code>CurDir("C")</code> - Drive C
- <code>CurDir("C:")</code> - Drive C
- <code>CurDir("D")</code> - Drive D</p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">&#x27; Get current directory of current drive
Dim currentDir As String
currentDir = CurDir()  &#x27; Returns something like &quot;C:\Users\Username\Documents&quot;
&#x27; Get current directory of specific drive
Dim cDrive As String
cDrive = CurDir(&quot;C&quot;)  &#x27; Returns current directory on C: drive
Dim dDrive As String
dDrive = CurDir(&quot;D:&quot;)  &#x27; Returns current directory on D: drive</code></pre>
<h3 id="save-and-restore-directory">Save and Restore Directory</h3>
<pre><code class="language-vbnet">Sub ProcessInDifferentDirectory(targetDir As String)
    Dim savedDir As String
    &#x27; Save current directory
    savedDir = CurDir()
    &#x27; Change to target directory
    ChDir targetDir
    &#x27; Do work in target directory
    ProcessFiles
    &#x27; Restore original directory
    ChDir savedDir
End Sub</code></pre>
<h3 id="building-relative-paths">Building Relative Paths</h3>
<pre><code class="language-vbnet">Function GetFullPath(relativePath As String) As String
    &#x27; Combine current directory with relative path
    If Right(CurDir(), 1) = &quot;\&quot; Then
        GetFullPath = CurDir() &amp; relativePath
    Else
        GetFullPath = CurDir() &amp; &quot;\&quot; &amp; relativePath
    End If
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="check-if-at-root-directory">Check if at Root Directory</h3>
<pre><code class="language-vbnet">Function IsRootDirectory() As Boolean
    Dim currentPath As String
    currentPath = CurDir()
    &#x27; Root directory ends with backslash (e.g., &quot;C:\&quot;)
    IsRootDirectory = (Len(currentPath) = 3 And Right(currentPath, 1) = &quot;\&quot;)
End Function</code></pre>
<h3 id="get-current-drive-letter">Get Current Drive Letter</h3>
<pre><code class="language-vbnet">Function GetCurrentDrive() As String
    Dim currentPath As String
    currentPath = CurDir()
    &#x27; Extract drive letter (first character)
    GetCurrentDrive = Left(currentPath, 1)
End Function</code></pre>
<h3 id="ensure-trailing-backslash">Ensure Trailing Backslash</h3>
<pre><code class="language-vbnet">Function EnsureTrailingBackslash(path As String) As String
    If Right(path, 1) &lt;&gt; &quot;\&quot; Then
        EnsureTrailingBackslash = path &amp; &quot;\&quot;
    Else
        EnsureTrailingBackslash = path
    End If
End Function
&#x27; Usage
Dim dirPath As String
dirPath = EnsureTrailingBackslash(CurDir())</code></pre>
<h3 id="directory-context-manager">Directory Context Manager</h3>
<pre><code class="language-vbnet">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
&#x27; Usage
Dim ctx As DirectoryContext
ctx = PushDirectory(&quot;C:\Temp&quot;)
&#x27; Do work...
PopDirectory ctx</code></pre>
<h3 id="multi-drive-path-tracking">Multi-Drive Path Tracking</h3>
<pre><code class="language-vbnet">Function GetAllDrivePaths() As Collection
    Dim paths As New Collection
    Dim drives() As String
    Dim i As Integer
    drives = Array(&quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;)
    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</code></pre>
<h3 id="log-file-in-current-directory">Log File in Current Directory</h3>
<pre><code class="language-vbnet">Function GetLogFilePath() As String
    Dim currentDir As String
    Dim logFile As String
    currentDir = CurDir()
    logFile = &quot;application.log&quot;
    If Right(currentDir, 1) = &quot;\&quot; Then
        GetLogFilePath = currentDir &amp; logFile
    Else
        GetLogFilePath = currentDir &amp; &quot;\&quot; &amp; logFile
    End If
End Function</code></pre>
<h3 id="temporary-directory-operations">Temporary Directory Operations</h3>
<pre><code class="language-vbnet">Sub ProcessInTempDirectory()
    Dim originalDir As String
    Dim tempDir As String
    originalDir = CurDir()
    tempDir = Environ(&quot;TEMP&quot;)
    On Error GoTo Cleanup
    ChDir tempDir
    &#x27; Process files in temp directory
    ProcessTempFiles
Cleanup:
    ChDir originalDir
End Sub</code></pre>
<h3 id="validate-relative-path">Validate Relative Path</h3>
<pre><code class="language-vbnet">Function IsRelativePath(path As String) As Boolean
    &#x27; Check if path is relative (doesn&#x27;t start with drive letter)
    IsRelativePath = (InStr(path, &quot;:&quot;) = 0)
End Function
Function ResolveRelativePath(relativePath As String) As String
    If IsRelativePath(relativePath) Then
        ResolveRelativePath = CurDir() &amp; &quot;\&quot; &amp; relativePath
    Else
        ResolveRelativePath = relativePath
    End If
End Function</code></pre>
<h3 id="directory-breadcrumb-trail">Directory Breadcrumb Trail</h3>
<pre><code class="language-vbnet">Function GetDirectoryParts() As String()
    Dim currentDir As String
    Dim parts() As String
    currentDir = CurDir()
    &#x27; Remove drive letter and colon
    If InStr(currentDir, &quot;:&quot;) &gt; 0 Then
        currentDir = Mid(currentDir, 3)
    End If
    &#x27; Remove leading backslash
    If Left(currentDir, 1) = &quot;\&quot; Then
        currentDir = Mid(currentDir, 2)
    End If
    &#x27; Split by backslash
    If Len(currentDir) &gt; 0 Then
        parts = Split(currentDir, &quot;\&quot;)
    End If
    GetDirectoryParts = parts
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="cross-drive-file-operations">Cross-Drive File Operations</h3>
<pre><code class="language-vbnet">Sub CopyFileToAnotherDrive(sourceFile As String, targetDrive As String)
    Dim sourceDrive As String
    Dim targetPath As String
    &#x27; Get current directory on target drive
    On Error Resume Next
    targetPath = CurDir(targetDrive)
    If Err.Number = 0 Then
        &#x27; Build target file path
        If Right(targetPath, 1) &lt;&gt; &quot;\&quot; Then
            targetPath = targetPath &amp; &quot;\&quot;
        End If
        FileCopy sourceFile, targetPath &amp; Dir(sourceFile)
    End If
End Sub</code></pre>
<h3 id="directory-stack-implementation">Directory Stack Implementation</h3>
<pre><code class="language-vbnet">Private dirStack As Collection
Sub InitDirectoryStack()
    Set dirStack = New Collection
End Sub
Sub PushDir(Optional newDir As String)
    If dirStack Is Nothing Then InitDirectoryStack
    &#x27; Save current directory
    dirStack.Add CurDir()
    &#x27; Change to new directory if specified
    If Len(newDir) &gt; 0 Then
        ChDir newDir
    End If
End Sub
Sub PopDir()
    If dirStack Is Nothing Then Exit Sub
    If dirStack.Count = 0 Then Exit Sub
    &#x27; Restore previous directory
    ChDir dirStack(dirStack.Count)
    dirStack.Remove dirStack.Count
End Sub</code></pre>
<h3 id="smart-path-concatenation">Smart Path Concatenation</h3>
<pre><code class="language-vbnet">Function CombinePaths(ParamArray paths() As Variant) As String
    Dim result As String
    Dim i As Integer
    Dim part As String
    If UBound(paths) &lt; LBound(paths) Then
        &#x27; 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))
        &#x27; Remove leading backslash from part
        If Left(part, 1) = &quot;\&quot; Then part = Mid(part, 2)
        &#x27; Add backslash if needed
        If Right(result, 1) &lt;&gt; &quot;\&quot; Then result = result &amp; &quot;\&quot;
        result = result &amp; part
    Next i
    CombinePaths = result
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function GetCurrentDirectorySafe(Optional drive As String = &quot;&quot;) 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  &#x27; Device unavailable
            MsgBox &quot;Drive &quot; &amp; drive &amp; &quot; is not available.&quot;, vbExclamation
        Case 71  &#x27; Disk not ready
            MsgBox &quot;Drive &quot; &amp; drive &amp; &quot; is not ready.&quot;, vbExclamation
        Case Else
            MsgBox &quot;Error getting current directory: &quot; &amp; Err.Description, vbCritical
    End Select
    GetCurrentDirectorySafe = &quot;&quot;
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 68</strong> (Device unavailable): Specified drive does not exist</li>
<li><strong>Error 71</strong> (Disk not ready): Drive exists but is not ready (e.g., no CD in drive)</li>
<li><strong>Error 5</strong> (Invalid procedure call): Invalid drive specification</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>CurDir</code> is a fast function with minimal overhead</li>
<li>Results can be cached if directory won't change during execution</li>
<li>Accessing network drives may have latency</li>
<li>For frequently used paths, cache the result in a variable</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="always-restore-directory">Always Restore Directory</h3>
<pre><code class="language-vbnet">Sub SafeDirectoryOperation()
    Dim savedDir As String
    savedDir = CurDir()
    On Error GoTo Cleanup
    &#x27; Change directory and do work
    ChDir &quot;C:\Temp&quot;
    ProcessFiles
Cleanup:
    ChDir savedDir
End Sub</code></pre>
<h3 id="use-absolute-paths-when-possible">Use Absolute Paths When Possible</h3>
<pre><code class="language-vbnet">&#x27; Instead of relying on current directory:
Open &quot;data.txt&quot; For Input As #1  &#x27; Depends on CurDir
&#x27; Use absolute paths:
Open &quot;C:\MyApp\Data\data.txt&quot; For Input As #1  &#x27; Explicit path</code></pre>
<h3 id="validate-drive-before-use">Validate Drive Before Use</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="platform-considerations">Platform Considerations</h2>
<ul>
<li><strong>Windows</strong>: Returns paths with backslashes (e.g., "C:\Windows")</li>
<li><strong>Drive letters</strong>: Windows-specific concept</li>
<li><strong>Network paths</strong>: <code>UNC</code> paths (\server\share) not supported by <code>CurDir</code></li>
<li><strong>Long paths</strong>: Paths longer than 260 characters may cause issues</li>
<li><strong>Case sensitivity</strong>: Windows file system is case-insensitive</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Returns only local drive paths, not <code>UNC</code> network paths</li>
<li>Cannot set the current directory (use <code>ChDir</code> for that)</li>
<li>Drive must be available and ready</li>
<li>Does not validate that the returned path still exists</li>
<li>Each drive remembers its own current directory independently</li>
<li>Does not work with drives that don't have current directory concept</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>ChDir</code>: Changes the current directory</li>
<li><code>ChDrive</code>: Changes the current drive</li>
<li><code>Dir</code>: Returns files/directories matching a pattern</li>
<li><code>MkDir</code>: Creates a new directory</li>
<li><code>RmDir</code>: Removes an empty directory</li>
<li><code>App.Path</code>: Returns the path where the application started</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to File</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>