<!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">' 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</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
' Save current directory
savedDir = CurDir()
' Change to target directory
ChDir targetDir
' Do work in target directory
ProcessFiles
' 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
' Combine current directory with relative path
If Right(CurDir(), 1) = "\" Then
GetFullPath = CurDir() & relativePath
Else
GetFullPath = CurDir() & "\" & 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()
' Root directory ends with backslash (e.g., "C:\")
IsRootDirectory = (Len(currentPath) = 3 And Right(currentPath, 1) = "\")
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()
' 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) <> "\" Then
EnsureTrailingBackslash = path & "\"
Else
EnsureTrailingBackslash = path
End If
End Function
' 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
' Usage
Dim ctx As DirectoryContext
ctx = PushDirectory("C:\Temp")
' 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("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</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 = "application.log"
If Right(currentDir, 1) = "\" Then
GetLogFilePath = currentDir & logFile
Else
GetLogFilePath = currentDir & "\" & 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("TEMP")
On Error GoTo Cleanup
ChDir tempDir
' 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
' 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</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()
' 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</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
' 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</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
' 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</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) < 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</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">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</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
' Change directory and do work
ChDir "C:\Temp"
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">' 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</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>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>