Dir()函数“。”和“......”

时间:2014-12-09 15:05:59

标签: ms-access access-vba

我正在通过别人的一些VBA代码来循环浏览根目录及其子目录中的文件:

Public Function RecursiveDir(colFiles As Collection, _
                             strFolder As String, _
                             strFileSpec As String, _
                             bIncludeSubfolders As Boolean)

     Dim strTemp As String
     Dim colFolders As New Collection
     Dim vFolderName As Variant

' Add files in strFolder matching strFileSpec to colFiles:

     strFolder = TrailingSlash(strFolder)
     strTemp = Dir(strFolder & strFileSpec)

     Do While strTemp <> vbNullString

         colFiles.Add strFolder & strTemp
         strTemp = Dir

     Loop

' Include Subfolders:

     If bIncludeSubfolders Then

     'Fill colFolders with list of subdirectories of strFolder

         strTemp = Dir(strFolder, vbDirectory)

         Do While strTemp <> vbNullString

             If (strTemp <> ".") And (strTemp <> "..") Then

                 If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then

                     colFolders.Add strTemp

                 End If

             End If

             strTemp = Dir

         Loop

     'Call RecursiveDir for each subfolder in colFolders

         For Each vFolderName In colFolders

             Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)

         Next vFolderName

     End If

End Function

我已经理解了大部分内容,但在某些情况下,我对Dir功能的工作方式有点挣扎。

具体来说,测试以下内容的行:

If (strTemp <> ".") And (strTemp <> "..") Then

似乎暗示前面的strTemp = Dir(strFolder, vbDirectory)可以评估为&#34;。&#34;或&#34; ..&#34;

我明白为什么我不希望它们被添加到我的收藏中,但是&#34;。&#34;和&#34; ..&#34; Dir(strFolder, vbDirectory)的结果表示?

1 个答案:

答案 0 :(得分:4)

  • &#39;&#39;表示当前目录
  • &#39; ..&#39;表示父目录

他们是由&#39; dir&#39;命令。在你的情况下(以及我见过的任何其他)他们只是妨碍了,所以代码特别忽略了它们