Export (0) Print
Expand All

Dir Function

Returns a string representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.

Public Overloads Function Dir() As String


Public Overloads Function Dir( _
   Optional ByVal PathName As String, _ 
   Optional ByVal Attributes As FileAttribute = FileAttribute.Normal _
) As String


Optional. String expression that specifies a file name, directory or folder name, or drive volume label. A zero-length string ("") is returned if PathName is not found.
Optional. Enumeration or numeric expression whose value specifies file attributes. If omitted, returns files that match PathName, but have no attributes.


The Attributes argument enumeration values are as follows:

Value Constant Description
Normal vbnormal Default. Specifies files with no attributes.
ReadOnly vbReadOnly Specifies read-only files in addition to files with no attributes.
Hidden vbHidden Specifies hidden files in addition to files with no attributes.
System vbSystem Specifies system files in addition to files with no attributes.
Volume vbVolume Specifies volume label; if any other attribute is specified, vbVolume is ignored.
Directory vbDirectory Specifies directories or folders in addition to files with no attributes.
Archive vbArchive File has changed since last backup.
Alias vbAlias File has a different name.
Note   These enumerations are specified by the Visual Basic language and can be used anywhere in your code in place of the actual values.


The Dir function supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files.

You must supply a PathName the first time you call the Dir function. Subsequent calls to the Dir function may be made with no parameters to retrieve the next item.

Security Note   Do not make decisions about the contents of a file based on the file name extension. For example, a file named Form1.vb may not be a Visual Basic .NET source file.


This example uses the Dir function to check if certain files and directories exist.

Dim MyFile, MyPath, MyName
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")   

' Returns filename with specified extension. If more than one *.INI
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in the
' same directory.
MyFile = Dir()

' Return first *.TXT file, including files with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\"   ' Set the path.
MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
Do While MyName <> ""   ' Start the loop.
      ' Use bitwise comparison to make sure MyName is a directory.
      If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
         ' Display entry only if it's a directory.
      End If   
   MyName = Dir()   ' Get next entry.

Smart Device Developer Notes

This function is not supported.


Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Microsoft Visual Basic .NET Runtime (in MicrosoftVisualBasic.dll)

See Also

ChDir Function | CurDir Function | FileAttribute Enumeration

© 2014 Microsoft