Discussion:
How to get long filename from short filename?
(too old to reply)
Paul Emmons
2005-07-16 12:37:21 UTC
Permalink
This demonstration script is not yet successful.

I would like to get the long name from the short name of a file (in
this case, given as a command-line argument).

I was hoping that either the name or the path property of the file
object would do it, but no joy. The name property = the same short
name given. The path property (default) also just gives the short name
after the subdirectory. There is no "longname" property.

So how is it done?

----------------------------------------------
dim oArgs, oFi, oFS
set oArgs = Wscript.Arguments
if oArgs.count=0 then
wscript.echo "No argument given"
wscript.quit
end if
set oFS = CreateObject("Scripting.FileSystemObject")
set oFi = oFS.GetFile(oArgs.item(0))
wscript.echo oFi.name
wscript.echo oFi
y sakuda
2005-07-16 13:12:28 UTC
Permalink
Post by Paul Emmons
This demonstration script is not yet successful.
I would like to get the long name from the short name of a file (in
this case, given as a command-line argument).
I was hoping that either the name or the path property of the file
object would do it, but no joy. The name property = the same short
name given. The path property (default) also just gives the short name
after the subdirectory. There is no "longname" property.
So how is it done?
----------------------------------------------
dim oArgs, oFi, oFS
set oArgs = Wscript.Arguments
if oArgs.count=0 then
wscript.echo "No argument given"
wscript.quit
end if
set oFS = CreateObject("Scripting.FileSystemObject")
set oFi = oFS.GetFile(oArgs.item(0))
wscript.echo oFi.name
wscript.echo oFi
Try this function.
I do'nt know who post this script first, I find this in Google some time ago.
Y Sakuda from JPN

Function LongName(ShortName)
With CreateObject("Wscript.Shell")
With .CreateShortcut("dummy.lnk")
.TargetPath = ShortName
LongName = .TargetPath
End with
End with
End Function
Paul Emmons
2005-07-16 19:04:52 UTC
Permalink
On Sat, 16 Jul 2005 22:12:28 +0900, "y sakuda"
Post by y sakuda
Function LongName(ShortName)
With CreateObject("Wscript.Shell")
With .CreateShortcut("dummy.lnk")
.TargetPath = ShortName
LongName = .TargetPath
End with
End with
End Function
I saw that, too, at:
http://www.codecomments.com/archive299-2005-1-370439.html

Unfortunately, it isn't working for me. The function again gives just
the short name.
Paul Emmons
2005-07-16 19:49:20 UTC
Permalink
On further experimentation, it turns out that if the short-name
argument is drive:path\filename, the function works quickly. But if
it is just filename (i.e. for a file in the current directory), not
only does not expand the name, but it seems to choke for a second or
two.

Here is a modification that I think will cause it to return the full
name in all cases:

Still a newbie, I don't know whether setting oFS and oFi to nothing at
the end accomplishes anything, since these two variables have local
scope anyway (by being declared in the function). But it is important
for they do have local scope, in case the same variable names are used
in the calling program.

-------------------
Function LongName(ShortName)
dim oFS, oFI
set oFS = CreateObject("Scripting.FileSystemObject")
set oFi = oFS.GetFile(ShortName)
With CreateObject("Wscript.Shell")
With .CreateShortcut("dummy.lnk")
.TargetPath = oFi
LongName = .TargetPath
End with
End with
set oFS = nothing
set oFi = nothing
End Function
Andreas Kaestner
2005-07-17 00:46:17 UTC
Permalink
Post by y sakuda
Function LongName(ShortName)
dim oFS, oFI
set oFS = CreateObject("Scripting.FileSystemObject")
set oFi = oFS.GetFile(ShortName)
With CreateObject("Wscript.Shell")
With .CreateShortcut("dummy.lnk")
.TargetPath = oFi
LongName = .TargetPath
End with
End with
set oFS = nothing
set oFi = nothing
End Function
A bit more compact (Const used to avoid long lines):

' returns long path/filename from any filename (long or short)
' regardless whether path is given or missing
'
Function LongName(strFName)
Const ScFSO = "Scripting.FileSystemObject"
Const WScSh = "WScript.Shell"
With WScript.CreateObject(WScSh).CreateShortcut("dummy.lnk")
.TargetPath = CreateObject(ScFSO).GetFile(strFName)
LongName = .TargetPath
End With
End Function
--
Gruß, | Bitte in der NG antworten | Win98-Tipps:
Regards, Andreas | Please reply to the NG | http://a-kaestner.de
==================*===========================*=======================72
OE-QuoteFix 1.19.2: http://home.in.tum.de/~jain/software/oe-quotefix/
Torgeir Bakken (MVP)
2005-07-16 13:09:41 UTC
Permalink
Post by Paul Emmons
This demonstration script is not yet successful.
I would like to get the long name from the short name of a file (in
this case, given as a command-line argument).
I was hoping that either the name or the path property of the file
object would do it, but no joy. The name property = the same short
name given. The path property (default) also just gives the short name
after the subdirectory. There is no "longname" property.
So how is it done?
(snip)
Hi,

This should work (based on a Walter Zackery finding):


'--------------------8<----------------------
Option Explicit
dim oArgs, oShell, oSC, sLongName
set oArgs = Wscript.Arguments
if oArgs.count=0 then
wscript.echo "No argument given"
'wscript.quit
end if

Set oShell = CreateObject("WScript.Shell")

' Link will not be created!
Set oSC = oShell.CreateShortcut("c:\foo.lnk")
oSC.TargetPath = oArgs.item(0)
sLongName = oSC.TargetPath

WScript.Echo sLongName
'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
Paul Emmons
2005-07-16 19:52:46 UTC
Permalink
On Sat, 16 Jul 2005 15:09:41 +0200, "Torgeir Bakken \(MVP\)"
Post by Torgeir Bakken (MVP)
Set oShell = CreateObject("WScript.Shell")
' Link will not be created!
Set oSC = oShell.CreateShortcut("c:\foo.lnk")
oSC.TargetPath = oArgs.item(0)
sLongName = oSC.TargetPath
WScript.Echo sLongName
'--------------------8<----------------------
It works given a drive and path in front of the name. It doesn't work
for just a name. But I've found an enhancement. Please see my reply
to y sakuda.

Thanks for your help! I never would have figured this out on my own.
Strange that it must be so convoluted. It's almost easier in
assembler (just a DOS function call).
mr_unreliable
2005-07-16 20:54:13 UTC
Permalink
For those using xp, Microsoft provides a function, see:

"Converting Short File Names to Long File Names ($$rename.txt)"

found here:

http://www.microsoft.com/resources/documentation/Windows/XP/all/reskit/en-us/Default.asp?url=/resources/documentation/Windows/XP/all/reskit/en-us/prbc_cai_ommx.asp

For those using win9x or win2k, you can use a simple api call. See:

"Convert Short (8.3) File Names to Long File Names"

found here:

http://www.freevbcode.com/ShowCode.asp?ID=1133

Note that Dynawrap could be used to call _that_ api from script.
(But unfortunately not every api).

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
Post by Paul Emmons
This demonstration script is not yet successful.
I would like to get the long name from the short name of a file (in
this case, given as a command-line argument).
I was hoping that either the name or the path property of the file
object would do it, but no joy. The name property = the same short
name given. The path property (default) also just gives the short name
after the subdirectory. There is no "longname" property.
So how is it done?
----------------------------------------------
dim oArgs, oFi, oFS
set oArgs = Wscript.Arguments
if oArgs.count=0 then
wscript.echo "No argument given"
wscript.quit
end if
set oFS = CreateObject("Scripting.FileSystemObject")
set oFi = oFS.GetFile(oArgs.item(0))
wscript.echo oFi.name
wscript.echo oFi
Loading...