Member Function Dir[(Pathname[,attributes])] As String |
Public Sub main() Dim filenames As String filenames = Dir("c:\", vbNormal + vbDirectory) Do While filenames <> "" Debug.Print filenames filenames = Dir ’再次调用dir函数,此时可以不带参数 Loop End Sub |
’将符合条件的条件和目录分类,存到对应的数组中,完成输出 Public Sub main() Dim FileNames As String, DirNames As String Dim FileArray() As String, DirArray() As String Dim TotalDirs As Integer, TotalFiles As Integer Dim iLoop As Integer DirNames = "c:\" FileNames = Dir(Dirname, vbNormal + vbDirectory) Do While FileNames <> "" If FileNames <> "." And FileNames <> ".." Then If (GetAttr(Dirname & FileNames) And vbDirectory) = vbDirectory Then TotalDirs = TotalDirs + 1 ReDim Preserve DirArray(TotalDirs) DirArray(TotalDirs) = FileNames Else TotalFiles = TotalFiles + 1 ReDim Preserve FileArray(TotalFiles) FileArray(TotalFiles) = FileNames End If End If FileNames = Dir Loop ’这里可对数组进行排序,此处略 Debug.Print "___Directories______" For iLoop = 0 To UBound(DirArray) Debug.Print DirArray(iLoop) Next Debug.Print "___Files____" For iLoop = 0 To UBound(FileArray) Debug.Print FileArray(iLoop) Next End Sub |