1

I am using the following function to count files of a certain file type sFileType in folder sPath.

Function CountFilesInFolder(sPath As String, Optional sFileType As String) As Long

    Dim vFile As Variant
    Dim lFileCount As Long
    
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    vFile = Dir(sPath & sFileType)
    While (vFile <> "")
        lFileCount = lFileCount + 1
        vFile = Dir
    Wend
    
    CountFilesInFolder = lFileCount
End Function

When testing the function on a folder that contains:

  • 2 xls files and
  • 3 xlsx files

With

Debug.Print CountFilesInFolder(“C:\test\”, “*.xls”)

I would expect it to return 2, however, the function is also counting the xlsx files and it returns 5.

If I test the function

Debug.Print CountFilesInFolder(“C:\test\”, “*.xlsx”)

it returns 3, like expected. Why does the function in the first example also count the xlsx files? I am not specifying any wildcard, but still Dir behaves like it. Am I doing something wrong here? I could probably add an If statement in the While/Wend, but I assume I am doing something wrong with the Dir function.

braX
  • 11,506
  • 5
  • 20
  • 33
Michael Wycisk
  • 1,590
  • 10
  • 24
  • 1
    Does this answer your question? [dir function finds xls not xlsx in excel vba](https://stackoverflow.com/questions/6534671/dir-function-finds-xls-not-xlsx-in-excel-vba) – braX Nov 09 '22 at 22:57
  • 1
    Maybe related - https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir#:~:text=The%20asterisk%20wildcard%20always%20uses%20short%20file%20name%20mapping%2C%20so%20you%20might%20get%20unexpected%20results.%20For%20example%2C%20the%20following%20directory%20contains%20two%20files%20(t.txt2%20and%20t97.txt)%3A Short names for files with xlsx extension end with ".XLS" – Tim Williams Nov 09 '22 at 22:58
  • @braX Thank you for that suggestion. Maybe I misunderstand something, but using `"*.xls?"` og `"*.xls*"` as a file filter as suggested in the upvoted answer does not solve the problem. I still receive `5` as result. – Michael Wycisk Nov 09 '22 at 23:03
  • 4
    @MichaelWycisk You are [supposed to](https://devblogs.microsoft.com/oldnewthing/20140313-00/?p=1513). – GSerg Nov 09 '22 at 23:05
  • @GSerg - that was the link I was looking for... – Tim Williams Nov 09 '22 at 23:07

1 Answers1

3

From: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir#:~:text=The%20asterisk%20wildcard%20always%20uses%20short%20file%20name%20mapping%2C%20so%20you%20might%20get%20unexpected%20results.%20For%20example%2C%20the%20following%20directory%20contains%20two%20files%20(t.txt2%20and%20t97.txt)%3A

The asterisk wildcard always uses short file name mapping, so you might get unexpected results.

Windows short name for files with (eg) "xlsx"/"xlsm"/etc extension all end with ".XLS"

More-detailed overview at GSerg's link in the comments above.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Thank you! So the issue is the same as described in the link from @GSerg. Is there any more elegant solution then adding something like `If Right(vFile, 3) = "xls" Then` in the `While/Wend` loop? – Michael Wycisk Nov 09 '22 at 23:16
  • 2
    You could use `If vFile Like sFileType Then` and I guess that would filter out the false positives? – Tim Williams Nov 09 '22 at 23:22
  • Check this out: https://vmlogger.com/excel/2011/11/how-to-get-list-of-all-files-in-a-folder-and-sub-folders/ Toward the bottom of the page you can click a button named 'Download File Manager' and download an Excel file that will do the work for you. – ASH Nov 10 '22 at 13:29