' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
' Display entry only if it's a directory.
MsgBox(MyName)
End If
MyName = Dir() ' Get next entry.
Loop
我正在看上面的代码。我特别不明白“ MyName = Dir()”的作用。有人评论说它获取下一个条目,但是我不明白它如何获取下一个条目-特别是Dir()在做什么?
Dir
是具有边缘效果的功能。
第一次调用Dir
:MyName = Dir(MyPath, vbDirectory)
初始化Dir
内部结构并返回第一个目录条目。
随后的调用Dir
使用相同的上下文,MyPath
一一生成目录内容。
它不是可重入的(这也是为什么您不能使用Dir嵌套/递归多个循环的原因),它不是很优雅,但这就是它的工作原理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句