Board index » delphi » Help - Win32 API - Short Filename to Long Filename

Help - Win32 API - Short Filename to Long Filename

Hi,

I have what should be just a simple problem. I have a "short" path name and
I need a function that will make this a "long" path name. Best explained by
an example:

c:\multi~1\mypres~1\    -->    c:\multi media\my presentations\

I know you can go the other way but to date I have tried GetFullPathName
and another I can't think of off-hand but these only return at best the
full name of the last directory specified. Maybe I am just lazy but I
really don't want to have to write a routine that goes through every single
directory in the path one by one. I would have thought there would be a
simple call to do the conversion in one hit??? Has anyone any suggestions?

I am using D1 and Call32NT to call Win32 APIs so any inbuilt functionality
to do the above in D2 is of little or no use to me - I need an API call or
custom routine.

I would appreciate any help, or if anyone can point me to an existing
resource on how to do this.

cc: to madk...@tpgi.com.au would really be appreciated.

Thank you all in advance.

Cheers, Michael.

 

Re:Help - Win32 API - Short Filename to Long Filename


On 13 Jan 1997 01:17:39 GMT, "Michael Adkins" <madk...@tpgi.com.au>
wrote:

Quote
>I have what should be just a simple problem. I have a "short" path name and
>I need a function that will make this a "long" path name. Best explained by
>an example:

>c:\multi~1\mypres~1\    -->    c:\multi media\my presentations\

{ Recursively expand a short file name to a long file name. }
function ExpandShortName(const ShortName: string): string;
var
  FindData: TWin32FindData;
  Handle: THandle;
  Path, Tail: string;
begin
  Path := ExtractFilePath(ShortName);
  Tail := ExtractFileName(ShortName);
  if Tail = '' then
    { Just the drive, e.g., D:\, so nothing to expand. }
    Result := ShortName
  else
  begin
    { strip the trailing backslash, prior to expanding the directory.
Quote
}

    if (Length(Path) > 0) and (Path[Length(Path)] = '\') then
      Delete(Path, Length(Path), 1);
    Path := ExpandShortName(Path);
    { Retstore the backslash, to append the file name. }
    if (Length(Path) > 0) and (Path[Length(Path)] <> '\') then
      AppendStr(Path, '\');
    { Get the base file name. }
    Handle := FindFirstFile(PChar(Path + Tail), FindData);
    if Handle = Invalid_Handle_Value then
      { File does not exist, so keep the short name. }
      Result := Path + Tail
    else
      try
        Result := Path + FindData.cFilename;
      finally
        WinProcs.FindClose(Handle);
      end;
  end;
end;

{ Get the long file name for a short name. }
function GetLongFilename(ShortName: string): string;
begin
  Result := ExpandShortName(ExpandFileName(ShortName));
end;

--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Re:Help - Win32 API - Short Filename to Long Filename


Quote
Michael Adkins (madk...@tpgi.com.au) wrote:

:
: c:\multi~1\mypres~1\    -->    c:\multi media\my presentations\
:
: I know you can go the other way but to date I have tried GetFullPathName
:
I need to go the other way, please tell me how to do it!

Seppo

         seppo.virta...@utu.fi              http://www.utu.fi/~seaavi
******************************************************************************

Re:Help - Win32 API - Short Filename to Long Filename


Seppo A. Virtanen <sea...@utu.fi> wrote in article
<5bi00q$...@castor.utu.fi>...

Quote
> I need to go the other way, please tell me how to do it!

Here's how to shorten a long path name.

Note, this is a solution for Delphi1 if you are using Delphi1.0 you will
need Call32NT.Pas.

If you are using Delphi2 you will be able to simplify the below
significantly - basically you only need the GetShortPathName function call.

=========================================

unit Shorten;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes,
  Call32NT;

function ShortName(Filename: string): string;

var
  W32GetShortPathName:
    function(Longname,Shortname:pchar; size:longint; id:longint):Longbool;
  id_W32GetShortPathName: longint;

var F32BitFunctionsAvailable : Boolean;

implementation

function ShortName(Filename: string): string;
var
  Short, Long : string;
  PShort, PLong : PChar;
begin
  if not F32BitFunctionsAvailable then begin result := filename; exit; end;
  if filename = '' then begin result := filename; exit; end;
  Long := FileName + #0;
  PLong := @Long[1];
  Short := ' ' + #0;
  PShort := @Short[1];
  W32GetShortPathName(PLong, PShort, sizeof(Short),
id_W32GetShortPathName);
  Result := StrPas(PShort);
end;  { Shorten }

initialization
  {Initialization of the 32 bit functions}
  @W32GetShortPathName:=@Call32;

  id_W32GetShortPathName:=Declare32('GetShortPathName', 'kernel32', 'ppi');

  {Check if everything went well. If there was only a single error,
   Call32NTError=false}
  if Call32NTError then F32BitFunctionsAvailable := false
                   else F32BitFunctionsAvailable := true;
end.

=========================================

Michael Adkins.
madk...@tpgi.com.au

Other Threads