This article was previously published under Q104685
This article has been archived. It is offered "as is" and will no longer be updated.
This article shows by example how to use the Dir[$]() function inconjunction with the GetAttr() function to retrieve read-only, hidden,or system files.
The Dir[$] functions take a filespec and an attrmask as optionalarguments.
If the attrmask argument specifies the volume label, the Dir[$] functionsignores all other attributes. If the attrmask argument is ATTR_HIDDEN,ATTR_SYSTEM, or ATTR_DIRECTORY, the functions also return the files thatdo not have any special attributes.
If the filespec argument is used, the functions return files that do nothave any hidden, system, or directory attributes and meet the filespecrequirements.
To retrieve only read-only, hidden, or system files, use the Dir[$]()functions in conjunction with the GetAttr() function. The following showsby example how to retrieve only hidden files (files that have the HIDDENor ATTR_HIDDEN+ATTR_ARCHIVE attributes) by using the Dir() function inconjunction with the GetAttr() function.
Start Visual Basic or begin a new project if Visual Basic is already running. Form1 is created by default.
Place the following code in the general declarations area for Form1:
Add the following code to the command button's click event procedure:
Sub Command1_Click () Dim filename As String Dim attr As Integer ' retrieve hidden and normal files filename = Dir$("c:\", ATTR_HIDDEN) Do Until filename = "" attr = GetAttr("c:\" & filename) ' if the file has the hidden attribute If (attr And ATTR_HIDDEN) Then ' select it List1.AddItem filename End If filename = Dir$ Loop End Sub
Run the program and click the command button to see any existing hidden files in the root directory.