Junctions Break Folder Collection

I'm trying to create a VB script that enumerates ALL files and folders on a disk.

I think I've got it working as it should BUT, when running on Windows 8.1 the "Documents and Settings" Junction/Symbolic Link breaks the script.  It enumerates everything from the root directory and all folders and sub folders UNTIL it hits documents and settings and then it stops.  

As near as I can tell, when I ask for Subfolders, it gets the collection only up until the junction... or else something is breaking it in a REALLY bad way as I'm using On Error resume Next.

The following code below is what I'm currently testing with.
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
StartFolder = "C:\"
Set CurrentFolder = FSO.GetFolder(StartFolder)
'Wscript.Echo CurrentFolder.Path
Set FolderFiles = CurrentFolder.Files
WScript.Echo CurrentFolder.Name
For Each File in FolderFiles
	WSCript.Echo File.Name

ShowSubfolders FSO.GetFolder(StartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
		WScript.Echo Subfolder.Name
		'Wscript.Echo Subfolder.Path & "; Attribs: " & Subfolder.Attributes
		Set CurrentFolder = FSO.GetFolder(Subfolder.Path)
		Set FolderFiles = CurrentFolder.Files
		If CurrentFolder.SubFolders.Count > 0 Then
			For Each File in FolderFiles
				If Err.Number = 0 Then
					WScript.Echo File.DateCreated & vbTab & File.DateLastModified & vbTab & File.Size & vbTab & File.Path
					WScript.Echo "Sub(2): " & Err.Number & ": " & Err.Description
				End If
			WScript.Echo "No files found in " & Subfolder.Path
		End If
		ShowSubFolders Subfolder
End Sub

Open in new window

Script Results and expected Directories to Check
Bottom line, how do I fix this - I don't necessarily want to follow the junction, skipping it is fine (it's Users anyway and SHOULD be checked later... how do I get the script to move on to the next ACTUAL folder?
LVL 98
Lee W, MVPTechnology and Business Process AdvisorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
If you want more info, search for "reparse points". This particular instance you get back as part of subfolders but you cannot access as an actual directory. It could possibly point to a different drive so sometimes you need to think about how you would want to handle that, but maybe not in this case.

Be careful with On Error Resume Next. An error in the Sub will just exit the sub and resume with the next statement after the call to the sub. You could repeat the On Error Resume Next inside the sub but if you're not careful about checking results (for example from getfolder) you could create an endless loop or recursion.

I think the simplest you can do if you want to skip them is, before line 18 insert this line:
If (Subfolder.Attributes And 1024) = 0 Then ' skip junction

Open in new window

and before the Next on line 33 (34 by now) insert an End If.

By the way, it looks to me like 'Set CurrentFolder' in the Sub is not necessary since you can use the Subfolder variable directly to check for count.

Also, are you aware that at your line 21 in the posted code you check for CurrentFolder.SubFolders.Count where I think it should be CurrentFolder.Files.Count (or FolderFiles.Count)?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lee W, MVPTechnology and Business Process AdvisorAuthor Commented:
The script I posted was a trimmed down version of the main one that I used for testing and debugging... But thanks for all your comments - when I add the if block you suggest, it seems to work fine.  Can you elaborate as to how that code works?  I found https://msdn.microsoft.com/en-us/library/5tx15443%28v=vs.84%29.aspx and the break down of the attributes... but I'm not sure I understand how the AND works to evaluate it (it might be might not fully understanding "AND" in a mathematical sense, but if you could explain, I'd be grateful.
Robert SchuttSoftware EngineerCommented:
Sure. It's a bit check. The "Alias" bit is set on for these folders.

And can be used in different ways, see here under Bitwise Operations.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.