Solved

GetShortPathName for folders with foreign characters

Posted on 2006-06-26
3
472 Views
Last Modified: 2013-12-03
I am trying to get the attributes of a folder with foreign characters in it (asian fonts)

I have populated an array of directory names using FindFirstFileW and FindNextFileW
(it confirms that each entry in the array is an existing directory)

I then want to look at the attributes of each directory

    aFolderList = GetFoldersWithForeignCharacters(ReadPath)
   
        For i = 0 To UBound(aFolderList)
           
            Set Current_Folder = Fso.GetFolder(Get_DOS_Path(aFolderList(i)))
            ' the below line doesn't work either
            'Set Current_Folder = Fso.GetFolder(aFolderList(i))
       
            If Current_Folder.Attributes And System Then _
               Current_Folder.Attributes = Current_Folder.Attributes Xor System
        Next

Public Function Get_DOS_Path(sLongName As String) As String

    'return DOS path & File_Name (8.3 compliant)
    Dim sOUT As String, lRet As Long
    sOUT = String(MAX_PATH + 2, 0)
    lRet = GetShortPathName(sLongName, sOUT, MAX_PATH + 1)
    If lRet > 0& Then
        Get_DOS_Path = Left$(sOUT, lRet)
    end if
end function



I am getting a file path not found when I try to assign the current_folder
if I do it with or without the getShortPathName function

Any ideas on how to use the fso.getfolder to accept foreign chracters,
or on how to get GetShortPathName to accept them
or is there another way to read and set the folder attributes?

Thanks,

DT
0
Comment
Question by:dtruong98
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
davidnsc1 earned 500 total points
ID: 16988374
You really shouldn't use the FileSystemObject.  The GetFileAttributesW and SetFileAttributesW apis will do nicely.
Be sure to use GetShortPathNameW instead of GetShortPathNameA.

In order to use the wide APIs, change all "Byval whatever as String"s in the API declarations to "Byval whatever as Long", and then instead of passing it, say, sLongName, pass it StrPtr(sLongName).

(And FYI, String() also has the faster, non-variant version String$(), but in general, for cases like these, if you're going for speed, it's better to use Space$(), since GetShortPathName adds its own null char anyway)
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 16996935
Try this. If it still does not work there may be a problem in "GetFoldersWithForeignCharacters" filling the array of Unicode names. Will need to see the code to fix it.

Private Declare Function GetFileAttributesW Lib "kernel32" (ByVal lpFilename As Long) As Long
Private Declare Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As Long, ByVal lpszShortPath As Long, ByVal cchBuffer As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal psString As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Public Function GetFileAttributes(ByVal lpFilename As String) As Long
   GetFileAttributes = GetFileAttributesW(StrPtr(lpFilename))
End Function

Public Function GetShortName(ByVal sLongFileName As String) As String
   Dim lRetVal          As Long
   Dim lPtr             As Long
   Dim lBuff            As Long
   Dim Buffer              As String * 255

   lPtr = StrPtr(sLongFileName)
   lBuff = StrPtr(Buffer)
   If lPtr Then
      lRetVal = GetShortPathNameW(lPtr, lBuff, 255)
      GetShortName = PtrToStrW(lBuff)
   End If
End Function

'Dereference Unicode string pointer
Public Function PtrToStrW(ByVal lpsz As Long) As String
   Dim lLen             As Long
   If lpsz Then
      lLen = lstrlenW(lpsz)
      PtrToStrW = Space$(lLen)
      CopyMemory ByVal StrPtr(PtrToStrW), ByVal lpsz, lLen * 2
   End If
End Function
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 16998949
Hi,
   Just a quick comment. If you are using FindFirstFileW to retrieve files then you already have the attribute of each file via the Win32FD structure as Win32FD.dwAttributes.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now