DOS 8.3 short paths via PowerShell
Use this PowerShell script to discover the DOS 8.3 path name for any selected folder in Windows.
Sometimes in programming, there is a need to return the short path name of a folder. The following PowerShell script will return the short path name of a selected directory in Windows.
In PowerShell, simply cd
to the directory that you wish to derive the DOS 8.3 path name for, then use the following code:
New-Item 0.txt -type file | Out-Null #Creates 0.txt
$path = (dir 0.txt).FullName #Returns full path name of file
$object = New-Object -ComObject Scripting.FileSystemObject
$output = $object.GetFile($path)
$output.shortpath.trim("0.txt") | Out-Null #Generates 8.3 short file path output
Write-Host "`n`n The short path of this directory is" $output.shortpath.trim("0.txt") #Writes output
Remove-Item 0.txt #Deletes 0.txt
The script works by making a new file (called 0.txt) in the folder and deriving the DOS 8.3 file name for that file. The path then has the file name removed (because we’re interested in the directory) and the new file is deleted again.
The equivalent code is for %I in (.) do echo %~sI
when using the Microsoft Command Prompt.
Comments
2 responses to “DOS 8.3 short paths via PowerShell”
Slightly more brief:
$object = New-Object -ComObject Scripting.FileSystemObject
$output = $object.GetFolder($PWD)
Write-Host "`n`n The short path of this directory is" $output.shortpath