Dir Command fails on folders with foreign names

Dir Command fails on folders with foreign names

Post by Lost in Un » Wed, 23 Feb 2005 20:57:01


The DIR function fails with Error 52 Bad file name if the folder has a
name containing foreign characters. Is there an api which can be used to
ascertain if a folder exists which also works on such folder names?

Thank you
 
 
 

Dir Command fails on folders with foreign names

Post by Mike D Sut » Wed, 23 Feb 2005 21:52:59

> The DIR function fails with Error 52 Bad file name if the folder has a

Have a look at the FindFirstFileW()/FindNextFileW() API calls:

'***
Private Declare Function FindFirstFile Lib "Kernel32.dll" Alias "FindFirstFileW" ( _
ByRef lpFileName As Any, ByRef lpFindFileData As Win32_Find_DataW) As Long
Private Declare Function FindNextFile Lib "Kernel32.dll" Alias "FindNextFileA" ( _
ByVal hFindFile As Long, ByRef lpFindFileData As Win32_Find_DataW) As Long
Private Declare Function FindClose Lib "Kernel32.dll" (ByVal hFindFile As Long) As Long

Private Const MAX_PATH As Long = 260&
Private Const AltLength As Long = 14

Private Type FileTime
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type Win32_Find_DataW
dwFileAttributes As Long
ftCreationTime As FileTime
ftLastAccessTime As FileTime
ftLastWriteTime As FileTime
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName(0 To (MAX_PATH * 2) - 1) As Byte
cAlternate(0 To (AltLength * 2) - 1) As Byte
End Type

Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10

Private Sub Form_Load()
Dim FindInf As Win32_Find_DataW
Dim hFind As Long

Const BasePath As String = "C:\Path\*.*"

hFind = FindFirstFile(ByVal StrPtr(BasePath), FindInf)
If (hFind <> INVALID_HANDLE_VALUE) Then
Do
If (FindInf.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
Debug.Print "[" & TrimNull(StrConv(FindInf.cFileName, vbUnicode)) & "]"
Else
Debug.Print TrimNull(StrConv(FindInf.cFileName, vbUnicode))
End If
Loop While FindNextFile(hFind, FindInf)

Call FindClose(hFind)
End If
End Sub

Private Function TrimNull(ByRef inString As String) As String
Dim NullPos As Long

NullPos = InStr(1, inString, vbNullChar)
If (NullPos) Then TrimNull = Left$(inString, NullPos - 1) Else TrimNull = inString
End Function
'***


FindFirstFileW() will do this for you, just pass it your directory name and check for a return value of
INVALID_HANDLE_VALUE.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: XXXX@XXXXX.COM
WWW: http://www.yqcomputer.com/