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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
//! `GetAttr` Function
//!
//! Returns an Integer representing the attributes of a file, directory, or folder.
//!
//! # Syntax
//!
//! ```vb
//! GetAttr(pathname)
//! ```
//!
//! # Parameters
//!
//! - `pathname` - Required. String expression that specifies a file name. May include directory or folder, and drive.
//!
//! # Return Value
//!
//! Returns an Integer representing the sum of the following attribute values:
//!
//! | Constant | Value | Description |
//! |----------|-------|-------------|
//! | vbNormal | 0 | Normal (no attributes set) |
//! | vbReadOnly | 1 | Read-only |
//! | vbHidden | 2 | Hidden |
//! | vbSystem | 4 | System file |
//! | vbVolume | 8 | Volume label |
//! | vbDirectory | 16 | Directory or folder |
//! | vbArchive | 32 | File has changed since last backup |
//! | vbAlias | 64 | Specified file name is an alias (Macintosh only) |
//!
//! # Remarks
//!
//! - Use the And operator to test for a specific attribute.
//! - The return value can be a combination of multiple attributes.
//! - To check if a file has a specific attribute, use bitwise AND comparison.
//! - `GetAttr` generates an error if the file doesn't exist.
//! - Use `Dir` to check if a file exists before calling `GetAttr`.
//! - On systems other than Macintosh, `vbAlias` is never set.
//! - The `vbVolume` constant is used for volume labels only.
//!
//! # Typical Uses
//!
//! - Checking if a file is read-only before attempting to modify it
//! - Detecting hidden or system files
//! - Verifying if a path points to a directory
//! - Determining file attributes for backup operations
//! - Filtering files based on attributes
//! - Security and permission checks
//!
//! # Basic Usage Examples
//!
//! ```vb
//! ' Check if file is read-only
//! Dim attr As Integer
//! attr = GetAttr("C:\data.txt")
//!
//! If attr And vbReadOnly Then
//!     MsgBox "File is read-only"
//! End If
//!
//! ' Check if path is a directory
//! Dim pathAttr As Integer
//! pathAttr = GetAttr("C:\MyFolder")
//!
//! If pathAttr And vbDirectory Then
//!     MsgBox "This is a directory"
//! End If
//!
//! ' Check for hidden file
//! Dim fileAttr As Integer
//! fileAttr = GetAttr("C:\hidden.sys")
//!
//! If fileAttr And vbHidden Then
//!     MsgBox "File is hidden"
//! End If
//!
//! ' Check for system file
//! If GetAttr("C:\Windows\System32\ntoskrnl.exe") And vbSystem Then
//!     MsgBox "This is a system file"
//! End If
//! ```
//!
//! # Common Patterns
//!
//! ## 1. Check Read-Only Before Writing
//!
//! ```vb
//! Sub WriteToFile(filename As String, content As String)
//!     Dim attr As Integer
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     attr = GetAttr(filename)
//!     
//!     If attr And vbReadOnly Then
//!         MsgBox "Cannot write to read-only file: " & filename
//!         Exit Sub
//!     End If
//!     
//!     ' Proceed with writing
//!     Dim fileNum As Integer
//!     fileNum = FreeFile
//!     Open filename For Output As #fileNum
//!     Print #fileNum, content
//!     Close #fileNum
//!     
//!     Exit Sub
//!     
//! ErrorHandler:
//!     MsgBox "Error accessing file: " & Err.Description
//! End Sub
//! ```
//!
//! ## 2. Detect Directory vs File
//!
//! ```vb
//! Function IsDirectory(path As String) As Boolean
//!     On Error GoTo ErrorHandler
//!     
//!     Dim attr As Integer
//!     attr = GetAttr(path)
//!     
//!     IsDirectory = (attr And vbDirectory) <> 0
//!     Exit Function
//!     
//! ErrorHandler:
//!     IsDirectory = False
//! End Function
//!
//! ' Usage
//! If IsDirectory("C:\Windows") Then
//!     Debug.Print "Path is a directory"
//! Else
//!     Debug.Print "Path is a file"
//! End If
//! ```
//!
//! ## 3. Find Hidden Files
//!
//! ```vb
//! Sub ListHiddenFiles(folderPath As String)
//!     Dim filename As String
//!     Dim fullPath As String
//!     Dim attr As Integer
//!     
//!     filename = Dir(folderPath & "\*.*", vbHidden)
//!     
//!     Do While filename <> ""
//!         fullPath = folderPath & "\" & filename
//!         
//!         On Error Resume Next
//!         attr = GetAttr(fullPath)
//!         On Error GoTo 0
//!         
//!         If (attr And vbHidden) And Not (attr And vbDirectory) Then
//!             Debug.Print "Hidden file: " & filename
//!         End If
//!         
//!         filename = Dir
//!     Loop
//! End Sub
//! ```
//!
//! ## 4. Check Multiple Attributes
//!
//! ```vb
//! Function GetFileAttributeDescription(filename As String) As String
//!     Dim attr As Integer
//!     Dim description As String
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     attr = GetAttr(filename)
//!     description = ""
//!     
//!     If attr = vbNormal Then
//!         description = "Normal"
//!     Else
//!         If attr And vbReadOnly Then description = description & "Read-Only "
//!         If attr And vbHidden Then description = description & "Hidden "
//!         If attr And vbSystem Then description = description & "System "
//!         If attr And vbDirectory Then description = description & "Directory "
//!         If attr And vbArchive Then description = description & "Archive "
//!     End If
//!     
//!     GetFileAttributeDescription = Trim(description)
//!     Exit Function
//!     
//! ErrorHandler:
//!     GetFileAttributeDescription = "Error: " & Err.Description
//! End Function
//! ```
//!
//! ## 5. Safe File Delete
//!
//! ```vb
//! Function SafeDeleteFile(filename As String) As Boolean
//!     Dim attr As Integer
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     ' Check if file exists and get attributes
//!     attr = GetAttr(filename)
//!     
//!     ' Don't delete system or read-only files
//!     If attr And vbSystem Then
//!         MsgBox "Cannot delete system file: " & filename
//!         SafeDeleteFile = False
//!         Exit Function
//!     End If
//!     
//!     ' Remove read-only attribute if set
//!     If attr And vbReadOnly Then
//!         SetAttr filename, attr And Not vbReadOnly
//!     End If
//!     
//!     ' Delete the file
//!     Kill filename
//!     SafeDeleteFile = True
//!     Exit Function
//!     
//! ErrorHandler:
//!     MsgBox "Error deleting file: " & Err.Description
//!     SafeDeleteFile = False
//! End Function
//! ```
//!
//! ## 6. List Files by Attribute
//!
//! ```vb
//! Sub ListFilesByAttribute(folderPath As String, attributeFlag As Integer)
//!     Dim filename As String
//!     Dim fullPath As String
//!     Dim attr As Integer
//!     
//!     filename = Dir(folderPath & "\*.*", vbNormal + vbHidden + vbSystem)
//!     
//!     Do While filename <> ""
//!         fullPath = folderPath & "\" & filename
//!         
//!         On Error Resume Next
//!         attr = GetAttr(fullPath)
//!         On Error GoTo 0
//!         
//!         If (attr And attributeFlag) And Not (attr And vbDirectory) Then
//!             Debug.Print filename & " (Attribute: " & attr & ")"
//!         End If
//!         
//!         filename = Dir
//!     Loop
//! End Sub
//!
//! ' Usage
//! ListFilesByAttribute "C:\Temp", vbArchive  ' List files needing backup
//! ```
//!
//! ## 7. Backup File Scanner
//!
//! ```vb
//! Function NeedsBackup(filename As String) As Boolean
//!     Dim attr As Integer
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     attr = GetAttr(filename)
//!     
//!     ' Check if archive bit is set
//!     NeedsBackup = (attr And vbArchive) <> 0
//!     Exit Function
//!     
//! ErrorHandler:
//!     NeedsBackup = False
//! End Function
//!
//! Sub FindFilesNeedingBackup(folderPath As String, lst As ListBox)
//!     Dim filename As String
//!     Dim fullPath As String
//!     
//!     lst.Clear
//!     filename = Dir(folderPath & "\*.*")
//!     
//!     Do While filename <> ""
//!         fullPath = folderPath & "\" & filename
//!         
//!         If NeedsBackup(fullPath) Then
//!             lst.AddItem filename
//!         End If
//!         
//!         filename = Dir
//!     Loop
//! End Sub
//! ```
//!
//! ## 8. File Permission Check
//!
//! ```vb
//! Function CanModifyFile(filename As String) As Boolean
//!     Dim attr As Integer
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     attr = GetAttr(filename)
//!     
//!     ' File can be modified if it's not read-only and not a system file
//!     CanModifyFile = Not ((attr And vbReadOnly) Or (attr And vbSystem))
//!     Exit Function
//!     
//! ErrorHandler:
//!     CanModifyFile = False
//! End Function
//!
//! Sub ModifyFileIfAllowed(filename As String)
//!     If CanModifyFile(filename) Then
//!         ' Proceed with modification
//!         Debug.Print "File can be modified"
//!     Else
//!         Debug.Print "File is protected"
//!     End If
//! End Sub
//! ```
//!
//! ## 9. Directory Tree Walker
//!
//! ```vb
//! Sub WalkDirectory(path As String, Optional level As Integer = 0)
//!     Dim filename As String
//!     Dim fullPath As String
//!     Dim attr As Integer
//!     Dim indent As String
//!     
//!     indent = String(level * 2, " ")
//!     filename = Dir(path & "\*.*", vbDirectory)
//!     
//!     Do While filename <> ""
//!         If filename <> "." And filename <> ".." Then
//!             fullPath = path & "\" & filename
//!             
//!             On Error Resume Next
//!             attr = GetAttr(fullPath)
//!             On Error GoTo 0
//!             
//!             If attr And vbDirectory Then
//!                 Debug.Print indent & "[DIR] " & filename
//!                 WalkDirectory fullPath, level + 1
//!             Else
//!                 Debug.Print indent & filename
//!             End If
//!         End If
//!         
//!         filename = Dir
//!     Loop
//! End Sub
//! ```
//!
//! ## 10. File Attribute Report
//!
//! ```vb
//! Sub GenerateAttributeReport(folderPath As String)
//!     Dim filename As String
//!     Dim fullPath As String
//!     Dim attr As Integer
//!     Dim normalCount As Long
//!     Dim readOnlyCount As Long
//!     Dim hiddenCount As Long
//!     Dim systemCount As Long
//!     Dim archiveCount As Long
//!     
//!     filename = Dir(folderPath & "\*.*", vbNormal + vbHidden + vbSystem)
//!     
//!     Do While filename <> ""
//!         fullPath = folderPath & "\" & filename
//!         
//!         On Error Resume Next
//!         attr = GetAttr(fullPath)
//!         On Error GoTo 0
//!         
//!         If Not (attr And vbDirectory) Then
//!             If attr = vbNormal Then normalCount = normalCount + 1
//!             If attr And vbReadOnly Then readOnlyCount = readOnlyCount + 1
//!             If attr And vbHidden Then hiddenCount = hiddenCount + 1
//!             If attr And vbSystem Then systemCount = systemCount + 1
//!             If attr And vbArchive Then archiveCount = archiveCount + 1
//!         End If
//!         
//!         filename = Dir
//!     Loop
//!     
//!     Debug.Print "File Attribute Report for: " & folderPath
//!     Debug.Print String(50, "=")
//!     Debug.Print "Normal: " & normalCount
//!     Debug.Print "Read-Only: " & readOnlyCount
//!     Debug.Print "Hidden: " & hiddenCount
//!     Debug.Print "System: " & systemCount
//!     Debug.Print "Archive: " & archiveCount
//! End Sub
//! ```
//!
//! # Advanced Usage
//!
//! ## 1. File Attribute Manager Class
//!
//! ```vb
//! ' Class: FileAttributeManager
//! Private m_Filename As String
//! Private m_Attributes As Integer
//!
//! Public Sub LoadFile(filename As String)
//!     m_Filename = filename
//!     RefreshAttributes
//! End Sub
//!
//! Private Sub RefreshAttributes()
//!     On Error Resume Next
//!     m_Attributes = GetAttr(m_Filename)
//!     On Error GoTo 0
//! End Sub
//!
//! Public Property Get IsReadOnly() As Boolean
//!     IsReadOnly = (m_Attributes And vbReadOnly) <> 0
//! End Property
//!
//! Public Property Let IsReadOnly(value As Boolean)
//!     If value Then
//!         SetAttr m_Filename, m_Attributes Or vbReadOnly
//!     Else
//!         SetAttr m_Filename, m_Attributes And Not vbReadOnly
//!     End If
//!     RefreshAttributes
//! End Property
//!
//! Public Property Get IsHidden() As Boolean
//!     IsHidden = (m_Attributes And vbHidden) <> 0
//! End Property
//!
//! Public Property Let IsHidden(value As Boolean)
//!     If value Then
//!         SetAttr m_Filename, m_Attributes Or vbHidden
//!     Else
//!         SetAttr m_Filename, m_Attributes And Not vbHidden
//!     End If
//!     RefreshAttributes
//! End Property
//!
//! Public Property Get IsDirectory() As Boolean
//!     IsDirectory = (m_Attributes And vbDirectory) <> 0
//! End Property
//!
//! Public Property Get AttributeValue() As Integer
//!     AttributeValue = m_Attributes
//! End Property
//! ```
//!
//! ## 2. Smart File Filter
//!
//! ```vb
//! Type FileFilter
//!     IncludeReadOnly As Boolean
//!     IncludeHidden As Boolean
//!     IncludeSystem As Boolean
//!     IncludeArchive As Boolean
//!     ExcludeDirectories As Boolean
//! End Type
//!
//! Function MatchesFilter(filename As String, filter As FileFilter) As Boolean
//!     Dim attr As Integer
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     attr = GetAttr(filename)
//!     
//!     ' Check exclusions first
//!     If filter.ExcludeDirectories And (attr And vbDirectory) Then
//!         MatchesFilter = False
//!         Exit Function
//!     End If
//!     
//!     ' Check inclusions
//!     MatchesFilter = True
//!     
//!     If Not filter.IncludeReadOnly And (attr And vbReadOnly) Then
//!         MatchesFilter = False
//!     ElseIf Not filter.IncludeHidden And (attr And vbHidden) Then
//!         MatchesFilter = False
//!     ElseIf Not filter.IncludeSystem And (attr And vbSystem) Then
//!         MatchesFilter = False
//!     ElseIf Not filter.IncludeArchive And (attr And vbArchive) Then
//!         MatchesFilter = False
//!     End If
//!     
//!     Exit Function
//!     
//! ErrorHandler:
//!     MatchesFilter = False
//! End Function
//! ```
//!
//! ## 3. Attribute Change Monitor
//!
//! ```vb
//! Type FileSnapshot
//!     Filename As String
//!     Attributes As Integer
//!     Timestamp As Date
//! End Type
//!
//! Private m_Snapshots As Collection
//!
//! Sub InitializeMonitor()
//!     Set m_Snapshots = New Collection
//! End Sub
//!
//! Sub TakeSnapshot(filename As String)
//!     Dim snapshot As FileSnapshot
//!     
//!     snapshot.Filename = filename
//!     snapshot.Attributes = GetAttr(filename)
//!     snapshot.Timestamp = Now
//!     
//!     m_Snapshots.Add snapshot, filename
//! End Sub
//!
//! Function DetectAttributeChanges(filename As String) As Boolean
//!     Dim currentAttr As Integer
//!     Dim snapshot As FileSnapshot
//!     Dim i As Long
//!     
//!     currentAttr = GetAttr(filename)
//!     
//!     ' Find snapshot
//!     For i = 1 To m_Snapshots.Count
//!         snapshot = m_Snapshots(i)
//!         If snapshot.Filename = filename Then
//!             DetectAttributeChanges = (snapshot.Attributes <> currentAttr)
//!             Exit Function
//!         End If
//!     Next i
//!     
//!     DetectAttributeChanges = False
//! End Function
//! ```
//!
//! ## 4. Bulk Attribute Operations
//!
//! ```vb
//! Sub SetAttributeForMultipleFiles(filenames() As String, _
//!                                  attributeToSet As Integer, _
//!                                  enable As Boolean)
//!     Dim i As Long
//!     Dim currentAttr As Integer
//!     Dim newAttr As Integer
//!     
//!     For i = LBound(filenames) To UBound(filenames)
//!         On Error Resume Next
//!         
//!         currentAttr = GetAttr(filenames(i))
//!         
//!         If enable Then
//!             newAttr = currentAttr Or attributeToSet
//!         Else
//!             newAttr = currentAttr And Not attributeToSet
//!         End If
//!         
//!         SetAttr filenames(i), newAttr
//!         
//!         On Error GoTo 0
//!     Next i
//! End Sub
//!
//! ' Usage
//! Sub MakeFilesReadOnly()
//!     Dim files() As String
//!     files = Array("file1.txt", "file2.txt", "file3.txt")
//!     
//!     SetAttributeForMultipleFiles files, vbReadOnly, True
//! End Sub
//! ```
//!
//! ## 5. File Security Analyzer
//!
//! ```vb
//! Type SecurityIssue
//!     Filename As String
//!     IssueType As String
//!     Severity As String
//! End Type
//!
//! Function AnalyzeFileSecurity(folderPath As String) As Collection
//!     Dim issues As New Collection
//!     Dim filename As String
//!     Dim fullPath As String
//!     Dim attr As Integer
//!     Dim issue As SecurityIssue
//!     
//!     filename = Dir(folderPath & "\*.*", vbNormal + vbHidden + vbSystem)
//!     
//!     Do While filename <> ""
//!         fullPath = folderPath & "\" & filename
//!         
//!         On Error Resume Next
//!         attr = GetAttr(fullPath)
//!         On Error GoTo 0
//!         
//!         If Not (attr And vbDirectory) Then
//!             ' Check for security issues
//!             
//!             ' Issue 1: System files that are not hidden
//!             If (attr And vbSystem) And Not (attr And vbHidden) Then
//!                 issue.Filename = filename
//!                 issue.IssueType = "System file not hidden"
//!                 issue.Severity = "Medium"
//!                 issues.Add issue
//!             End If
//!             
//!             ' Issue 2: Important files not marked read-only
//!             If InStr(1, filename, "config", vbTextCompare) > 0 Then
//!                 If Not (attr And vbReadOnly) Then
//!                     issue.Filename = filename
//!                     issue.IssueType = "Config file not read-only"
//!                     issue.Severity = "Low"
//!                     issues.Add issue
//!                 End If
//!             End If
//!         End If
//!         
//!         filename = Dir
//!     Loop
//!     
//!     Set AnalyzeFileSecurity = issues
//! End Function
//! ```
//!
//! ## 6. Attribute Comparison Tool
//!
//! ```vb
//! Function CompareFileAttributes(file1 As String, file2 As String) As String
//!     Dim attr1 As Integer
//!     Dim attr2 As Integer
//!     Dim differences As String
//!     
//!     On Error GoTo ErrorHandler
//!     
//!     attr1 = GetAttr(file1)
//!     attr2 = GetAttr(file2)
//!     
//!     differences = ""
//!     
//!     If (attr1 And vbReadOnly) <> (attr2 And vbReadOnly) Then
//!         differences = differences & "Read-Only differs" & vbCrLf
//!     End If
//!     
//!     If (attr1 And vbHidden) <> (attr2 And vbHidden) Then
//!         differences = differences & "Hidden differs" & vbCrLf
//!     End If
//!     
//!     If (attr1 And vbSystem) <> (attr2 And vbSystem) Then
//!         differences = differences & "System differs" & vbCrLf
//!     End If
//!     
//!     If (attr1 And vbArchive) <> (attr2 And vbArchive) Then
//!         differences = differences & "Archive differs" & vbCrLf
//!     End If
//!     
//!     If differences = "" Then
//!         CompareFileAttributes = "Attributes are identical"
//!     Else
//!         CompareFileAttributes = "Differences found:" & vbCrLf & differences
//!     End If
//!     
//!     Exit Function
//!     
//! ErrorHandler:
//!     CompareFileAttributes = "Error: " & Err.Description
//! End Function
//! ```
//!
//! # Error Handling
//!
//! ```vb
//! Function SafeGetAttr(pathname As String, _
//!                      Optional defaultValue As Integer = -1) As Integer
//!     On Error GoTo ErrorHandler
//!     
//!     SafeGetAttr = GetAttr(pathname)
//!     Exit Function
//!     
//! ErrorHandler:
//!     Select Case Err.Number
//!         Case 53  ' File not found
//!             Debug.Print "File not found: " & pathname
//!         Case 76  ' Path not found
//!             Debug.Print "Path not found: " & pathname
//!         Case 68  ' Device unavailable
//!             Debug.Print "Device unavailable: " & pathname
//!         Case Else
//!             Debug.Print "Error " & Err.Number & ": " & Err.Description
//!     End Select
//!     
//!     SafeGetAttr = defaultValue
//! End Function
//! ```
//!
//! Common errors:
//! - **Error 53 (File not found)**: The specified file doesn't exist.
//! - **Error 76 (Path not found)**: The specified path doesn't exist.
//! - **Error 68 (Device unavailable)**: Network drive or removable media not available.
//!
//! # Performance Considerations
//!
//! - `GetAttr` is a fast file system call
//! - Use `Dir` to check existence before calling `GetAttr` if unsure
//! - For multiple files, consider caching attribute values
//! - Network paths may be slower than local paths
//! - Accessing removable media can cause delays if not present
//!
//! # Best Practices
//!
//! 1. **Always use error handling** - file may not exist
//! 2. **Use bitwise AND** to test individual attributes
//! 3. **Check Dir first** if file existence is uncertain
//! 4. **Cache results** when checking attributes repeatedly
//! 5. **Use constants** (vbReadOnly, etc.) instead of numeric values
//! 6. **Handle network paths carefully** - may be unavailable
//! 7. **Don't assume attributes** - always check explicitly
//!
//! # Comparison with Other Functions
//!
//! ## `GetAttr` vs `FileLen`
//!
//! ```vb
//! ' GetAttr - Returns file attributes
//! attr = GetAttr("file.txt")  ' Returns attribute flags
//!
//! ' FileLen - Returns file size
//! size = FileLen("file.txt")  ' Returns size in bytes
//! ```
//!
//! ## `GetAttr` vs `Dir`
//!
//! ```vb
//! ' GetAttr - Gets attributes of specific file
//! attr = GetAttr("file.txt")
//!
//! ' Dir - Searches for files matching pattern
//! filename = Dir("*.txt", vbNormal)
//! ```
//!
//! ## `GetAttr` vs `FileDateTime`
//!
//! ```vb
//! ' GetAttr - Returns attributes
//! attr = GetAttr("file.txt")
//!
//! ' FileDateTime - Returns modification date/time
//! dt = FileDateTime("file.txt")
//! ```
//!
//! # Limitations
//!
//! - Returns Integer (limited to values 0-32767 due to VB6 Integer type)
//! - `vbAlias` only works on Macintosh systems
//! - Cannot set attributes (use `SetAttr` for that)
//! - No support for extended attributes or NTFS features
//! - Limited to file system attributes only
//! - Does not provide security descriptor information
//!
//! # Bitwise Operations
//!
//! Testing for specific attributes:
//!
//! ```vb
//! ' Test if read-only
//! If (attr And vbReadOnly) Then ' Has read-only attribute
//!
//! ' Test if NOT read-only
//! If Not (attr And vbReadOnly) Then ' Doesn't have read-only
//!
//! ' Test for multiple attributes
//! If (attr And (vbReadOnly Or vbHidden)) Then ' Has either attribute
//!
//! ' Test for exact attribute combination
//! If attr = (vbReadOnly Or vbHidden) Then ' Has exactly these two
//! ```
//!
//! # Related Functions
//!
//! - `SetAttr` - Sets the attributes of a file
//! - `Dir` - Returns a file or directory name matching a pattern
//! - `FileLen` - Returns the length of a file in bytes
//! - `FileDateTime` - Returns the date and time a file was created or last modified
//! - `FileAttr` - Returns the file mode or file handle for an open file
//! - `FileExists` - Checks if a file exists (custom function using Dir)

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn getattr_basic() {
        let source = r#"attr = GetAttr("C:\data.txt")"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_with_variable() {
        let source = r"fileAttr = GetAttr(filename)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_readonly_check() {
        let source = r#"If GetAttr("file.txt") And vbReadOnly Then MsgBox "Read-only""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_directory_check() {
        let source = r"If GetAttr(path) And vbDirectory Then isDir = True";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_hidden_check() {
        let source = r#"If GetAttr(fullPath) And vbHidden Then Debug.Print "Hidden""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_in_function() {
        let source = r"Function IsDirectory(path As String) As Boolean
    IsDirectory = (GetAttr(path) And vbDirectory) <> 0
End Function";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_assignment() {
        let source = r"Dim attr As Integer
attr = GetAttr(filename)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_error_handling() {
        let source = r"On Error GoTo ErrorHandler
attr = GetAttr(filename)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_system_check() {
        let source = r"If GetAttr(filename) And vbSystem Then Exit Sub";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_archive_check() {
        let source = r"needsBackup = (GetAttr(filename) And vbArchive) <> 0";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_multiple_checks() {
        let source = r#"If GetAttr(file) And vbReadOnly Then description = "Read-Only""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_comparison() {
        let source = r#"If GetAttr(filename) = vbNormal Then MsgBox "Normal file""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_not_operator() {
        let source = r"canModify = Not (GetAttr(filename) And vbReadOnly)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_select_case() {
        let source = r#"Select Case GetAttr(filename) And vbDirectory
    Case 0
        Debug.Print "File"
End Select"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_do_loop() {
        let source = r#"Do While filename <> ""
    attr = GetAttr(fullPath)
Loop"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_setattr() {
        let source = r"SetAttr filename, GetAttr(filename) And Not vbReadOnly";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_debug_print() {
        let source = r#"Debug.Print "Attributes: " & GetAttr(filename)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_class_member() {
        let source = r"m_Attributes = GetAttr(m_Filename)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_property() {
        let source = r"IsReadOnly = (GetAttr(filename) And vbReadOnly) <> 0";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_type_field() {
        let source = r"snapshot.Attributes = GetAttr(filename)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_or_operator() {
        let source = r"newAttr = GetAttr(filename) Or vbReadOnly";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_comparison_vars() {
        let source = r"If GetAttr(file1) <> GetAttr(file2) Then changed = True";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_for_loop() {
        let source = r"For i = LBound(files) To UBound(files)
    currentAttr = GetAttr(files(i))
Next i";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_msgbox() {
        let source = r#"MsgBox "File attributes: " & GetAttr(filename)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_listbox() {
        let source = r#"lst.AddItem filename & " (" & GetAttr(fullPath) & ")""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_iif() {
        let source = r#"result = IIf(GetAttr(filename) And vbReadOnly, "RO", "RW")"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn getattr_on_error_resume() {
        let source = r"On Error Resume Next
attr = GetAttr(fullPath)
On Error GoTo 0";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");
        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/file/getattr");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}