Getting the 8.3 file Name associated With a Long File Name


Question

I'm attempting to shell off to another application and the other app doesn't support long file names. Is there a way to obtain the associated 8.3 file name programmatically?

Answer


Here is the function built on top of GetShortPathName API32 call:

Private Declare Function smsGetShortPathName& _
        Lib "Kernel32" Alias "GetShortPathNameA" _
        (ByVal lpszLongPath As String, _
         ByVal lpszShortPath As String, _
         ByVal cchBuffer As Long)

Public Function ShortPathNameGet(ByVal vstrLongPath As String) As String
'
' written by Shamil Salakhetdinov: shamil@marta.darts.spb.ru
'
  Dim strTmpBuf As String * 512
  Dim lngTmpBufLen As Long
  Dim lngRet As Long

  lngTmpBufLen = 512
  lngRet = smsGetShortPathName(vstrLongPath, strTmpBuf, lngTmpBufLen)
  If lngRet = 0 Then
    Err.Raise 503 + vbObjectError, "ShortPathNameGet", _
              "File not found: " & vstrLongPath
  ElseIf lngTmpBufLen < lngRet Then
    Err.Raise 503 + vbObjectError, "ShortPathNameGet", _
              "File path greater than temp buffer: " & _
              lngRet & " > " & lngTmpBufLen
  End If

  ShortPathNameGet = Left(strTmpBuf, lngRet)
End Function

Copyright 1999-2008 by Shamil Salakhetdinov.  All rights reserved.