统计指定目录下的文件和文件夹个数总是出错!!
Sub统计指定目录中的文件及文件夹个数()DimnAsStringDimaAsLong,bAsLongDimmAsStringm="E:\"IfRight(m,1)<>"...
Sub 统计指定目录中的文件及文件夹个数()
Dim n As String
Dim a As Long, b As Long
Dim m As String
m = "E:\"
If Right(m, 1) <> "\" Then m = m & "\"
n = Dir(m, vbNormal + vbDirectory)
Do While n <> ""
If GetAttr(m & n) And vbDirectory Then
a = a + 1
Else
b = b + 1
End If
n = Dir
Loop
MsgBox "文件夹数:" & a & vbNewLine & "文件数:" & b
End Sub
为什么我运行以上过程,得到的结果,只有m=根目录才会得到正确结果,而若不是跟目录,得到的结果,文件个数没有错,但文件夹个数中是多1个或多2个呢?? 展开
Dim n As String
Dim a As Long, b As Long
Dim m As String
m = "E:\"
If Right(m, 1) <> "\" Then m = m & "\"
n = Dir(m, vbNormal + vbDirectory)
Do While n <> ""
If GetAttr(m & n) And vbDirectory Then
a = a + 1
Else
b = b + 1
End If
n = Dir
Loop
MsgBox "文件夹数:" & a & vbNewLine & "文件数:" & b
End Sub
为什么我运行以上过程,得到的结果,只有m=根目录才会得到正确结果,而若不是跟目录,得到的结果,文件个数没有错,但文件夹个数中是多1个或多2个呢?? 展开
1个回答
展开全部
那是因为子文件夹内有.和..文件夹(本文件夹和上级文件夹),你显示获取的所有的文件夹就知道了!而根目录下没有!
追问
如何才能得到正确值呢,比如在累计之前能限制什么条件,而能得到正确的值!!
追答
Private Sub Command1_Click()
Dim n As String
Dim a As Long, b As Long
Dim m As String
m = "E:\test201005\"
If Right(m, 1) "\" Then m = m & "\"
n = Dir(m, vbNormal + vbDirectory)
Do While n ""
If n "." And n ".." Then
If GetAttr(m & n) And vbDirectory Then
a = a + 1
Else
b = b + 1
End If
End If
n = Dir
Loop
MsgBox "文件夹数:" & a & vbNewLine & "文件数:" & b
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询