How to Retrieve Hidden/System Files Using Dir[$]() Function

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.
More information
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.

Step-by-Step Example

  1. Start Visual Basic or begin a new project if Visual Basic is already running. Form1 is created by default.
  2. Place the following code in the general declarations area for Form1:
       Const ATTR_NORMAL = 0   Const ATTR_READONLY = 1   Const ATTR_HIDDEN = 2   Const ATTR_SYSTEM = 4   Const ATTR_VOLUME = 8   Const ATTR_DIRECTORY = 16   Const ATTR_ARCHIVE = 32						
  3. Add a List box and a command button to Form1.
  4. 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						
  5. Run the program and click the command button to see any existing hidden files in the root directory.
2.00 3.00 Dir Dir$ GetAttr docerr

Article ID: 104685 - Last Review: 10/26/2013 11:09:00 - Revision: 3.0

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • kbnosurvey kbarchive KB104685