Converting Short File Names to Long File Names 
Author Message
 Converting Short File Names to Long File Names

I need to pass an short file name argument to a VBScript and have it return
the respective long file name. Passing the argument is no problem, but I
can't figure out a way to convert the file name. Can anyone help?

- Robert Perry



Sat, 09 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
Don't think you can with just the FileSystemObject's methods, but there is a way
using the DOS dir command.

Use a WScript.Shell object to Run the dir command with the /b switch and
redirect to a file.  Then parse out the LFN from the output.

For example, if you had an actual LFN full path name "C:\Some Path\New Text
File.txt" with a short name of "C:\SOMEPA~1\NEWTEX~1.TXT"

shortname = "C:\SOMEPA~1\NEWTEX~1.TXT"
Shell.Run "%comspec% /c dir /b " & shortname  & " > tempfile.txt",0,True

then tempfile.txt will contain one line containing

"New Text File.txt" (without the quotes of course)

Ugly but it works...

--
Michael Harris


| I need to pass an short file name argument to a VBScript and have it return
| the respective long file name. Passing the argument is no problem, but I
| can't figure out a way to convert the file name. Can anyone help?
|
| - Robert Perry
|
|
|



Sat, 09 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
Actually there _is_ an FSO based way to do this, but it's nearly as ugly and it
works _ONLY_ if what you start with is KNOWN to be in short file name format:

Set fso = CreateObject("Scripting.FileSystemObject")
SFN_path = "C:\SOMEPA~1\NEWTEX~1.TXT"
mypath = fso.getparentfoldername(SFN_path)
myshortname = ucase(fso.getfilename(SFN_path))
set fo = fso.getfolder(mypath)
for each fi in fo.files
  if ucase(fi.shortname) = myshortname then
    msgbox fi.name
    exit for
  end if
next

--
Michael Harris


| Don't think you can with just the FileSystemObject's methods, but there is a
way
| using the DOS dir command.
|
| Use a WScript.Shell object to Run the dir command with the /b switch and
| redirect to a file.  Then parse out the LFN from the output.
|
| For example, if you had an actual LFN full path name "C:\Some Path\New Text
| File.txt" with a short name of "C:\SOMEPA~1\NEWTEX~1.TXT"
|
| shortname = "C:\SOMEPA~1\NEWTEX~1.TXT"
| Shell.Run "%comspec% /c dir /b " & shortname  & " > tempfile.txt",0,True
|
| then tempfile.txt will contain one line containing
|
| "New Text File.txt" (without the quotes of course)
|
| Ugly but it works...
|
| --
| Michael Harris
|
|
|


| | I need to pass an short file name argument to a VBScript and have it return
| | the respective long file name. Passing the argument is no problem, but I
| | can't figure out a way to convert the file name. Can anyone help?
| |
| | - Robert Perry
| |
| |
| |
|
|



Sat, 09 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
I asked this same question when I first started in WSH almost a year
ago.  There was NO response back then, so I rooted around and created an
approach similar to your's Mike.  However, I would point out that your
solution only assumes the path is one folder deep from the root.  If it
can be more than one deep, the solution needs another loop walk the
folders from the deepest to the root, as in ...

Function LongPathName(sName) ' As String
  Dim colF, cF, _
      sShortFolderName, sBaseName, sPathName, sShortPath
  sPathName = ""
  On Error Resume Next
    sPathName = objFS.GetFile(sName).ShortPath
  On Error Goto 0 ' (a VB construct, undocumented in VBScript)
  if sPathName <> "" Then
    sShortFolderName = objFS.GetFolder(sPathName  & "\..").Path
    sPathName = objFS.GetDrive(Left(sName,Instr(sPathName , _
               ":"))).RootFolder
    Do ' Enumerate folders and match to named file's path
      Set colF = objFS.GetFolder(sPathName).Subfolders
      For Each cF in colF
        sShortPath = cF.ShortPath
        if Instr(sName, sShortPath) > 0 Then
          sPathname = cF.Path
          Exit For
        End if
      Next
    Loop Until sShortPath = sShortFolderName
    sBaseName = objFS.GetFile(sName).ShortName
    Set colF = objFS.GetFolder(sShortFolderName).Files
    For Each cF in colF ' Enumerate files and match to named file
      if cF.ShortName = sBaseName Then
        LongPathName = sPathname & "\" & cF.Name
        Exit For
      End if
    Next
    Set cF = Nothing
    Set colF = Nothing
  Else
    LongPathName = ""
  End if
End Function

And you thought your's was messy, Mike ;^)

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/

Quote:

> Actually there _is_ an FSO based way to do this, but it's nearly as ugly and it
> works _ONLY_ if what you start with is KNOWN to be in short file name format:

> Set fso = CreateObject("Scripting.FileSystemObject")
> SFN_path = "C:\SOMEPA~1\NEWTEX~1.TXT"
> mypath = fso.getparentfoldername(SFN_path)
> myshortname = ucase(fso.getfilename(SFN_path))
> set fo = fso.getfolder(mypath)
> for each fi in fo.files
>   if ucase(fi.shortname) = myshortname then
>     msgbox fi.name
>     exit for
>   end if
> next

> --
> Michael Harris



> | Don't think you can with just the FileSystemObject's methods, but there is a
> way
> | using the DOS dir command.
> |
> | Use a WScript.Shell object to Run the dir command with the /b switch and
> | redirect to a file.  Then parse out the LFN from the output.
> |
> | For example, if you had an actual LFN full path name "C:\Some Path\New Text
> | File.txt" with a short name of "C:\SOMEPA~1\NEWTEX~1.TXT"
> |
> | shortname = "C:\SOMEPA~1\NEWTEX~1.TXT"
> | Shell.Run "%comspec% /c dir /b " & shortname  & " > tempfile.txt",0,True
> |
> | then tempfile.txt will contain one line containing
> |
> | "New Text File.txt" (without the quotes of course)
> |
> | Ugly but it works...
> |
> | --
> | Michael Harris
> |
> |
> |


> | | I need to pass an short file name argument to a VBScript and have it return
> | | the respective long file name. Passing the argument is no problem, but I
> | | can't figure out a way to convert the file name. Can anyone help?
> | |
> | | - Robert Perry



Sun, 10 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
Thanks Michael,

You would think that VBScript would have a LongName property to go with the
ShortName property; however, your approach will work fine for what I'm
trying to do. Thanks for the tip.

- Rob



Sun, 10 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
"[...] However, I would point out that your
solution only assumes the path is one folder deep from the root.  If it
can be more than one deep, the solution needs another loop walk the
folders from the deepest to the root, as in [...]"

Actually, not true...

<1> mypath = fso.getparentfoldername(SFN_path)

Gets only the path portion of the fully qualified path which can be several
folders deep.

<2> myshortname = ucase(fso.getfilename(SFN_path))

Gets only the filename portion of the fully qualified path.

<3> set fo = fso.getfolder(mypath)

Gets the folder object that contains the file of interest.  It doesn't matter
how deeply nested the path is.  What it _doesn't_ do (but should) is handle the
simple unqualified filename case (no path included) which would result in the
variable "mypath" containing an empty string.  That would make <3> croak but can
easily be fixed by testing for an empty string and reassigning it to "." for the
current folder.

<1> mypath = fso.getparentfoldername(SFN_path)
<1.1> if trim(mypath) = "" then mypath = "."
...

The example wasn't meant to be 100% robust.  You would need to do FolderExists
and FileExists method calls to make sure that you were dealing with existing
folders/files...  The nice thing about the technique is that it handles relative
paths automatically (other than the one test for an unqualified filename) since
all of the FSO methods support relative paths, including the ...Exists methods.
The same is true for UNC paths.

--
Michael Harris

| > Actually there _is_ an FSO based way to do this, but it's nearly as ugly and
it
| > works _ONLY_ if what you start with is KNOWN to be in short file name
format:
| >
| > Set fso = CreateObject("Scripting.FileSystemObject")
| > SFN_path = "C:\SOMEPA~1\NEWTEX~1.TXT"
| > mypath = fso.getparentfoldername(SFN_path)
| > myshortname = ucase(fso.getfilename(SFN_path))
| > set fo = fso.getfolder(mypath)
| > for each fi in fo.files
| >   if ucase(fi.shortname) = myshortname then
| >     msgbox fi.name
| >     exit for
| >   end if
| > next
| >
| > --
| > Michael Harris
| >


| > | Don't think you can with just the FileSystemObject's methods, but there is
a
| > way
| > | using the DOS dir command.
| > |
| > | Use a WScript.Shell object to Run the dir command with the /b switch and
| > | redirect to a file.  Then parse out the LFN from the output.
| > |
| > | For example, if you had an actual LFN full path name "C:\Some Path\New
Text
| > | File.txt" with a short name of "C:\SOMEPA~1\NEWTEX~1.TXT"
| > |
| > | shortname = "C:\SOMEPA~1\NEWTEX~1.TXT"
| > | Shell.Run "%comspec% /c dir /b " & shortname  & " > tempfile.txt",0,True
| > |
| > | then tempfile.txt will contain one line containing
| > |
| > | "New Text File.txt" (without the quotes of course)
| > |
| > | Ugly but it works...
| > |
| > | --
| > | Michael Harris
| > |
| > |
| > |


| > | | I need to pass an short file name argument to a VBScript and have it
return
| > | | the respective long file name. Passing the argument is no problem, but I
| > | | can't figure out a way to convert the file name. Can anyone help?
| > | |
| > | | - Robert Perry



Sun, 10 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
Tom,

I should note that my "solution" doesn't even attempt to resolve the path
portion back to LFN format (only the filename portion) whereas yours does...

--
Michael Harris


| "[...] However, I would point out that your
| solution only assumes the path is one folder deep from the root.  If it
| can be more than one deep, the solution needs another loop walk the
| folders from the deepest to the root, as in [...]"
|
| Actually, not true...
|
| <1> mypath = fso.getparentfoldername(SFN_path)
|
| Gets only the path portion of the fully qualified path which can be several
| folders deep.
|
| <2> myshortname = ucase(fso.getfilename(SFN_path))
|
| Gets only the filename portion of the fully qualified path.
|
| <3> set fo = fso.getfolder(mypath)
|
| Gets the folder object that contains the file of interest.  It doesn't matter
| how deeply nested the path is.  What it _doesn't_ do (but should) is handle
the
| simple unqualified filename case (no path included) which would result in the
| variable "mypath" containing an empty string.  That would make <3> croak but
can
| easily be fixed by testing for an empty string and reassigning it to "." for
the
| current folder.
|
| <1> mypath = fso.getparentfoldername(SFN_path)
| <1.1> if trim(mypath) = "" then mypath = "."
| ...
|
| The example wasn't meant to be 100% robust.  You would need to do FolderExists
| and FileExists method calls to make sure that you were dealing with existing
| folders/files...  The nice thing about the technique is that it handles
relative
| paths automatically (other than the one test for an unqualified filename)
since
| all of the FSO methods support relative paths, including the ...Exists
methods.
| The same is true for UNC paths.
|
|
| --
| Michael Harris
|
|
| | > Actually there _is_ an FSO based way to do this, but it's nearly as ugly
and
| it
| | > works _ONLY_ if what you start with is KNOWN to be in short file name
| format:
| | >
| | > Set fso = CreateObject("Scripting.FileSystemObject")
| | > SFN_path = "C:\SOMEPA~1\NEWTEX~1.TXT"
| | > mypath = fso.getparentfoldername(SFN_path)
| | > myshortname = ucase(fso.getfilename(SFN_path))
| | > set fo = fso.getfolder(mypath)
| | > for each fi in fo.files
| | >   if ucase(fi.shortname) = myshortname then
| | >     msgbox fi.name
| | >     exit for
| | >   end if
| | > next
| | >
| | > --
| | > Michael Harris
| | >


| | > | Don't think you can with just the FileSystemObject's methods, but there
is
| a
| | > way
| | > | using the DOS dir command.
| | > |
| | > | Use a WScript.Shell object to Run the dir command with the /b switch and
| | > | redirect to a file.  Then parse out the LFN from the output.
| | > |
| | > | For example, if you had an actual LFN full path name "C:\Some Path\New
| Text
| | > | File.txt" with a short name of "C:\SOMEPA~1\NEWTEX~1.TXT"
| | > |
| | > | shortname = "C:\SOMEPA~1\NEWTEX~1.TXT"
| | > | Shell.Run "%comspec% /c dir /b " & shortname  & " > tempfile.txt",0,True
| | > |
| | > | then tempfile.txt will contain one line containing
| | > |
| | > | "New Text File.txt" (without the quotes of course)
| | > |
| | > | Ugly but it works...
| | > |
| | > | --
| | > | Michael Harris
| | > |
| | > |
| | > |


| | > | | I need to pass an short file name argument to a VBScript and have it
| return
| | > | | the respective long file name. Passing the argument is no problem, but
I
| | > | | can't figure out a way to convert the file name. Can anyone help?
| | > | |
| | > | | - Robert Perry
|
|



Sun, 10 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names


Quote:
> Thanks Michael,

> You would think that VBScript would have a LongName property to go
with the
> ShortName property; however, your approach will work fine for what I'm
> trying to do. Thanks for the tip.

> - Rob

Actually, you don't even have to create the shortcut! You don't even
have to pass a valid filename for the LNK file. It seems that as soon
as you pass the short filename to the TargetPath property, it is
translated on-the-fly to the long filename. Here is an example that
demonstrates this. It will only show the long filename if the short
name is an existing file or folder; otherwise it will just show the
short name.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oShellLink = WshShell.CreateShortcut("<.lnk")
oShellLink.TargetPath = "C:\progra~1\micros~1"
msgbox oShellLink.TargetPath

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Mon, 11 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
Michael and Tom's solutions work, but they have an awful lot of
overhead just to get the long filename path of one file or directory.
Just imagine looping through a directory structure that has thousands
of files and directories. A better idea is the following bit of
pseudo-code:

1) Create a shortcut to the short filename file or folder using the
CreateShortcut method.

2) Get the long filename that you're looking for by getting the target
path of the newly created shortcut using the TargetPath property of
the CreateShortcut method. This target path will always be in complete
long filename format, starting from the root.

3) Delete the created shortcut.

Let me know if you get stuck somewhere in there.


: Thanks Michael,
:
: You would think that VBScript would have a LongName property to go
with the
: ShortName property; however, your approach will work fine for what
I'm
: trying to do. Thanks for the tip.
:
: - Rob
:
:
:



Mon, 11 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names
Yes, exactly.  That's really what I was getting at, but obviously didn't
make myself clear.

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/

Quote:

> Tom,

> I should note that my "solution" doesn't even attempt to resolve the
> path portion back to LFN format (only the filename portion) whereas
> yours does...

> --
> Michael Harris



> | "[...] However, I would point out that your
> | solution only assumes the path is one folder deep from the root.  If it
> | can be more than one deep, the solution needs another loop walk the
> | folders from the deepest to the root, as in [...]"
> |
> | Actually, not true...
> |
> | <1> mypath = fso.getparentfoldername(SFN_path)
> |
> | Gets only the path portion of the fully qualified path which can be several
> | folders deep.
> |
> | <2> myshortname = ucase(fso.getfilename(SFN_path))
> |
> | Gets only the filename portion of the fully qualified path.
> |
> | <3> set fo = fso.getfolder(mypath)
> |
> | Gets the folder object that contains the file of interest.  It doesn't matter
> | how deeply nested the path is.  What it _doesn't_ do (but should) is handle
> the
> | simple unqualified filename case (no path included) which would result in the
> | variable "mypath" containing an empty string.  That would make <3> croak but
> can
> | easily be fixed by testing for an empty string and reassigning it to "." for
> the
> | current folder.
> |
> | <1> mypath = fso.getparentfoldername(SFN_path)
> | <1.1> if trim(mypath) = "" then mypath = "."
> | ...
> |
> | The example wasn't meant to be 100% robust.  You would need to do FolderExists
> | and FileExists method calls to make sure that you were dealing with existing
> | folders/files...  The nice thing about the technique is that it handles
> relative
> | paths automatically (other than the one test for an unqualified filename)
> since
> | all of the FSO methods support relative paths, including the ...Exists
> methods.
> | The same is true for UNC paths.
> |
> |
> | --
> | Michael Harris
> |
> |
> | | > Actually there _is_ an FSO based way to do this, but it's nearly as ugly
> and
> | it
> | | > works _ONLY_ if what you start with is KNOWN to be in short file name
> | format:
> | | >
> | | > Set fso = CreateObject("Scripting.FileSystemObject")
> | | > SFN_path = "C:\SOMEPA~1\NEWTEX~1.TXT"
> | | > mypath = fso.getparentfoldername(SFN_path)
> | | > myshortname = ucase(fso.getfilename(SFN_path))
> | | > set fo = fso.getfolder(mypath)
> | | > for each fi in fo.files
> | | >   if ucase(fi.shortname) = myshortname then
> | | >     msgbox fi.name
> | | >     exit for
> | | >   end if
> | | > next
> | | >
> | | > --
> | | > Michael Harris
> | | >


> | | > | Don't think you can with just the FileSystemObject's methods, but there
> is
> | a
> | | > way
> | | > | using the DOS dir command.
> | | > |
> | | > | Use a WScript.Shell object to Run the dir command with the /b switch and
> | | > | redirect to a file.  Then parse out the LFN from the output.
> | | > |
> | | > | For example, if you had an actual LFN full path name "C:\Some Path\New
> | Text
> | | > | File.txt" with a short name of "C:\SOMEPA~1\NEWTEX~1.TXT"
> | | > |
> | | > | shortname = "C:\SOMEPA~1\NEWTEX~1.TXT"
> | | > | Shell.Run "%comspec% /c dir /b " & shortname  & " > tempfile.txt",0,True
> | | > |
> | | > | then tempfile.txt will contain one line containing
> | | > |
> | | > | "New Text File.txt" (without the quotes of course)
> | | > |
> | | > | Ugly but it works...
> | | > |
> | | > | --
> | | > | Michael Harris
> | | > |
> | | > |
> | | > |


> | | > | | I need to pass an short file name argument to a VBScript and have it
> | return
> | | > | | the respective long file name. Passing the argument is no problem, but
> I
> | | > | | can't figure out a way to convert the file name. Can anyone help?
> | | > | |
> | | > | | - Robert Perry



Mon, 11 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names

Quote:



> > Thanks Michael,

> > You would think that VBScript would have a LongName property to go
> with the
> > ShortName property; however, your approach will work fine for what I'm
> > trying to do. Thanks for the tip.

> > - Rob

> Actually, you don't even have to create the shortcut! You don't even
> have to pass a valid filename for the LNK file. It seems that as soon
> as you pass the short filename to the TargetPath property, it is
> translated on-the-fly to the long filename. Here is an example that
> demonstrates this. It will only show the long filename if the short
> name is an existing file or folder; otherwise it will just show the
> short name.

> Set WshShell = WScript.CreateObject("WScript.Shell")
> Set oShellLink = WshShell.CreateShortcut("<.lnk")
> oShellLink.TargetPath = "C:\progra~1\micros~1"
> msgbox oShellLink.TargetPath

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Awesome.  WSH has kludges! ;^))

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/



Mon, 11 Mar 2002 03:00:00 GMT  
 Converting Short File Names to Long File Names

Zack,  

I give you the WSH-WOW (Windows Script Host - Wizard Of the Week) award <g>!

--
Michael Harris



  > Thanks Michael,
  >
  > You would think that VBScript would have a LongName property to go
  with the
  > ShortName property; however, your approach will work fine for what I'm
  > trying to do. Thanks for the tip.
  >
  > - Rob
  >
  >

  Actually, you don't even have to create the shortcut! You don't even
  have to pass a valid filename for the LNK file. It seems that as soon
  as you pass the short filename to the TargetPath property, it is
  translated on-the-fly to the long filename. Here is an example that
  demonstrates this. It will only show the long filename if the short
  name is an existing file or folder; otherwise it will just show the
  short name.

  Set WshShell = WScript.CreateObject("WScript.Shell")
  Set oShellLink = WshShell.CreateShortcut("<.lnk")
  oShellLink.TargetPath = "C:\progra~1\micros~1"
  msgbox oShellLink.TargetPath

  Sent via Deja.com http://www.deja.com/
  Share what you know. Learn what you don't.



Mon, 11 Mar 2002 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Converting Long File Names to Short File Names

2. Converting Long File Names to Short File Names

3. Getting short file name from long file name:

4. Converting Paths having short name to long versions

5. Long File Names in ASP ... Convert with VBScript to Short File Names

6. Converting long file names to DOS format

7. Long names from short.

8. registry %1 argument and short/long path names

9. Unzip a zip file to preserve long file name

10. Get Short File Name in VBScript

11. WSH Run does recognizes only short DOS convention file path names

12. using long file names?

 

 
Powered by phpBB® Forum Software