Home > Enterprise >  Get file size (FileLen() of a file that has unicode characters in the name [Excel VBA]
Get file size (FileLen() of a file that has unicode characters in the name [Excel VBA]

Time:10-05

I am trying to list the contents of a directory in Windows. All works as expected but when I try to get the file size of the files (Debug.Print FileLen(filepath & filename) using the function FileLen() I get an error 53 on the second file because it has a unicode character in the name (a fullwidth number sign U FF03). Any ideas how to deal with this?

Content of the directory c:\Z\Test\:

1#.txt
2#.txt

Note that the first file has a "normal" number sign and the second file name has a fullwidth number sign (U FF03). Here the VBA Code which works fine without the second Debug.Print.

Sub ListFiles()
Dim filename As String
Dim filepath As String
filepath = "c:\Z\Test\"
filename = Dir(filepath)
Do While filename <> ""
   Debug.Print filename
   Debug.Print FileLen(filepath & filename)
   filename = Dir()
Loop
End Sub

CodePudding user response:

Please, try the next way. VBScript objects are able to deal with non ASCII characters in the file name:

Sub ListFilesNonASCIIName()
   Dim FSOLibrary As Object, fldObj As Object, fsoFile As Object
   Dim foldPath As String
   Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
    foldPath = "c:\Z\Test\"
    Set fldObj = FSOLibrary.GetFolder(foldPath)
    For Each fsoFile In fldObj.files
         Debug.Print fsoFile.Name, fsoFile.Size
    Next
End Sub
  • Related