VBA Tipp: Langen Dateinamen aus kurzem Dateinamen ermitteln

Aus DBWiki
Wechseln zu: Navigation, Suche

Aufgabenstellung

Ich möchte ermitteln, welche "langen Dateinamen" den "kurzen Dateinamen" (in der 16-bit-Windows 8.3-Notation) entsprechen.

Lösung

Public Function GetLongPath(ByVal sShortName As String) As String
 
  Dim sLongName As String, sTemp As String, iSlashPos As Integer
 
  'Add \ to short name to prevent InStr from failing
  sShortName = sShortName & "\"
 
  'Start from 4 to ignore the "[Drive Letter]:\" characters
  iSlashPos = InStr(4, sShortName, "\")
 
  'Pull out each string between \ character for conversion
  While iSlashPos
    sTemp = Dir(left$(sShortName, iSlashPos - 1), _
      vbNormal + vbHidden + vbSystem + vbDirectory)
    If sTemp = "" Then
      'Error 52 - Bad File Name or Number
      GetLongPath = ""
      Exit Function
    End If
    sLongName = sLongName & "\" & sTemp
    iSlashPos = InStr(iSlashPos + 1, sShortName, "\")
  Wend
 
  'Prefix with the drive letter
  GetLongPath = left$(sShortName, 2) & sLongName
 
End Function

Aufruf

Debug.Print GetLongPath("C:\PROGRA~1")
 
C:\Programme

Weblinks

MS Knowledge Base Artikel Q154822: