Board index » delphi » How to retrieve long path name from short path name

How to retrieve long path name from short path name

Hi,
I place my application to W95 SendTo dir, so that I can pass the file name
to
the application form the explorer.
But W95 pass a short path name like this:
d:\progra~1\myprog~1\doc.txt

How can I retrieve the long path name like this:
d:\program files\my program\doc.txt

Thanks in advance.

 

Re:How to retrieve long path name from short path name


Ulysses <wai...@hkstar.com> wrote in article
<01bca778$b70678a0$LocalHost@falcon>...

Quote
> Hi,
> I place my application to W95 SendTo dir, so that I can pass the file
name
> to
> the application form the explorer.
> But W95 pass a short path name like this:
> d:\progra~1\myprog~1\doc.txt

> How can I retrieve the long path name like this:
> d:\program files\my program\doc.txt

> Thanks in advance.

Hi Ulysses

You may use the GetFullPathName( ) API to retrieve the full path given a
file name.

For further information refer to online help.

Hope this helps

--
Regards

V.VijayaRaghvan
Aditi Technologies Pvt. Ltd.
vvi...@aditi.com
http://www.aditi.com

Quote

Re:How to retrieve long path name from short path name


There's probably an easier way, but you probably could use FindFirstFile
and get the alternate name.

dave

Quote
> But W95 pass a short path name like this:
> d:\progra~1\myprog~1\doc.txt

> How can I retrieve the long path name like this:
> d:\program files\my program\doc.txt

> Thanks in advance.

--
dave.   dbros...@chesco.com
--please email
visit me at http://www.chesco.com/~dbrosius/bs.html

Re:How to retrieve long path name from short path name


Quote
Ulysses wrote:

> I place my application to W95 SendTo dir, so that I can pass the file
> name to the application form the explorer.
> But W95 pass a short path name like this:
> d:\progra~1\myprog~1\doc.txt

> How can I retrieve the long path name like this:
> d:\program files\my program\doc.txt

It's tricky but possible. The function you need is FindFirstFile(), but
since it only returns the last component of the file name, you have to
call it repeatedly for each directory in the path.

Here's the function I wrote to handle this:

Quote
> string LongFileName(const string& short_file_name) {
>   string long_file_name;
>   string working_name(short_file_name);
>   int subpath_length(0);
>   // FindFirstFile() only returns the last part of the file name, so
>   // we have to build up the complete name one path component at a
>   // time
>   while (subpath_length < short_file_name.size()) {
>     int last_subpath_length(subpath_length);
>     // Find the next backslash or the end of the name
>     while (subpath_length < short_file_name.size()
>            && short_file_name[subpath_length] != '\\')
>       ++subpath_length;
>     bool is_directory(subpath_length < short_file_name.size());
>     if (subpath_length == 2 && short_file_name[1] == ':') {
>       // If this is the drive component, just copy it
>       long_file_name = working_name.substr(0, 2);
>     } else {
>       // Change the backslash to a null in the working copy
>       if (is_directory)
>         working_name[subpath_length] = '\0';
>       // Look up the real file name, and add it to the long name
>       WIN32_FIND_DATA find_data;
>       HANDLE search_handle(FindFirstFile(working_name.c_str(),
>                                          &find_data));
>       if (search_handle == INVALID_HANDLE_VALUE)
>         long_file_name += working_name.substr(last_subpath_length);
>       else
>         long_file_name += find_data.cFileName;
>     }
>     if (is_directory) {
>       // Repair the working copy and skip the backslash
>       long_file_name += '\\';
>       working_name[subpath_length] = '\\';
>       ++subpath_length;
>     }
>   return long_file_name;
> }

--
Ross Smith ............................. <mailto:ross.sm...@nz.eds.com>
Internet and New Media, EDS (New Zealand) Ltd., Wellington, New Zealand
   "The first thing we do, let's kill all the language lawyers."
                             -- Henry VI Part II, by W. Shakespeare;
                                additional dialogue by B. Stroustrup

Re:How to retrieve long path name from short path name


1. Use FindFirstFile on each component to convert it from short
to long.

2. Convert the filename to an ITEMIDLIST, then use
SHGetPathFromIDList to convert it back.  Explorer will always
return the long file name if you use SHGetPathFromIDList.

--
(My return address is intentionally invalid to foil spammers.  Delete the
".---" to get my real address.  I do this on my own time with my own money;
my responses are not to be considered official technical support or advice.)

Re:How to retrieve long path name from short path name


In article <01bca7b0$a645b480$a80c36ca@nquest59>, vvijay
<vvi...@aditi.com> writes

Quote
>Hi Ulysses

>You may use the GetFullPathName( ) API to retrieve the full path given a
>file name.

>For further information refer to online help.

Perhaps YOU should read the online help. If you believe that
"GetFullPathName" converts a short pathname into a long pathname, you're
sadly mistaken.

The purpose of "GetFullPathName" is to convert a "partial path" such as
"file.doc" or "..\file.doc" into a full pathname. That full pathname is
*not* guarenteed to be a "long" filename.

As someone had already said, the correct way to convert a "short path"
into a "long path" is to expand it component by component with
"FindFirstFile".

Chris

----------------------------------------------------------------
Chris Marriott, Microsoft Certified Solution Developer.
SkyMap Software, U.K. e-mail: ch...@skymap.com
Visit our web site at http://www.skymap.com

Re:How to retrieve long path name from short path name


This is a multi-part message in MIME format.
--------------114A7CEE5947EFC151159304
Content-Type: multipart/alternative; boundary="------------37BDE92BFC559C3CB4E3D63F"

--------------37BDE92BFC559C3CB4E3D63F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Quote
Dave Brosius wrote:
> There's probably an easier way, but you probably could use
> FindFirstFile
> and get the alternate name.

> dave

> > But W95 pass a short path name like this:
> > d:\progra~1\myprog~1\doc.txt

> > How can I retrieve the long path name like this:
> > d:\program files\my program\doc.txt

> > Thanks in advance.

> --
> dave.   dbros...@chesco.com
> --please email
> visit me at http://www.chesco.com/~dbrosius/bs.html

Use GetFullPathName. Here's the definition:

The GetFullPathName function retrieves the full path and filename of a
specified file.

DWORD GetFullPathName(
  LPCTSTR lpFileName,         // address of name of file to find path
for
  DWORD nBufferLength,        // size, in characters, of path buffer
  LPTSTR lpBuffer,            // address of path buffer
  LPTSTR *lpFilePart          // address of filename in path
);

--
Joseph Partridge jose...@totcon.com
Partridge Software
http://www.partridgesoft.com

--------------37BDE92BFC559C3CB4E3D63F
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<HTML>

Quote
Dave Brosius wrote:

<BLOCKQUOTE TYPE=CITE>There's probably an easier way, but you probably
could use FindFirstFile
<BR>and get the alternate name.

<P>dave

<P>> But W95 pass a short path name like this:
<BR>> d:\progra~1\myprog~1\doc.txt
<BR>>
<BR>> How can I retrieve the long path name like this:
<BR>> d:\program files\my program\doc.txt
<BR>>
<BR>> Thanks in advance.

<P>--
<BR>dave.&nbsp;&nbsp; dbros...@chesco.com
<BR>--please email
<BR>visit me at <A HREF="http://www.chesco.com/~dbrosius/bs.html">http://www.chesco.com/~dbrosius/bs.html</A></BLOCKQUOTE>
Use GetFullPathName. Here's the definition:

<P><TT>The GetFullPathName function retrieves the full path and filename
of a specified file.</TT><TT></TT>

<P><TT>DWORD GetFullPathName(</TT>
<BR><TT>&nbsp; LPCTSTR lpFileName,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// address of name of file to find path for</TT>
<BR><TT>&nbsp; DWORD nBufferLength,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// size, in characters, of path buffer</TT>
<BR><TT>&nbsp; LPTSTR lpBuffer,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// address of path buffer</TT>
<BR><TT>&nbsp; LPTSTR *lpFilePart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// address of filename in path</TT>
<BR><TT>);</TT>

<P>--
<BR>Joseph Partridge jose...@totcon.com
<BR>Partridge Software
<BR><A HREF="http://www.partridgesoft.com">http://www.partridgesoft.com</A>
<BR>&nbsp;</HTML>

--------------37BDE92BFC559C3CB4E3D63F--

--------------114A7CEE5947EFC151159304
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Joseph Partridge
Content-Disposition: attachment; filename="vcard.vcf"

begin:          vcard
fn:             Joseph Partridge
n:              Partridge;Joseph
org:            Partridge Software
email;internet: jose...@totcon.com
title:          Software Developer
x-mozilla-cpt:  ;0
x-mozilla-html: FALSE
end:            vcard

--------------114A7CEE5947EFC151159304--

Re:How to retrieve long path name from short path name


Raymond Chen [MS] (raymo...@microsoft.com.---) wrote:
: 1. Use FindFirstFile on each component to convert it from short
: to long.

: 2. Convert the filename to an ITEMIDLIST, then use
: SHGetPathFromIDList to convert it back.  Explorer will always
: return the long file name if you use SHGetPathFromIDList.

: --
: (My return address is intentionally invalid to foil spammers.  Delete the
: ".---" to get my real address.  I do this on my own time with my own money;
: my responses are not to be considered official technical support or advice.)

Does the second solution also needs to do per component??

I have made a routine in Delphi3 that converts a short filepath to long. But
still not UNC paths. It really is tricky because FindFirstFile returns for
\\ntserver '' and for \\ntserver1\swap '.'
very much special cases as you see! when the unc path is longer than 2 dirs the behaviour is like
non unc paths.

Isn't there a standard routine who gives me the x dir of a path?

and why did those Microsoft boys didn't give us a opposite routine of
GetShortPath??Really pathetic!

Let me know if anybody made a bulletproof short UNC/non UNC path to LongPath converter!

Greetings Jan Oonk
--
Please EMAIL all replies to J...@stack.nl

Re:How to retrieve long path name from short path name


In article <33F298A0.3E52C...@totcon.com>, Joseph Partridge
<jose...@totcon.com> writes

Quote
>I am very surprised to hear this answer from someone that works at
>Microsoft. Just use the GetFullPathName function.

Read my earlier reply, Joseph. Unlike you, Ray knows what the
"GetFullPathName" function actually does. It is *not* the "opposite" of
"GetShortPathName". As Ray correctly said, using "FindFirstFile" on each
component of the name is the right answer.

Chris

----------------------------------------------------------------
Chris Marriott, Microsoft Certified Solution Developer.
SkyMap Software, U.K. e-mail: ch...@skymap.com
Visit our web site at http://www.skymap.com

Re:How to retrieve long path name from short path name


This is a multi-part message in MIME format.
--------------D346EB7B9A00AB8B2DBE45AA
Content-Type: text/plain; charset=us-ascii
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Transfer-Encoding: 7bit

Quote
Raymond Chen [MS] wrote:
> 1. Use FindFirstFile on each component to convert it from short
> to long.

> 2. Convert the filename to an ITEMIDLIST, then use
> SHGetPathFromIDList to convert it back.  Explorer will always
> return the long file name if you use SHGetPathFromIDList.

> --
> (My return address is intentionally invalid to foil spammers.  Delete
> the
> ".---" to get my real address.  I do this on my own time with my own
> money;
> my responses are not to be considered official technical support or
> advice.)

I am very surprised to hear this answer from someone that works at
Microsoft. Just use the GetFullPathName function.

--
Joseph Partridge jose...@totcon.com
Partridge Software
http://www.partridgesoft.com

--------------D346EB7B9A00AB8B2DBE45AA
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Joseph Partridge
Content-Disposition: attachment; filename="vcard.vcf"

begin:          vcard
fn:             Joseph Partridge
n:              Partridge;Joseph
org:            Partridge Software
email;internet: jose...@totcon.com
title:          Software Developer
x-mozilla-cpt:  ;0
x-mozilla-html: FALSE
end:            vcard

--------------D346EB7B9A00AB8B2DBE45AA--

Re:How to retrieve long path name from short path name


Will using SHGFI_DISPLAYNAME exclude the file extension if specified in
explorer? (Or is that only done by GetFileTitle()...)??

Quote
Tarkan Yetiser wrote:
> In article <sJiyOhA4ba8zE...@chrism.demon.co.uk>,
> ch...@chrism.demon.co.uk says...
> > The purpose of "GetFullPathName" is to convert a "partial path" such
> as
> > "file.doc" or "..\file.doc" into a full pathname. That full pathname
> is
> > *not* guarenteed to be a "long" filename.

> > As someone had already said, the correct way to convert a "short
> path"
> > into a "long path" is to expand it component by component with
> > "FindFirstFile".

> Nah, that's overkill. Try the following instead:

> //-----------------------------------------------------------------
> BOOL  UTILS_short_2_long_fname (char *short_fname, char *long_fname) {

>    SHFILEINFO  sfi;

>    long_fname[0] = '\0';
>    sfi.szDisplayName[0] = '\0';
>    SHGetFileInfo (short_fname, 0, &sfi, sizeof(sfi),
> SHGFI_DISPLAYNAME);
>    if (sfi.szDisplayName[0] != '\0') {
>       strncpy (long_fname, sfi.szDisplayName, _MAX_PATH);
>       return (TRUE);
>    }
>    return (FALSE);
> }

> --
> Regards

> Tarkan Yetiser
> VDSARG
> tyeti...@vdsarg.com
> http://www.vdsarg.com

> data != information != knowledge != perspective != wisdom
> Perforin for WinWord finds and removes macro viruses.

--

Kind regards,
Phillip

** ThumbsPlus 3.0g1 is now available from our Web site **
--
 ///
//     Cerious Software, Inc.
//     http://www.cerious.com
 ///

Re:How to retrieve long path name from short path name


This is a multi-part message in MIME format.
--------------A477F637882FDF35517CD551
Content-Type: text/plain; charset=us-ascii
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Transfer-Encoding: 7bit

Quote
Chris Marriott wrote:
> In article <33F298A0.3E52C...@totcon.com>, Joseph Partridge
> <jose...@totcon.com> writes
> >I am very surprised to hear this answer from someone that works at
> >Microsoft. Just use the GetFullPathName function.

> Read my earlier reply, Joseph. Unlike you, Ray knows what the
> "GetFullPathName" function actually does. It is *not* the "opposite"
> of
> "GetShortPathName". As Ray correctly said, using "FindFirstFile" on
> each
> component of the name is the right answer.

> Chris

> ----------------------------------------------------------------
> Chris Marriott, Microsoft Certified Solution Developer.
> SkyMap Software, U.K. e-mail: ch...@skymap.com
> Visit our web site at http://www.skymap.com

You're absolutely right, Chris. My mistake, I should have paid more
attention to the online help.

--
Joseph Partridge jose...@totcon.com
Partridge Software
http://www.partridgesoft.com

--------------A477F637882FDF35517CD551
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Joseph Partridge
Content-Disposition: attachment; filename="vcard.vcf"

begin:          vcard
fn:             Joseph Partridge
n:              Partridge;Joseph
org:            Partridge Software
email;internet: jose...@totcon.com
title:          Software Developer
x-mozilla-cpt:  ;0
x-mozilla-html: FALSE
end:            vcard

--------------A477F637882FDF35517CD551--

Re:How to retrieve long path name from short path name


This is a multi-part message in MIME format.
--------------49E2A5533D45B10CC14011E2
Content-Type: text/plain; charset=us-ascii
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Transfer-Encoding: 7bit

Quote
Jan Oonk wrote:
> Raymond Chen [MS] (raymo...@microsoft.com.---) wrote:
> <snip>.
> : 2. Convert the filename to an ITEMIDLIST, then use
> : SHGetPathFromIDList to convert it back.  Explorer will always
> : return the long file name if you use SHGetPathFromIDList.
> <snip>

> Does the second solution also needs to do per component??

No, the shell should convert the whole thing in one gulp.

Quote
> I have made a routine in Delphi3 that converts a short filepath to
> long. But
> <snip>

> and why did those Microsoft boys didn't give us a opposite routine of
> GetShortPath??Really pathetic!

I agree, that is pathetic! I would think it would be equally difficult
to write a function to convert a short path to a long one.

--
Joseph Partridge jose...@totcon.com
Partridge Software
http://www.partridgesoft.com

--------------49E2A5533D45B10CC14011E2
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Joseph Partridge
Content-Disposition: attachment; filename="vcard.vcf"

begin:          vcard
fn:             Joseph Partridge
n:              Partridge;Joseph
org:            Partridge Software
email;internet: jose...@totcon.com
title:          Software Developer
x-mozilla-cpt:  ;0
x-mozilla-html: FALSE
end:            vcard

--------------49E2A5533D45B10CC14011E2--

Re:How to retrieve long path name from short path name


 I just implemented a solution using Raymond's second
suggestion.
It worked like a charm the first time. Here's the code:

static CString GetFullPath(LPCTSTR pszShortPath)
{
 CString stringIn(pszShortPath);

 // convert the short path to an IDL
 CComPtr<IShellFolder> pDesktopFolder;
 HRESULT hRes = ::SHGetDesktopFolder(&pDesktopFolder);
 if (FAILED(hRes))
  {
  ASSERT(FALSE);
  AfxThrowOleException(hRes);
  }
 LPITEMIDLIST pidl;
 CPSBSTR bstrShortPath(pszShortPath);
 ULONG nCharsParsed;

 hRes =
pDesktopFolder->ParseDisplayName(AfxGetMainWnd()->GetSafeHwn
d(),
    NULL, bstrShortPath, &nCharsParsed, &pidl, NULL);
 if (FAILED(hRes))
  {
  ASSERT(FALSE);
  AfxThrowOleException(hRes);
  }

 // convert the IDL to the full path
 TCHAR szFullPath[MAX_PATH];
 if (SHGetPathFromIDList(pidl, szFullPath))
  return szFullPath;
 return stringIn;

Quote
}

My only question: do I have to free the PIDL allocated by
ParseDisplayName(), and
if so, how?

Ken

Raymond Chen [MS] wrote in article
<33f2fd60.70560...@157.61.218.20>...

Quote
>1. Use FindFirstFile on each component to convert it from
short
>to long.

>2. Convert the filename to an ITEMIDLIST, then use
>SHGetPathFromIDList to convert it back.  Explorer will
always
>return the long file name if you use SHGetPathFromIDList.

>--
>(My return address is intentionally invalid to foil

spammers.  Delete the
Quote
>".---" to get my real address.  I do this on my own time
with my own money;
>my responses are not to be considered official technical

support or advice.)

Re:How to retrieve long path name from short path name


 Just answered my own question: I need to use SHGetMalloc to
get the malloc object to free the IDL with. Here's the
snippet:

 IMallocPtr pMalloc;
 hRes = ::SHGetMalloc(&pMalloc);
 if (FAILED(hRes))
  {
  ASSERT(FALSE);
  AfxThrowOleException(hRes);
  }
 pMalloc->Free(pidl);

Ken Freeman wrote in article ...

Quote
> I just implemented a solution using Raymond's second
>suggestion.
>It worked like a charm the first time. Here's the code:

>static CString GetFullPath(LPCTSTR pszShortPath)
>{
> CString stringIn(pszShortPath);

> // convert the short path to an IDL
> CComPtr<IShellFolder> pDesktopFolder;
> HRESULT hRes = ::SHGetDesktopFolder(&pDesktopFolder);
> if (FAILED(hRes))
>  {
>  ASSERT(FALSE);
>  AfxThrowOleException(hRes);
>  }
> LPITEMIDLIST pidl;
> CPSBSTR bstrShortPath(pszShortPath);
> ULONG nCharsParsed;

> hRes =
>pDesktopFolder->ParseDisplayName(AfxGetMainWnd()->GetSafeHw
n
>d(),
>    NULL, bstrShortPath, &nCharsParsed, &pidl, NULL);
> if (FAILED(hRes))
>  {
>  ASSERT(FALSE);
>  AfxThrowOleException(hRes);
>  }

> // convert the IDL to the full path
> TCHAR szFullPath[MAX_PATH];
> if (SHGetPathFromIDList(pidl, szFullPath))
>  return szFullPath;
> return stringIn;
>}

>My only question: do I have to free the PIDL allocated by
>ParseDisplayName(), and
>if so, how?

>Ken

>Raymond Chen [MS] wrote in article
><33f2fd60.70560...@157.61.218.20>...

>>1. Use FindFirstFile on each component to convert it from
>short
>>to long.

>>2. Convert the filename to an ITEMIDLIST, then use
>>SHGetPathFromIDList to convert it back.  Explorer will
>always
>>return the long file name if you use SHGetPathFromIDList.

>>--
>>(My return address is intentionally invalid to foil
>spammers.  Delete the
>>".---" to get my real address.  I do this on my own time
>with my own money;
>>my responses are not to be considered official technical
>support or advice.)

Go to page: [1] [2]

Other Threads