Convert Long Filename to Short

By | 2019-09-30

Nothing more than converting a long filename to a short 8.3 style filename.

Module

Private Declare Function GetShortPathName Lib "kernel32.dll" Alias "GetShortPathNameA" ( _
    ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, _
    ByVal cchBuffer As Long_
    ) As Long
Public Function ShortName(LongPath As String) As String
'***************************************************************************
' Converts Long FileName to Short FileName
'***************************************************************************
    Dim ShortPath As String
    Dim Ret As Long
    Const MAX_PATH = 260

    If LongPath = "" Then
        Exit Function
    End If
    ShortPath = Space$(MAX_PATH)
    Ret = GetShortPathName(LongPath, ShortPath, MAX_PATH)
    If Ret Then
        ShortName = Left$(ShortPath, Ret)
    End If
End Function

Usage

Private Sub Command1_Click()
    MsgBox ShortName("C:\Program Files\Microsoft Visual Studio\Longexecutable.EXE")
    ' Good for Shell uses or Name etc
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.