Short File and Path Names


I know you can quote your strings to get around long file names and even longer path names. It also helps with spaces in the names.
However, getting the quotes in the right places is often a pain, so I use short file names whenever I can. One of my customers was worried that a new file or directory could be created and change the number of my file or directory.

For example;

10/21/2008 12:55 PM <DIR> CONFIR~4 ConfirmTHEOrder
10/17/2008 09:39 AM <DIR> CONFIR~3 ConfirmTHEClub
10/17/2008 09:39 AM <DIR> CONFIR~2 ConfirmTHEClubUS
10/21/2008 09:25 AM <DIR> CONFIR~1 ConfirmTHEOrders

What would happen if another directory was created between THEOrder and ConfirmTHEOrders?
So I did a little looking around and found this.

Set objFile=objFSO.GetFile(strPath1)
strCMD1="Wscript " & objFile.ShortPath


That’s right, I put the fully quallified path in strPath1 and it returns the short path name.
Too cool.
Here is the sample I found by Jeffery Hicks for wrapping up powershell in a VBScript:

Dim objShell,objFSO,objFile
Set objShell=CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")
'enter the path for your PowerShell Script
'strPath="e:documents and settingsjhicksmy documentsscriptsposhGet-DiskSize.ps1?
'verify file exists
If objFSO.FileExists(strPath) Then
'return short path name
set objFile=objFSO.GetFile(strPath)
strCMD="powershell -nologo -command " & Chr(34) & "&{" &_
objFile.ShortPath & "}" & Chr(34)
'Uncomment next line for debugging
'WScript.Echo strCMD
'use 0 to hide window, True makes it wait until it's finishes.
objShell.Run strCMD,0, True
Else
'Display error message
WScript.Echo "Failed to find " & strPath
WScript.Quit
End If


In case you are wondering; the Chr(34) puts a double quote in the string.
Yet another way of dealing with quotes.

Or in Pure Powershell:

$a = New-Object -ComObject Scripting.FileSystemObject 
$f = $a.GetFile("C:\Program Files\Internet Explorer") 
$f.ShortPath
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s