我正在使用以下函式來計算檔案夾中某種檔案型別sFileType的檔案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
在包含以下內容的檔案夾上測驗函式時:
- 2個
xls檔案和 - 3 個
xlsx檔案
和
Debug.Print CountFilesInFolder(“C:\test\”, “*.xls”)
我希望它回傳2,但是,該函式也在計算xlsx檔案并回傳5。
如果我測驗功能
Debug.Print CountFilesInFolder(“C:\test\”, “*.xlsx”)
它回傳 3,如預期的那樣。為什么第一個示例中的函式也計算xlsx檔案?我沒有指定任何通配符,但仍然Dir表現得像它。我在這里做錯了嗎?我可能會在 中添加一個If陳述句,但我認為我在函式中While/Wend做錯了什么。Dir
uj5u.com熱心網友回復:
來自:https ://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir#:~:text=The asterisk wildcard always uses short file name mapping , so you might get unexpected results. For example, the following directory contains two files (t.txt2 and t97.txt):
星號通配符始終使用短檔案名映射,因此您可能會得到意想不到的結果。
帶有(例如)“xlsx”/“xlsm”/etc 擴展名的檔案的 Windows 短名稱都以“.XLS”結尾
上面評論中的 GSerg 鏈接中的更詳細概述。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/530977.html
標籤:擅长vba
