Tipp 1.15 - Langen Dateipfad ermitteln

Wie kann ich den langen Dateipfad ermitteln?

Mit dieser Funktion können Sie den langen Dateipfad ermitteln. Kopieren Sie einfach nachfolgenden Quellcode in die Zwischenablage und fügen Sie anschließend den Inhalt der Zwischenablage in ein neues Modul ein. Die Aufrufparameter finden Sie im Quellcode beschrieben.

Api-AufrufeVerwendete Win32-Api-Aufrufe und Typen: FindFirstFile - FILETIME, WIN32_FIND_DATA

Beispiel:

Option Explicit
 
Public Const MAXPATH     As Long = 260
 
Public Type FILETIME
  dwLowDateTime          As Long
  dwHighDateTime         As Long
End Type
 
Public Type WIN32_FIND_DATA
  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              As String * MAXPATH
  cAlternate             As String * 14
End Type
 
Public Declare Function FindFirstFile _
    Lib "kernel32" Alias _
    "FindFirstFileA" _
    (ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" _
    (ByVal hFindFile As Long) As Long
 
Public Function GetLongPath( _
                ByVal sShortPath As String) _
                As String
  '// -----------------------------------------------------
  '// Methode:   | Ermittelt den "langen" Pfad aus einem 
  '//            | 8.3 (DOS)-Pfad
  '//            | benötigt VBA6+ wegen InStrRev-Methode
  '// -----------------------------------------------------
  '// Parameter: | sShortPath = gültiger 8.3 (DOS)-Pfad
  '// -----------------------------------------------------
  '// Rückgabe:  | Konvertierter Pfad (C:\PROGRA~1 ->
  '//            | C:\Programme)
  '// -----------------------------------------------------
  '// Autor:     | Stefan Kulpa
  '//            | EDV Innovation & Consulting - Dormagen
  '// -----------------------------------------------------
 
  Const INVALID_HANDLE_VALUE  As Long = -1
  Dim uWFA                    As WIN32_FIND_DATA
  Dim lOffset                 As Long
  Dim lHandle                 As Long
  Dim sFile                   As String
  Dim sTemp                   As String
 
  sShortPath = VBA.Trim$(sShortPath)
  If VBA.Right$(sShortPath, 1) = "\" Then
    sShortPath = VBA.Left$(sShortPath, _
                           VBA.Len(sShortPath) - 1)
  End If
  Do While Not lHandle = INVALID_HANDLE_VALUE
    lHandle = FindFirstFile(sShortPath, uWFA)
    sFile = VBA.Left$(uWFA.cFileName, _
                      VBA.InStr(uWFA.cFileName, _
                      vbNullChar) - 1)
    If VBA.Len(sShortPath) > 2 Then
      sTemp = sFile & "\" & sTemp
      lOffset = VBA.InStrRev(sShortPath, "\")
      If lOffset > 0 Then sShortPath = VBA.Left$( _
                                       sShortPath, _
                                       lOffset - 1)
    Else
      sTemp = sShortPath & "\" & sTemp
      Exit Do
    End If
  Loop
  GetLongPath = VBA.Left$(sTemp, VBA.Len(sTemp) - 1)
  FindClose lHandle
 
End Function