vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<!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 - dir - File">
    <title>dir - 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> / dir</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="dir-function">Dir Function</h1>
<p>Returns a <code>String</code> representing the name of a file, directory, or folder that matches a
specified pattern or file attribute, or the volume label of a drive.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Dir[(pathname[, attributes])]</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>pathname</strong>: Optional. <code>String</code> expression that specifies a file name, directory name,
  or folder name. May include wildcards (* and ?). If not specified, uses the pattern
  from the previous <code>Dir</code> call.</li>
<li><strong>attributes</strong>: Optional. Constant or numeric expression whose sum specifies file
  attributes. If omitted, returns files that match pathname but have no attributes.</li>
</ul>
<h2 id="attributes">Attributes</h2>
<ul>
<li><strong>vbNormal</strong> (0): Normal files (default)</li>
<li><strong>vbReadOnly</strong> (1): Read-only files</li>
<li><strong>vbHidden</strong> (2): Hidden files</li>
<li><strong>vbSystem</strong> (4): System files</li>
<li><strong>vbVolume</strong> (8): Volume label (pathname ignored)</li>
<li><strong>vbDirectory</strong> (16): Directories or folders</li>
<li><strong>vbArchive</strong> (32): Files that have changed since last backup</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> containing the name of a file, directory, or folder that matches the
specified pattern and attributes. Returns a zero-length string ("") when no more files
are found.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Dir</code> function is used to retrieve file and directory names that match a pattern.
It's commonly used to iterate through files in a directory or to check if a file exists.
<strong>Important Characteristics:</strong>
- First call with pathname initializes search and returns first match
- Subsequent calls without arguments return next matching file
- Returns empty string ("") when no more matches found
- Case-insensitive pattern matching
- Supports wildcards: * (multiple chars) and ? (single char)
- Does not return "." and ".." directory entries
- Order of returned files is not guaranteed (typically file system order)
- Maintains internal state between calls
- Multiple Dir loops cannot be nested without complications
- Changing directory during Dir enumeration can cause issues</p>
<h2 id="wildcards">Wildcards</h2>
<ul>
<li><code>*</code> - Matches zero or more characters</li>
<li><code>?</code> - Matches exactly one character</li>
<li><code>*.*</code> - All files with extensions</li>
<li><code>*.txt</code> - All .txt files</li>
<li><code>test?.dat</code> - Files like test1.dat, testA.dat</li>
</ul>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">&#x27; Get first .txt file in current directory
Dim fileName As String
fileName = Dir(&quot;*.txt&quot;)
MsgBox fileName
&#x27; Check if specific file exists
If Len(Dir(&quot;C:\data\report.txt&quot;)) &gt; 0 Then
    MsgBox &quot;File exists&quot;
End If
&#x27; Get volume label
Dim volumeLabel As String
volumeLabel = Dir(&quot;C:\&quot;, vbVolume)</code></pre>
<h3 id="iterate-through-files">Iterate Through Files</h3>
<pre><code class="language-vbnet">Sub ListAllTextFiles()
    Dim fileName As String
    &#x27; First call with pattern
    fileName = Dir(&quot;C:\Documents\*.txt&quot;)
    &#x27; Loop through all matches
    Do While fileName &lt;&gt; &quot;&quot;
        Debug.Print fileName
        fileName = Dir  &#x27; Subsequent calls without arguments
    Loop
End Sub</code></pre>
<h3 id="count-files">Count Files</h3>
<pre><code class="language-vbnet">Function CountFiles(path As String, pattern As String) As Long
    Dim fileName As String
    Dim count As Long
    count = 0
    fileName = Dir(path &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        count = count + 1
        fileName = Dir
    Loop
    CountFiles = count
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="file-existence-check">File Existence Check</h3>
<pre><code class="language-vbnet">Function FileExists(filePath As String) As Boolean
    FileExists = (Len(Dir(filePath)) &gt; 0)
End Function
&#x27; Usage
If FileExists(&quot;C:\data\file.txt&quot;) Then
    &#x27; File exists
End If</code></pre>
<h3 id="get-all-files-in-directory">Get All Files in Directory</h3>
<pre><code class="language-vbnet">Function GetFileList(folderPath As String, pattern As String) As Variant
    Dim files() As String
    Dim fileName As String
    Dim count As Long
    count = 0
    ReDim files(0 To 100)
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        files(count) = fileName
        count = count + 1
        If count &gt; UBound(files) Then
            ReDim Preserve files(0 To UBound(files) + 100)
        End If
        fileName = Dir
    Loop
    If count &gt; 0 Then
        ReDim Preserve files(0 To count - 1)
        GetFileList = files
    Else
        GetFileList = Array()
    End If
End Function</code></pre>
<h3 id="find-files-by-attribute">Find Files by Attribute</h3>
<pre><code class="language-vbnet">Sub ListHiddenFiles(folderPath As String)
    Dim fileName As String
    fileName = Dir(folderPath &amp; &quot;\*.*&quot;, vbHidden)
    Do While fileName &lt;&gt; &quot;&quot;
        Debug.Print &quot;Hidden: &quot; &amp; fileName
        fileName = Dir
    Loop
End Sub
Sub ListDirectories(folderPath As String)
    Dim dirName As String
    dirName = Dir(folderPath &amp; &quot;\*.*&quot;, vbDirectory)
    Do While dirName &lt;&gt; &quot;&quot;
        &#x27; Filter out &quot;.&quot; and &quot;..&quot; if they appear
        If dirName &lt;&gt; &quot;.&quot; And dirName &lt;&gt; &quot;..&quot; Then
            &#x27; Check if it&#x27;s actually a directory
            If GetAttr(folderPath &amp; &quot;\&quot; &amp; dirName) And vbDirectory Then
                Debug.Print &quot;Directory: &quot; &amp; dirName
            End If
        End If
        dirName = Dir
    Loop
End Sub</code></pre>
<h3 id="search-multiple-file-types">Search Multiple File Types</h3>
<pre><code class="language-vbnet">Function FindDocuments(folderPath As String) As Variant
    Dim files() As String
    Dim fileName As String
    Dim count As Long
    Dim extensions As Variant
    Dim i As Integer
    extensions = Array(&quot;*.txt&quot;, &quot;*.doc&quot;, &quot;*.docx&quot;, &quot;*.pdf&quot;)
    ReDim files(0 To 100)
    count = 0
    For i = LBound(extensions) To UBound(extensions)
        fileName = Dir(folderPath &amp; &quot;\&quot; &amp; extensions(i))
        Do While fileName &lt;&gt; &quot;&quot;
            files(count) = fileName
            count = count + 1
            If count &gt; UBound(files) Then
                ReDim Preserve files(0 To UBound(files) + 100)
            End If
            fileName = Dir
        Loop
    Next i
    If count &gt; 0 Then
        ReDim Preserve files(0 To count - 1)
        FindDocuments = files
    Else
        FindDocuments = Array()
    End If
End Function</code></pre>
<h3 id="get-full-file-paths">Get Full File Paths</h3>
<pre><code class="language-vbnet">Function GetFullPaths(folderPath As String, pattern As String) As Variant
    Dim paths() As String
    Dim fileName As String
    Dim count As Long
    count = 0
    ReDim paths(0 To 100)
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        paths(count) = folderPath &amp; &quot;\&quot; &amp; fileName
        count = count + 1
        If count &gt; UBound(paths) Then
            ReDim Preserve paths(0 To UBound(paths) + 100)
        End If
        fileName = Dir
    Loop
    If count &gt; 0 Then
        ReDim Preserve paths(0 To count - 1)
        GetFullPaths = paths
    Else
        GetFullPaths = Array()
    End If
End Function</code></pre>
<h3 id="delete-all-files-matching-pattern">Delete All Files Matching Pattern</h3>
<pre><code class="language-vbnet">Sub DeleteMatchingFiles(folderPath As String, pattern As String)
    Dim fileName As String
    Dim fullPath As String
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        fullPath = folderPath &amp; &quot;\&quot; &amp; fileName
        &#x27; Get next file BEFORE deleting (Dir state would be lost)
        fileName = Dir
        &#x27; Delete the file
        Kill fullPath
    Loop
End Sub</code></pre>
<h3 id="find-newest-file">Find Newest File</h3>
<pre><code class="language-vbnet">Function GetNewestFile(folderPath As String, pattern As String) As String
    Dim fileName As String
    Dim newestFile As String
    Dim newestDate As Date
    Dim currentDate As Date
    Dim fullPath As String
    newestDate = 0
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        fullPath = folderPath &amp; &quot;\&quot; &amp; fileName
        currentDate = FileDateTime(fullPath)
        If currentDate &gt; newestDate Then
            newestDate = currentDate
            newestFile = fileName
        End If
        fileName = Dir
    Loop
    GetNewestFile = newestFile
End Function</code></pre>
<h3 id="calculate-total-size">Calculate Total Size</h3>
<pre><code class="language-vbnet">Function GetTotalFileSize(folderPath As String, pattern As String) As Double
    Dim fileName As String
    Dim totalSize As Double
    Dim fullPath As String
    totalSize = 0
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        fullPath = folderPath &amp; &quot;\&quot; &amp; fileName
        totalSize = totalSize + FileLen(fullPath)
        fileName = Dir
    Loop
    GetTotalFileSize = totalSize
End Function</code></pre>
<h3 id="recursive-directory-search">Recursive Directory Search</h3>
<pre><code class="language-vbnet">Sub SearchRecursive(folderPath As String, pattern As String)
    Dim fileName As String
    Dim dirName As String
    Dim fullPath As String
    &#x27; Search files in current directory
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        Debug.Print folderPath &amp; &quot;\&quot; &amp; fileName
        fileName = Dir
    Loop
    &#x27; Search subdirectories
    dirName = Dir(folderPath &amp; &quot;\*.*&quot;, vbDirectory)
    Do While dirName &lt;&gt; &quot;&quot;
        If dirName &lt;&gt; &quot;.&quot; And dirName &lt;&gt; &quot;..&quot; Then
            fullPath = folderPath &amp; &quot;\&quot; &amp; dirName
            If GetAttr(fullPath) And vbDirectory Then
                SearchRecursive fullPath, pattern
            End If
        End If
        dirName = Dir
    Loop
End Sub</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="file-filter-with-multiple-criteria">File Filter with Multiple Criteria</h3>
<pre><code class="language-vbnet">Function FindFilesAdvanced(folderPath As String, _
                          minSize As Long, maxSize As Long, _
                          afterDate As Date) As Variant
    Dim files() As String
    Dim fileName As String
    Dim fullPath As String
    Dim fileSize As Long
    Dim fileDate As Date
    Dim count As Long
    count = 0
    ReDim files(0 To 100)
    fileName = Dir(folderPath &amp; &quot;\*.*&quot;)
    Do While fileName &lt;&gt; &quot;&quot;
        fullPath = folderPath &amp; &quot;\&quot; &amp; fileName
        fileSize = FileLen(fullPath)
        fileDate = FileDateTime(fullPath)
        If fileSize &gt;= minSize And fileSize &lt;= maxSize And fileDate &gt; afterDate Then
            files(count) = fileName
            count = count + 1
            If count &gt; UBound(files) Then
                ReDim Preserve files(0 To UBound(files) + 100)
            End If
        End If
        fileName = Dir
    Loop
    If count &gt; 0 Then
        ReDim Preserve files(0 To count - 1)
        FindFilesAdvanced = files
    Else
        FindFilesAdvanced = Array()
    End If
End Function</code></pre>
<h3 id="safe-dir-loop-helper">Safe Dir Loop Helper</h3>
<pre><code class="language-vbnet">&#x27; Helper to avoid nested Dir issues
Type FileInfo
    Name As String
    FullPath As String
    Size As Long
    Modified As Date
End Type
Function GetFileInfoList(folderPath As String, pattern As String) As Variant
    Dim files() As FileInfo
    Dim fileName As String
    Dim count As Long
    count = 0
    ReDim files(0 To 100)
    fileName = Dir(folderPath &amp; &quot;\&quot; &amp; pattern)
    Do While fileName &lt;&gt; &quot;&quot;
        files(count).Name = fileName
        files(count).FullPath = folderPath &amp; &quot;\&quot; &amp; fileName
        files(count).Size = FileLen(files(count).FullPath)
        files(count).Modified = FileDateTime(files(count).FullPath)
        count = count + 1
        If count &gt; UBound(files) Then
            ReDim Preserve files(0 To UBound(files) + 100)
        End If
        fileName = Dir
    Loop
    If count &gt; 0 Then
        ReDim Preserve files(0 To count - 1)
        GetFileInfoList = files
    Else
        GetFileInfoList = Array()
    End If
End Function</code></pre>
<h3 id="backup-old-files">Backup Old Files</h3>
<pre><code class="language-vbnet">Sub BackupOldFiles(sourcePath As String, backupPath As String, daysOld As Integer)
    Dim fileName As String
    Dim fullPath As String
    Dim cutoffDate As Date
    cutoffDate = Date - daysOld
    fileName = Dir(sourcePath &amp; &quot;\*.*&quot;)
    Do While fileName &lt;&gt; &quot;&quot;
        fullPath = sourcePath &amp; &quot;\&quot; &amp; fileName
        If FileDateTime(fullPath) &lt; cutoffDate Then
            FileCopy fullPath, backupPath &amp; &quot;\&quot; &amp; fileName
        End If
        fileName = Dir
    Loop
End Sub</code></pre>
<h3 id="build-file-index">Build File Index</h3>
<pre><code class="language-vbnet">Function BuildFileIndex(rootPath As String) As Collection
    Dim index As New Collection
    Dim fileName As String
    &#x27; Add all files to collection with full path as key
    fileName = Dir(rootPath &amp; &quot;\*.*&quot;)
    Do While fileName &lt;&gt; &quot;&quot;
        On Error Resume Next
        index.Add fileName, UCase(fileName)
        On Error GoTo 0
        fileName = Dir
    Loop
    Set BuildFileIndex = index
End Function</code></pre>
<h3 id="file-synchronization-check">File Synchronization Check</h3>
<pre><code class="language-vbnet">Function CompareDirectories(path1 As String, path2 As String) As String
    Dim files1 As Collection
    Dim files2 As Collection
    Dim fileName As String
    Dim report As String
    Set files1 = New Collection
    Set files2 = New Collection
    &#x27; Get files from first directory
    fileName = Dir(path1 &amp; &quot;\*.*&quot;)
    Do While fileName &lt;&gt; &quot;&quot;
        files1.Add fileName
        fileName = Dir
    Loop
    &#x27; Get files from second directory
    fileName = Dir(path2 &amp; &quot;\*.*&quot;)
    Do While fileName &lt;&gt; &quot;&quot;
        files2.Add fileName
        fileName = Dir
    Loop
    &#x27; Compare (simplified - full version would check both ways)
    report = &quot;Files only in &quot; &amp; path1 &amp; &quot;:&quot; &amp; vbCrLf
    &#x27; ... comparison logic ...
    CompareDirectories = report
End Function</code></pre>
<h3 id="generate-directory-listing-report">Generate Directory Listing Report</h3>
<pre><code class="language-vbnet">Sub ExportDirectoryListing(folderPath As String, outputFile As String)
    Dim fileName As String
    Dim fullPath As String
    Dim fileNum As Integer
    fileNum = FreeFile
    Open outputFile For Output As #fileNum
    Print #fileNum, &quot;Directory Listing for: &quot; &amp; folderPath
    Print #fileNum, &quot;Generated: &quot; &amp; Now
    Print #fileNum, String(80, &quot;-&quot;)
    Print #fileNum, &quot;Filename&quot; &amp; vbTab &amp; &quot;Size&quot; &amp; vbTab &amp; &quot;Modified&quot;
    Print #fileNum, String(80, &quot;-&quot;)
    fileName = Dir(folderPath &amp; &quot;\*.*&quot;)
    Do While fileName &lt;&gt; &quot;&quot;
        fullPath = folderPath &amp; &quot;\&quot; &amp; fileName
        Print #fileNum, fileName &amp; vbTab &amp; _
                       FileLen(fullPath) &amp; vbTab &amp; _
                       FileDateTime(fullPath)
        fileName = Dir
    Loop
    Close #fileNum
End Sub</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeDir(pathname As String, Optional attributes As Integer = 0) As String
    On Error Resume Next
    SafeDir = Dir(pathname, attributes)
    If Err.Number &lt;&gt; 0 Then
        SafeDir = &quot;&quot;
    End If
End Function
Function SafeFileExists(filePath As String) As Boolean
    On Error Resume Next
    SafeFileExists = (Len(Dir(filePath)) &gt; 0)
    If Err.Number &lt;&gt; 0 Then
        SafeFileExists = False
    End If
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 52</strong> (Bad file name or number): Invalid pathname or pattern</li>
<li><strong>Error 76</strong> (Path not found): Directory does not exist</li>
<li><strong>Error 68</strong> (Device unavailable): Drive not ready or network path unavailable</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Dir</code> is relatively fast for simple file enumeration</li>
<li>For large directories, consider showing progress</li>
<li>Network paths can be slow; consider timeout handling</li>
<li>Avoid nested <code>Dir</code> loops (collect to array first)</li>
<li><code>FileSystemObject</code> may be faster for complex operations</li>
<li>Cache results if scanning same directory repeatedly</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="always-check-for-empty-string">Always Check for Empty String</h3>
<pre><code class="language-vbnet">&#x27; Good - Check for no more files
fileName = Dir(&quot;*.txt&quot;)
Do While fileName &lt;&gt; &quot;&quot;
    &#x27; Process file
    fileName = Dir
Loop
&#x27; Avoid - May cause infinite loop
fileName = Dir(&quot;*.txt&quot;)
Do While Len(fileName) &gt; 0  &#x27; Less reliable
    fileName = Dir
Loop</code></pre>
<h3 id="store-files-before-processing">Store Files Before Processing</h3>
<pre><code class="language-vbnet">&#x27; Good - Collect files first
Dim files() As String, i As Integer
ReDim files(0 To 100)
count = 0
fileName = Dir(&quot;*.txt&quot;)
Do While fileName &lt;&gt; &quot;&quot;
    files(count) = fileName
    count = count + 1
    fileName = Dir
Loop
&#x27; Now process without Dir active
For i = 0 To count - 1
    ProcessFile files(i)
Next i</code></pre>
<h3 id="use-absolute-paths">Use Absolute Paths</h3>
<pre><code class="language-vbnet">&#x27; Good - Explicit path
fileName = Dir(&quot;C:\Data\*.txt&quot;)
&#x27; Risky - Depends on current directory
fileName = Dir(&quot;*.txt&quot;)</code></pre>
<h3 id="handle-no-matches-gracefully">Handle No Matches Gracefully</h3>
<pre><code class="language-vbnet">fileName = Dir(&quot;*.xyz&quot;)
If fileName = &quot;&quot; Then
    MsgBox &quot;No matching files found&quot;
    Exit Sub
End If</code></pre>
<h2 id="comparison-with-other-methods">Comparison with Other Methods</h2>
<h3 id="dir-vs-filesystemobject"><code>Dir</code> vs <code>FileSystemObject</code></h3>
<pre><code class="language-vbnet">&#x27; Dir - Built-in, faster for simple cases
fileName = Dir(&quot;C:\Data\*.txt&quot;)
&#x27; FileSystemObject - More features, but requires reference
Dim fso As New FileSystemObject
Dim folder As Folder
Set folder = fso.GetFolder(&quot;C:\Data&quot;)
&#x27; ... more complex but more powerful</code></pre>
<h3 id="dir-vs-file-dialog"><code>Dir</code> vs <code>File Dialog</code></h3>
<pre><code class="language-vbnet">&#x27; Dir - Programmatic file discovery
fileName = Dir(&quot;*.txt&quot;)
&#x27; File Dialog - User selection
fileName = Application.GetOpenFilename(&quot;Text Files (*.txt), *.txt&quot;)</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot nest Dir loops reliably (single internal state)</li>
<li>Does not return files in sorted order</li>
<li>Returns only file names, not full paths</li>
<li>No built-in recursion into subdirectories</li>
<li>Cannot filter by date, size, or other attributes directly</li>
<li>Changing current directory during enumeration causes issues</li>
<li>Limited attribute filtering compared to <code>FileSystemObject</code></li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>GetAttr</code>: Gets file attributes</li>
<li><code>SetAttr</code>: Sets file attributes</li>
<li><code>FileLen</code>: Returns file size</li>
<li><code>FileDateTime</code>: Returns file modification date/time</li>
<li><code>CurDir</code>: Returns current directory</li>
<li><code>ChDir</code>: Changes current directory</li>
<li><code>MkDir</code>: Creates directory</li>
<li><code>RmDir</code>: Removes directory</li>
<li><code>Kill</code>: Deletes file</li>
<li><code>FileCopy</code>: Copies file</li>
<li><code>Name</code>: Renames/moves file</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>