How to resolve tilde-completed paths in windows Cmd/Powershell

Solution 1:

How is this mechanism called?

Those are the "8.3 names" generated for compatibility with 16-bit MS-DOS and Windows 3.x software, in which file names were limited to exactly this size.

(File access APIs in those operating systems have this fixed length assumption built into them, so old programs written for Win16 or DOS are permanently affected by the limit even if you run them on later Windows version. Therefore, Windows provides short aliases for long names so that people could still run their old programs on their brand new Windows 95 machines.)

It could be that you're seeing this path because older versions of WISE Installation System actually generated 32-bit installers with a 16-bit launcher (meant to display a friendly error message on Windows 3.x, instead of a generic "invalid .exe file").

It turns out a tilde character followed by a number performs something like a tab expansion. A bit error-prone, but ok.

No, the tilde is not actually expanded at all. Instead, the same file or folder literally has both names assigned to it, and the tilde just happens to be an actual part of 8.3 names that Windows generates. (Some operating systems that deal with FAT-formatted disks are known to generate 8.3 names without a tilde.)

So the expansion is done not by userspace but by the filesystem itself – the FAT or NTFS driver recognizes the same folder as being simultaneously named Program Files and PROGRA~1. There is no guessing involved – the 8.3 name is assigned statically, whenever the item is created or renamed (you can see current 8.3 names using dir /x in Command Prompt), and PROGRA~3 will always remain PROGRA~3 even if PROGRA~2 is deleted.

This also means that anywhere a file path is used, both the full name and the short 8.3 name should be accepted the same, despite the latter being hidden from directory listings. Programs which don't accept an 8.3 name are usually doing something wrong.

(Do note that generation of 8.3 names on NTFS disks can be disabled, so the dir /x command might show some names missing. Disabling 8.3 generation does not invalidate previously assigned names, however.)

How do I resolve this tilde-completed path to a normal one?

Through the Win32 API, you can call GetLongPathName() or GetFullPathName().

Through PowerShell, you can access (Get-Item).FullName or (Get-ChildItem).FullName:

PS C:\Users\Foo> (get-item -force "c:\users\foo\virtua~1").Name
.VirtualBox

PS C:\Users\Foo> (get-item -force "c:\users\foo\virtua~1").FullName
C:\Users\Foo\.VirtualBox

PS C:\Users\Foo> get-item -force docume~1,micros~1,virtua~1 | ft Name,FullName

Name                 FullName
----                 --------
Documents            C:\Users\Foo\Documents
MicrosoftEdgeBackups C:\Users\Foo\MicrosoftEdgeBackups
.VirtualBox          C:\Users\Foo\.VirtualBox