Curious problem with counting files

Sambodh

New member
Local time
Today, 12:14
Joined
Nov 29, 2016
Messages
7
Hi,

I am using FileSystemObject to count the number of files in a folder and up until a few months ago it always gave me the right answer in all the environments I use. Then suddenly the Windows 8.1 Pro development PC started to give an answer of 1 for all empty folders, whilst the Windows 10 PC gave the correct zero answer and so did the production environment on Amazon AWS where the server runs Windows Server 2012 R2.

Today I determined to find out the cause and examined the versions of scrrun.dll on the 3 systems. To my astonishment the Windows 8.1 and Server 2012 R2 versions were 5.8.9600.17415 whilst Windows 10 was running with version 5.812.10240.16384. The production version on the server is running an accde file and gives the correct answer, and the same accde gives the wrong answer on the development Win 8.1 PC.

Can anyone explain how this is possible? The actual line of code is:

intInvoiceCount = objFSO.GetFolder(DBPath & "\MMEMReprocessing").Files.Count

Many thanks in advance,

Sambodh
 
up until a few months ago it always gave me the right answer in all the environments I use. Then suddenly the Windows 8.1 Pro development PC started to give an answer of 1 for all empty folders

Are you saying that it used to be right for 8.1 also, but then stopped? If you can figure out when that happened, you might be able to pin it on a particular Windows update, because they show up in the "Installed programs" list. You might be able to see if others have reported squirrely results for directory file counts.

Don't forget that directories other than the root of any physical disk will have a directory entry pointing to the parent folder (and when doing a Command Prompt DIR command, it shows up as ".."). If that is being counted as a file suddenly, there's your culprit, though why only that version does it? Damned if I know.
 
In case anyone want to see if they have the same problem on their systems I've attached is a simple application with a folder picker that runs the problem code and display the file count in a msgbox.
 

Attachments

Hi Doc Man,

Unfortunately I've been back through my work book and cannot find a reference to when this first started happening in the Win8.1Pro development environment. At the time, I was shocked at the sudden appearance of buttons which are made visible only when there are files in the relevant folders, but when I discovered that the production environment did not exhibit the anomaly, I gave up searching for an answer and continued with more pressing upgrades.

Now I have a bit of a breathing space as the programming year down in Australia draws to a close, so would like to put this weirdness to bed...

Sambodh
 
Hi sneuberg,

Thanks for the code snippet.

As expected, it gives file count of 1 on Win8.1Pro development PC and 0 on Win10 staging PC.

At least it's reproducible!!

Sambodh
 
Hidden system files (thumbs.db or windows.ini) would be my guess.
 
Hi Static,

View is set to show hidden files and there are none. The folders are empty.

Sambodh
 
What happens if you try to output the name of the phantom file?
 
Hi Static,

View is set to show hidden files and there are none. The folders are empty.

Sambodh

There's a separate setting for sys files.

But as Cronk said. Print the list out and see what the files are. eg

Code:
set fls = objFSO.GetFolder(DBPath & "\MMEMReprocessing").Files
for each f in fls
   debug.print f.name
next
 
Hi Static,

I used your code and it is indeed a thumbs.db file causing the problem.

My File Explorer won't display it even if I tick Hidden files.

Not sure of the best way to dispose of it!!?? Deleting in the cmd window won't do it. My gpedit attempt to change Local Group Policy at

Turn off the caching of thumbnails in hidden thumbs.db files

won't take on my Win8.1 PC, it throws all sorts of .NET errors. Perhaps Disk Clean to remove Thumbnails?

Sambodh
 
Explorer > organise > folder and search options > view > untick hide protected operating system files

But deleting the file isn't the answer.

Your .Files.Count is returning the correct value, it's just not the value you want it to be.
Therefore, you need to filter out the files that you don't want to be counted.

Code:
Sub test()
    Debug.Print CountFiles(DBPath & "\MMEMReprocessing", False)
End Sub

Function CountFiles(path As String, IncludeIfHidden As Boolean) As Integer
    With CreateObject("Scripting.FileSystemObject")
        With .getfolder(path)
            If Not IncludeIfHidden Then
                For Each f In .Files
                    If Not f.Attributes And 2 Then
                        CountFiles = CountFiles + 1
                    Else
                        Debug.Print "File not counted - " & f.Name
                    End If
                Next
            Else
                CountFiles = .Files.Count
            End If
        End With
    End With
End Function
 
Hi Static,

I've just implemented your function and have to thank you for restoring my faith in Microsoft Access & FSO!

It's been my first query to this forum, so it's been a very positive experience.

Sambodh
 

Users who are viewing this thread

Back
Top Bottom