Dir Function


Dir Function

Class

Microsoft.VisualBasic.FileSystem

Syntax

     Dim result As String = Dir([pathname[, attributes]]) 


pathname (optional; String)

A filename or directory, with optional wildcard characters * and ? in the filename component.


attributes (optional; FileAttribute enumeration)

One or more of the following Microsoft.VisualBasic.FileAttribute enumeration values, added or Or'd together, specifying the file attributes to be matched:

Value

Description

Normal

Normal file (one that has neither the Hidden nor the System flag set)

ReadOnly

Read-only flag set on file

Hidden

Hidden file

System

System file

Volume

Volume label; must be used without any other attributes or enumeration values

Directory

Directory or folder

Archive

Archive flag set on file


If omitted, but pathname is supplied, Normal is used.

Description

The Dir function returns the name of a single file or directory matching the pattern and attribute mask passed to the function. One or both of the arguments must be supplied the first time the Dir function is called; this call returns the first match based on the supplied arguments. Subsequent calls do not include any arguments and return the next match based on the original use of the function. When there are no more matches, Dir returns an empty string.

Usage at a Glance

  • If attributes is not specified, files matching pathname are returned regardless of the attributes defined for those files.

  • You can use the wildcard characters * and ? within pathname to return multiple files.

  • Using Volume for attributes returns the volume label for the drive specified by pathname instead of a matching filename.

  • In previous versions of Visual Basic, the Dir function was commonly used to determine whether a particular file existed. Although it can still be used for this purpose, the System.IO.File.Exists method is more straightforward. In Visual Basic 2005, the My.Computer.FileSystem.FileExists method also checks for the existence of a file.

  • The Dir function returns filenames in the order in which they appear in the file-allocation table.

  • The Dir function saves its state between invocations. This means that the function cannot be called recursively. For example, if the function returns the name of the directory, you cannot then call the Dir function to iterate the files in that directory and then return to the original directory.

  • If you are calling the Dir function to return the names of one or more files, you must provide an explicit file specification. For instance, if you want to retrieve the names of all files in the Windows directory, the function call:

         fileMatch = Dir("C:\windows", FileAttribute.Normal) 

    fails. Instead, the Dir function must be called with pathname defined as follows:

         fileMatch = Dir("C:\windows\*.*", FileAttribute.Normal) 

Example

     Private Sub Button1_Click(ByVal sender As System.Object, _           ByVal e As System.EventArgs) Handles Button1.Click        ' ----- Add all matching files to the list.        Dim matchingFile As String        ListBox1.Items.Clear(  )        matchingFile = Dir(TextBox1.Text)        Do While (matchingFile <> "")           ListBox1.Items.Add(matchingFile)           matchingFile = Dir(  )        Loop     End Sub 

Version Differences

Visual Basic 2005 includes a My.Computer.FileSystem.GetDirectoryInfo method that provides related functionality. The My.Computer.FileSystem.FileExists method can be used to test for the existence of a file.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net