Windows Command Line
Here is a way using the Windows command line:
getShortPathName[path_] :=
Import["!for %p in (\""~~path~~"\") do @echo %~sp", "Text"]
So then:
getShortPathName[$InstallationDirectory]
(* "C:\\PROGRA~1\\WOLFRA~1\\MATHEM~1\\11.2" *)
NETLink
Here is a way using NETLink...
First, we declare the Win32 API function GetShortPathName:
Needs["NETLink`"]
InstallNET[];
getShortPathNameWin32 =
DefineDLLFunction @
"[DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)] string path,
[MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder shortPath,
int shortPathLength
)
";
Next, we define our own function that performs the necessary ceremony to invoke that API function:
$maxPathLength = 260;
getShortPathName[path_] :=
NETBlock @ Module[{shortPath = NETNew["System.Text.StringBuilder", $maxPathLength]}
, getShortPathNameWin32[path, shortPath, $maxPathLength]
; shortPath@ToString[]
]
So then:
getShortPathName[$InstallationDirectory]
(* "C:\\PROGRA~1\\WOLFRA~1\\MATHEM~1\\11.2" *)