VBA/Excel/Access/Word/Forms/Dir

Материал из VB Эксперт
Перейти к: навигация, поиск

Dir() Returns a string representing the name of a file or directory with specified pattern.

 
Sub strDemo19()
   Debug.Print Dir("C:\Windows\*.ini")
End Sub



Get Directory by using the Dir function

 
Sub DirDemo2()
    MsgBox Dir
End Sub



Get the file names

 
Sub FileNames()
  Dim FName As String
  Dim FNames() As String
  Dim FType As String
  Dim i As Integer
  
  FType = "*.xls"
  FName = Dir(FType)
  Do Until FName = ""
    i = i + 1
    ReDim Preserve FNames(1 To i)
    FNames(i) = FName
    FName = Dir
  Loop
  If i <> 0 Then
    For i = 1 To UBound(FNames)
      MsgBox FNames(i)
    Next i
  End If
End Sub



Use Dir function to get the file directory

 
Sub DirDemo()
  Dim FType As String
  FType = "*.xls"
  MsgBox Dir(FType)
  
End Sub



Validate the file path

 
Function ValidPath() As Boolean
    Dim fPath As String
    fPath = "c:\"
    If Dir(fPath, vbDirectory) = "" Or fPath = "" Then
        ValidPath = False
    Else
        ValidPath = True
    End If
End Function