Solved

GetShortPathName for folders with foreign characters

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now