Board index » delphi » Short filename to long filename conversion

Short filename to long filename conversion

My apologies - I should have checked the index BEFORE asking the
question (a few weeks ago Patrick.K...@cch.cerberus.ch had answered a
similar question).

In case anyone is interested, the answer involves the repeated use of
FindFirst.

--
{*word*104}cat
AKA MauriceV

 

Re:Short filename to long filename conversion


When dragging files onto my app's icon, I find the short names
(including folder info) of these selected files in the parameter list.
How would I convert those short filespecs to long ones?

I've tried using FindFirst, and have found that both SearchRec.Name
and SearchRec.FindData.cFileName provide the long name (filename only,
no folder info), but have no idea how to determine the long version of
the path description.

Can anyone help? Any information would be appreciated.

--
{*word*104}cat
AKA MauriceV

Re:Short filename to long filename conversion


A year ago I asked the very same question. Did you try to search
www.dejanews.com? okokok here's the solution..have fun with it:

function MakeLongPath(ShortPath:String):String;
var PIDL:PItemIDList;
    szPath: Array[0..MAX_PATH] of Char;
    olePath: Array[0..MAX_PATH] of WideChar;
    chEaten,dwAttributes: Longint;
    Buffer:Array[0..Max_Path] of Char;
begin
 Result:=ShortPath; //If not succeed then return ShortPath
 StrCopy(szPath,PChar(ShortPath));
 If CoGetMalloc(MEMCTX_TASK,OLEManager)=S_OK then begin
  If SHGetDesktopFolder(PDeskTopFolder)=NOERROR then begin
   if MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1,
olePath,MAX_PATH)<>0 then begin
    If
PDesktopFolder.ParseDisplayName(0,nil,olePath,chEaten,PIDL,dwAttributes)=S_O
K then begin
     If SHGetPathFromIDList(PIDL,Buffer) then Result:=Buffer;
     OLEManager.Free(PIDL);
    end;
   end;
   PDesktopFolder._Release;
  end;
  OLEManager.Release;
 end;
end;

Hope that helps.............
Greetings Jan Oonk

Re:Short filename to long filename conversion


function xx(ShortFilename: String): String;
var
  FileInfo: TSHFileInfo;
begin
  if SHGetFileInfo(PChar(ShortFilename), 0, FileInfo, SizeOf(FileInfo),
SHGFI_DISPLAYNAME) <> 0 then
    Result := FileInfo.szDisplayName
  else
    Result := '';
end;

Quote
{*word*104}cat wrote:

> When dragging files onto my app's icon, I find the short names
> (including folder info) of these selected files in the parameter list.
> How would I convert those short filespecs to long ones?

> I've tried using FindFirst, and have found that both SearchRec.Name
> and SearchRec.FindData.cFileName provide the long name (filename only,
> no folder info), but have no idea how to determine the long version of
> the path description.

> Can anyone help? Any information would be appreciated.

> --
> {*word*104}cat
> AKA MauriceV

Re:Short filename to long filename conversion


guest357 schrieb in Nachricht <351C900B.40ECA...@po.cnet-ca.ne.jp>...

Quote
>function xx(ShortFilename: String): String;
>var
>  FileInfo: TSHFileInfo;
>begin
>  if SHGetFileInfo(PChar(ShortFilename), 0, FileInfo,
SizeOf(FileInfo),
>SHGFI_DISPLAYNAME) <> 0 then
>    Result := FileInfo.szDisplayName
>  else
>    Result := '';
>end;

This will give you the display name of the file, not the complete
path. I used something similar:

function LongPathName(ShortName: string): string;
var
  PIDL: PItemIDList;
  Shell: IShellFolder;
  WideName: array[0..MAX_PATH] of WideChar;
  AnsiName: array[0..MAX_PATH] of AnsiChar;
  Dummy: Longint;
begin
  Result := ShortName;
  if Succeeded(SHGetDesktopFolder(Shell)) then
  begin
    StringToWideChar(ShortName, WideName, MAX_PATH);
    if Succeeded(Shell.ParseDisplayName(0, nil, WideName, Dummy, PIDL,
Dummy)) then
    try
      SHGetPathFromIDList(PIDL, AnsiName);
      Result := AnsiName;
    finally
      CoTaskMemFree(PIDL);
    end;
  end;
end;

Re:Short filename to long filename conversion


If the user doesn't like recursion much, he or she might use something
like:

function LongFileSpec(const AFileSpec: string): string;
var Temp,TheDrive,TheDir: string; SearchRec: TSearchRec;
begin
  Temp:=AFileSpec;
  TheDrive:=ExtractFileDrive(Temp);
  Delete(Temp,1,Length(TheDrive));
  TheDir:=ExtractFileDir(Temp);
  while TheDir<>'\' do
    begin
      FindFirst(TheDrive+TheDir,faAnyFile,SearchRec);
      Result:=Concat(SearchRec.Name,'\',Result);
      FindClose(SearchRec);
      TheDir:=ExtractFileDir(TheDir)
    end;
  Result:=Concat(TheDrive,'\',Result);
  FindFirst(AFileSpec,faAnyFile,SearchRec);
  Result:=Concat(Result,SearchRec.Name);
  FindClose(SearchRec)
end;

--
{*word*104}cat

We're born {*word*192}, hungry, covered in {*word*76}
and screaming. Then it gets worse.

Re:Short filename to long filename conversion


On Thu, 02 Apr 1998 01:36:45 GMT, Mauri...@bellsouth.net ({*word*104}cat)
wrote:

Quote
>If the user doesn't like recursion much, he or she might use something
>like:

>function LongFileSpec(const AFileSpec: string): string;
>var Temp,TheDrive,TheDir: string; SearchRec: TSearchRec;
>begin
>  Temp:=AFileSpec;
>  TheDrive:=ExtractFileDrive(Temp);
>  Delete(Temp,1,Length(TheDrive));
>  TheDir:=ExtractFileDir(Temp);
>  while TheDir<>'\' do
>    begin
>      FindFirst(TheDrive+TheDir,faAnyFile,SearchRec);
>      Result:=Concat(SearchRec.Name,'\',Result);
>      FindClose(SearchRec);
>      TheDir:=ExtractFileDir(TheDir)
>    end;
>  Result:=Concat(TheDrive,'\',Result);
>  FindFirst(AFileSpec,faAnyFile,SearchRec);
>  Result:=Concat(Result,SearchRec.Name);
>  FindClose(SearchRec)
>end;

Hm. So much for checking the details.

The user might want to insert

Result := '';

just before the WHILE loop (unless of course he or she is in a funny
mood and doesn't care about accuracy.)

--
{*word*104}cat

We're born {*word*192}, hungry, covered in {*word*76}
and screaming. Then it gets worse.

Re:Short filename to long filename conversion


Quote
{*word*104}cat wrote...
>If the user doesn't like recursion much, he or she might use
something
>like:

>function LongFileSpec(const AFileSpec: string): string;
>var Temp,TheDrive,TheDir: string; SearchRec: TSearchRec;

As far as I can see, my solution doesn't use recursion. It *does* use
OLE though. This is not everyone's favourite, I know.

BTW, yesterday, a guy from Microsoft gave the same solution to a
similar question on another NG however (he used C++, of course):

void GetLongName(const char *shortname, char *longname)
{
  *longname = '\0';
  IShellFolder *desktop;
  if (SHGetDesktopFolder(&desktop) == NOERROR) {
    wchar_t widename[MAX_PATH];
    MultiByteToWideChar(CP_ACP, 0, shortname, -1, widename, MAX_PATH);
    ULONG len, attr;
    LPITEMIDLIST id = NULL;
    desktop->ParseDisplayName(NULL, NULL, widename, &len, &id, &attr);
    if (id) {
      SHGetPathFromIDList(id, longname);
      IMalloc *mem;
      if (SHGetMalloc(&mem) == NOERROR) {
        mem->Free(id);
        mem->Release();
      }
    }
    desktop->Release();
  }

Quote
}

Rudy velthuis

Re:Short filename to long filename conversion


On Thu, 2 Apr 1998 22:30:38 +0200, "Rudy Velthuis" <rvelth...@cww.de>
wrote:

Quote
>As far as I can see, my solution doesn't use recursion. It *does* use
>OLE though. This is not everyone's favourite, I know.

>BTW, yesterday, a guy from Microsoft gave the same solution to a
>similar question on another NG however (he used C++, of course):

My apologies; I think I responded to the wrong post (somebody
somewhere did provide a recursive routine).

Thanks for providing the OLE-based method, I always like to learn new
ways to approach things.

--
{*word*104}cat

We're born {*word*192}, hungry, covered in {*word*76}
and screaming. Then it gets worse.

Other Threads