gpt4 book ai didi

vba - dir()函数理解

转载 作者:行者123 更新时间:2023-12-04 22:08:28 29 4
gpt4 key购买 nike

' 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 internals 并返回第一个目录条目。

随后调用 Dir使用相同的上下文,产生 MyPath目录内容一一。

它不是可重入的(这也是为什么你不能使用 Dir 嵌套/递归多个循环的原因),不是很优雅,但这就是它的工作原理。

关于vba - dir()函数理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39273901/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com