DOS 8.3 short paths via PowerShell | Adam Dimech's Coding Blog
Skip to content

DOS 8.3 short paths via PowerShell

G5th July 2016

CNo Comments

TPowerShell, Windows

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.

Screen capture of PowerShell ISE

PowerShell ISE displaying the code that reveals the DOS 8.3 short path of a directory

The equivalent code is for %I in (.) do echo %~sI when using the Microsoft Command Prompt.

Screen capture of Windows Command Prompt

The Windows Command Prompt showing the use of a command to reveal the DOS 8.3 short path.

   

Comments:

No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.