Solved

GetShortPathName for folders with foreign characters

Posted on 2006-06-26
3
479 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 122
Updates not working for MS Windows 7 12 183
message box in access 4 51
Getting warning: You are about to delete 1 row(s) 9 53
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

821 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