VB详细枚举指定目录、文件夹文件列表:
Public Function TheFileAttr(P As String) As String '读取指定文件/文件夹属性
On Error GoTo Over
If (GetAttr(P) And vbNormal) = vbNormal Then TheFileAttr = "常规"
If (GetAttr(P) And vbReadOnly) = vbReadOnly Then TheFileAttr = TheFileAttr & "+只读"
If (GetAttr(P) And vbHidden) = vbHidden Then TheFileAttr = TheFileAttr & "+隐藏"
If (GetAttr(P) And vbSystem) = vbSystem Then TheFileAttr = TheFileAttr & "+系统"
If (GetAttr(P) And vbDirectory) = vbDirectory Then TheFileAttr = TheFileAttr & "+目录"
If (GetAttr(P) And vbArchive) = vbArchive Then TheFileAttr = TheFileAttr & "+存档"
If (GetAttr(P) And vbAlias) = vbAlias Then TheFileAttr = TheFileAttr & "+别名"
Exit Function
Over:
TheFileAttr = "无法识别"
End Function
Public Function DirList(P As String) As String '详细枚举指定路径下的文件
On Error Resume Next '只能用强行排错兼容某些特殊文件。
Dim NowList As String, DList As String, FList As String, Thelen As String '当前列表,列表列表,文件列表,文件/目录大小,属性
If Right(P, 1) <> "\" Then P = P & "\" '判断最后一位是否为"\",添加"\"防止搜索出错
NowList = Dir(P, vbHidden + vbNormal + vbReadOnly + vbSystem + vbDirectory) '枚举目录列表
While NowList <> "" '搜索当前目录,直到结果为空
If NowList <> "." And NowList <> ".." Then '排除上级目录和当前目录
If (GetAttr(P & NowList) And vbDirectory) <> vbDirectory Then '判断属性
Thelen = FileLen(P & NowList) '获取文件大小
If Thelen < 1 Then Thelen = Thelen / 1024 & " KB" Else Thelen = Thelen & " Bytes" '处理不足1Kb的
FList = FList & NowList & "|文件|" & Thelen & "|" & TheFileAttr(P & NowList) & "|" & FileDateTime(P & NowList) & vbCrLf '如果找到的是文件,则累加文件'文件名,类型,大小,属性,创建日期
Else
DList = DList & NowList & "|文件夹|0 KB|" & TheFileAttr(P & NowList) & "|" & FileDateTime(P & NowList) & vbCrLf '如果找到的是目录,则累加目录'文件名,类型,大小,属性,创建日期
End If
End If
NowList = Dir() '继续获取
Wend
DirList = DList & FList
Over:
If Len(DirList) = 0 Then DirList = "查找错误,或目录不存在!,或目录为空!"
End Function
Private Sub Command1_Click()
MsgBox DirList("c:\")
End Sub |