Επεξεργασία

Κοινή χρήση μέσω


FileSystem.Dir Method

Definition

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. The FileSystem gives you better productivity and performance in file I/O operations than the Dir function. See GetDirectoryInfo(String) for more information.

Overloads

Dir()

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. The FileSystem gives you better productivity and performance in file I/O operations than the Dir function. See GetDirectoryInfo(String) for more information.

Dir(String, FileAttribute)

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. The FileSystem gives you better productivity and performance in file I/O operations than the Dir function. See GetDirectoryInfo(String) for more information.

Dir()

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

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. The FileSystem gives you better productivity and performance in file I/O operations than the Dir function. See GetDirectoryInfo(String) for more information.

public:
 static System::String ^ Dir();
public static string Dir ();
static member Dir : unit -> string
Public Function Dir () As String

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.

Examples

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

Dim MyFile, MyPath, MyName As String
' 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.
        MsgBox(MyName)
    End If
    MyName = Dir()   ' Get next entry.
Loop

Remarks

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

VbVolume returns the volume label for the drive instead of a specific file name.

You must supply a PathName the first time that you call the Dir function. To retrieve the next item, you can make subsequent calls to the Dir function without parameters.

Important

To run correctly, the Dir function requires the Read and PathDiscovery flags of FileIOPermission to be granted to the executing code. For more information, see FileIOPermission, SecurityException, and Code Access Permissions.

The Attributes argument enumeration values are as follows:

Value Constant Description
Normal vbnormal Default. Specifies files without attributes.
ReadOnly vbReadOnly Specifies read-only files, and also files without attributes.
Hidden vbHidden Specifies hidden files, and also files without attributes.
System vbSystem Specifies system files, and also files without attributes.
Volume vbVolume Specifies volume label; if any other attribute is specified, vbVolume is ignored.
Directory vbDirectory Specifies directories or folders, and also files without 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 instead of the actual values.

See also

Applies to

Dir(String, FileAttribute)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

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. The FileSystem gives you better productivity and performance in file I/O operations than the Dir function. See GetDirectoryInfo(String) for more information.

[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static string Dir (string PathName, Microsoft.VisualBasic.FileAttribute Attributes = Microsoft.VisualBasic.FileAttribute.Normal);
public static string Dir (string PathName, Microsoft.VisualBasic.FileAttribute Attributes = Microsoft.VisualBasic.FileAttribute.Normal);
public static string Dir (string Pathname, Microsoft.VisualBasic.FileAttribute Attributes = Microsoft.VisualBasic.FileAttribute.Normal);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Dir : string * Microsoft.VisualBasic.FileAttribute -> string
static member Dir : string * Microsoft.VisualBasic.FileAttribute -> string
Public Function Dir (PathName As String, Optional Attributes As FileAttribute = Microsoft.VisualBasic.FileAttribute.Normal) As String
Public Function Dir (Pathname As String, Optional Attributes As FileAttribute = Microsoft.VisualBasic.FileAttribute.Normal) As String

Parameters

PathNamePathname
String

Optional. A 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.

Attributes
FileAttribute

Optional. Enumeration or numeric expression whose value specifies file attributes. If omitted, Dir returns files that match Pathname but have no attributes.

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.

Attributes

Examples

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

Dim MyFile, MyPath, MyName As String
' 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.
        MsgBox(MyName)
    End If
    MyName = Dir()   ' Get next entry.
Loop

Remarks

The Dir function supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files.
VbVolume returns the volume label for the drive instead of a specific file name.
You must supply a PathName the first time that you call the Dir function. To retrieve the next item, you can make subsequent calls to the Dir function with no parameters.

Important

To run correctly, the Dir function requires the Read and PathDiscovery flags of FileIOPermission to be granted to the executing code. For more information, see FileIOPermission, SecurityException, and Code Access Permissions.
The Attributes argument enumeration values are as follows:
|Value|Constant|Description|
|-|-|-|
|Normal|vbnormal|Default. Specifies files that have no attributes.|
|ReadOnly|vbReadOnly|Specifies read-only files, in addition to files that have no attributes.|
|Hidden|vbHidden|Specifies hidden files, in addition to files that have no attributes.|
|System|vbSystem|Specifies system files, in addition to files that have 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 that have 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.

See also

Applies to