1 Attachment(s)
FileSystem Object, Folder.Files.Count Does Not Match Files.Item Count
Excel Experts,
I realize the subject of this post is not tied directly to Excel but I have encountered it while working in an Excel project and hoping due to the volume of participation in this forum that someone else might have experienced the phenomenon and have a solution.
A few years ago I put together a project using the FileSystemObject that would create a list in Word or Excel of what I "thought" were all files in a folder and subfolders. Recently, I was informed that process was sometimes not returning 100% of the files and was painfully too slow for practical use dealing with very large folders.
In my attempt to determine why all files were listed, I stumbled upon the subject of this post. There are situations when the FileSystemObject will return a file count that matches the actual number of files in a folder, but the IFilesCollection of that folder does not match as the following illustrates.
Attachment 20177
As best I can tell, the issue is related to deep nested subfolders. I assume this because if I move the problematic folder to a different location with a shorter path, the file count and item count then match.
Here is my code:
Code:
Sub Test()
Dim strFile As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim lngIndex As Long
Dim strFolder As String
strFolder = "D:\My Documents\Word\Word Documents\Word Tips\Macros\Word FAQ Pages and Code\Customization\Templates\How to create a template that makes it easy for users to fill in the blanks, without doing any programming_files\FillinTheBlanksContent_files"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strFolder)
Debug.Print oFolder.Files.Count
For Each oFile In oFolder.Files
Debug.Print oFile.Name
Next
End Sub
If I move the folder "FillinTheBlanksContent_files" to the root of my D:\ drive this problem goes away.
Anyone else seen this or know of a way to ensure the FileSystemObject with faithfully return "ALL" files?
Thanks.