Long/ShortFileName, Long/ShortPathName Functions

Action

Return a long filename and long path name of a file.

Syntax

LongFileName[$]([file$])

LongPathName[$]([file$])

ShortFileName[$]([file$])

ShortPathName[$]([file$])

file$:filename

Description

With the function LongFileName() you can determine a long filename or directory from a short name, for example: "StartMenu" instead of its short name "STARTM~1". ShortFileName does the reverse (see Known Issues).

LongFileName() also returns the pathname of the last call of Dir[$].

With the function LongPathName() you determine the long path name of a file, for example: "c:\example-folder\test“ instead of "c:\exam~1\test" (see Known Issues); ShortPathName does the reverse.

Example

// Create two test files

// Files must exist or the functions return a File Name error

Local f1$ = App.scSpecialDir(39) & "\tinyname.bmp"

Local f2$ = App.scSpecialDir(39) & "\reallylongfilename.bmp"

BSave f1$, 100000, 100 : BSave f2$, 100000, 100

Local f3$ = ShortPathName(f2$)

// Show the results in the Debug screen

Debug.Show

Trace f1$

Trace LongFileName(f1$)

Trace ShortFileName(f1$) // Error: Returns a blank - see known issues below

Trace LongPathName(f1$)

Trace ShortPathName(f1$)

Debug.Print

Trace f2$

Trace ShortFileName(f2$) // Acts correctly - see known issues below

Trace ShortPathName(f2$)

Debug.Print

Trace f3$

Trace LongFileName(f3$)

Trace LongPathName(f3$) // Error: Returns the Short Path - see known issues below

// Remove test files

Kill f1$

Kill f2$

Known Issues

If the filename in file$ fits within the old 8.3 format (filename <=8; extension <=3) then ShortFileName returns a blank rather than the filename. There are two possible workarounds for this:

  1. Create a function such as the one below which reverts to the original filename if ShortFileName returns a blank.

  2. Local f1$ = App.scSpecialDir(39) & "\tinyname.bmp"

    Local f2$ = App.scSpecialDir(39) & "\reallylongfilename.bmp"

    BSave f1$, 100000, 100 : BSave f2$, 100000, 100

    Print GetShortName(f1$)

    Print GetShortName(f2$)

    Kill f1$ : Kill f2$

     

    Function GetShortName(file$)

    If ShortFileName(file$) = "" Then Return Upper(file$)

    Return ShortFileName(file$)

    EndFunc


  3. Use the result from ShortPathName() as in the example below:

  4. Local f1$ = App.scSpecialDir(39) & "\tinyname.bmp"

    Local f2$ = App.scSpecialDir(39) & "\reallylongfilename.bmp"

    BSave f1$, 100000, 100 : BSave f2$, 100000, 100

    Print GetShortName(f1$)

    Print GetShortName(f2$)

    Kill f1$ : Kill f2$

     

    Function GetShortName(file$)

    Local slen As Byte, sf$

    sf$ = ShortPathName(file$)

    slen = RInStr(sf$, "\")

    Return Upper(Mid(sf$, slen + 1))

    EndFunc


LongPathName does not return the long path name as stated; an example of this and a workaround using the GetLongPathName() API is below:

Local f2$ = App.scSpecialDir(39) & "\reallylongfilename.bmp"

BSave f2$, 100000, 100

Local f3$ = ShortPathName(f2$), f4$ = Space(255)

Print LongPathName(f3$) // Error

Print GetLongPath(f3$)

Kill f2$

 

Function GetLongPath(file$)

Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" (ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long

'

Local fp$ = Space(255), flen = GetLongPathName(file$, fp$, 255)

Return Left(fp$, flen)

EndFunc

See Also

Dir, ShortProgName(), App

{Created by Sjouke Hamstra; Last updated: 12/10/2014 by James Gaite}