FSO returns 个不存在的子文件夹
FSO returns non-existing subfolders
我正在使用此代码获取目录的子文件夹:
Dim fo As Scripting.Folder
Set fo = fso.GetFolder(m_sFolder)
Dim nSubfolder As Scripting.Folder
For Each nSubfolder In fo.SubFolders
Debug.Print "Folder " & fo.Path & " has subfolder " & nSubfolder
Next
现在 m_sFolder 为 "C:\Users\MyUser\Documents" 时,一个子文件夹为 "C:\Users\MyUser\Documents\Eigene Bilder"。
"Eigene Bilder" 是 Windows 在德语中对文件夹 "My Pictures" 的称呼。
但是,文件夹 "C:\Users\MyUser\Documents" 不包含 "My Pictures"、"Pictures" 或 "Eigene Bilder"。
文件夹 "My Pictures" 可在此处找到:
C:\Users\MyUser\Pictures
谁能告诉我为什么 FSO 可能想告诉我这个目录 "C:\Users\MyUser\Documents\Eigene Bilder" 存在?
我完全莫名其妙。
它不是一个目录,它是一个 Junction (or Reparse) Point,就像在文件系统级别重定向到另一个位置。
dir "C:\Users\MyUser\Documents\" /ad
命令行将使用 <JUNCTION>
标记列出这些(与 <DIR>
相对)。
不需要使用FSO,内置的文件系统函数将不包括这些:
Dim path As String: path = "C:\Users\MyUser\Documents\"
Dim dirn As String
dirn = Dir$(path, vbDirectory)
Do While dirn <> ""
If (GetAttr(path & dirn) And vbDirectory) = vbDirectory And dirn <> "." And dirn <> ".." Then
Debug.Print path & dirn
End If
dirn = Dir$()
Loop
如果你坚持使用FSO,你需要了解这些事情。这个例子试图让你知道,并且应该给你处理这个问题所需的信息:
Const ssfPERSONAL = 5
Const FILE_ATTRIBUTE_REPARSE_POINT = &H400&
Dim TargetFolderPath As String
Dim SubFolder As Scripting.Folder
Dim SubFile As Scripting.File
'Don't early-bind to Shell32 objects, Microsoft has failed
'to maintain binary compatibility across Windows versions:
TargetFolderPath = CreateObject("Shell.Application").NameSpace(ssfPERSONAL).Self.Path
Debug.Print TargetFolderPath
With New Scripting.FileSystemObject
With .GetFolder(TargetFolderPath)
For Each SubFolder In .SubFolders
With SubFolder
Debug.Print .Name;
Debug.Print " ["; .Type;
If .Attributes And FILE_ATTRIBUTE_REPARSE_POINT Then
Debug.Print ", reparse point";
End If
Debug.Print "]"
End With
Next
For Each SubFile In .Files
With SubFile
Debug.Print .Name; " ["; .Type; "]"
End With
Next
End With
End With
我正在使用此代码获取目录的子文件夹:
Dim fo As Scripting.Folder
Set fo = fso.GetFolder(m_sFolder)
Dim nSubfolder As Scripting.Folder
For Each nSubfolder In fo.SubFolders
Debug.Print "Folder " & fo.Path & " has subfolder " & nSubfolder
Next
现在 m_sFolder 为 "C:\Users\MyUser\Documents" 时,一个子文件夹为 "C:\Users\MyUser\Documents\Eigene Bilder"。 "Eigene Bilder" 是 Windows 在德语中对文件夹 "My Pictures" 的称呼。
但是,文件夹 "C:\Users\MyUser\Documents" 不包含 "My Pictures"、"Pictures" 或 "Eigene Bilder"。
文件夹 "My Pictures" 可在此处找到: C:\Users\MyUser\Pictures
谁能告诉我为什么 FSO 可能想告诉我这个目录 "C:\Users\MyUser\Documents\Eigene Bilder" 存在?
我完全莫名其妙。
它不是一个目录,它是一个 Junction (or Reparse) Point,就像在文件系统级别重定向到另一个位置。
dir "C:\Users\MyUser\Documents\" /ad
命令行将使用 <JUNCTION>
标记列出这些(与 <DIR>
相对)。
不需要使用FSO,内置的文件系统函数将不包括这些:
Dim path As String: path = "C:\Users\MyUser\Documents\"
Dim dirn As String
dirn = Dir$(path, vbDirectory)
Do While dirn <> ""
If (GetAttr(path & dirn) And vbDirectory) = vbDirectory And dirn <> "." And dirn <> ".." Then
Debug.Print path & dirn
End If
dirn = Dir$()
Loop
如果你坚持使用FSO,你需要了解这些事情。这个例子试图让你知道,并且应该给你处理这个问题所需的信息:
Const ssfPERSONAL = 5
Const FILE_ATTRIBUTE_REPARSE_POINT = &H400&
Dim TargetFolderPath As String
Dim SubFolder As Scripting.Folder
Dim SubFile As Scripting.File
'Don't early-bind to Shell32 objects, Microsoft has failed
'to maintain binary compatibility across Windows versions:
TargetFolderPath = CreateObject("Shell.Application").NameSpace(ssfPERSONAL).Self.Path
Debug.Print TargetFolderPath
With New Scripting.FileSystemObject
With .GetFolder(TargetFolderPath)
For Each SubFolder In .SubFolders
With SubFolder
Debug.Print .Name;
Debug.Print " ["; .Type;
If .Attributes And FILE_ATTRIBUTE_REPARSE_POINT Then
Debug.Print ", reparse point";
End If
Debug.Print "]"
End With
Next
For Each SubFile In .Files
With SubFile
Debug.Print .Name; " ["; .Type; "]"
End With
Next
End With
End With