Board index » delphi » Converting Short Filenames to Long Filenames

Converting Short Filenames to Long Filenames

Hi,

Can someone please tell me how I convert a short filename eg
c:\progra~1\access~1\this_i~1.txt into a long filename eg c:\program
files\accessories\this is a long filename.txt. I am using Delphi Desktop
2.0 and I have searched every damn help file I can find along with the
manuals and I can't find anything that gives me the answer I'm seeking.
Please help. This problem is driving me nuts.

Thanks in advance

Greg Bell
--

It's news Jim, but not as we know it.

 

Re:Converting Short Filenames to Long Filenames


Quote
>Can someone please tell me how I convert a short filename eg
>c:\progra~1\access~1\this_i~1.txt into a long filename eg c:\program
>files\accessories\this is a long filename.txt.

Off the top of my head, I think you need the API function SHGetFileInfo.

Jeremy Collins

"There's a cool web of language draws us in,
    Retreat from too much joy or too much fear"
-Robert Graves, 'The Cool Web', 1927

Re:Converting Short Filenames to Long Filenames


Quote
Greg Bell wrote:

> Hi,

> Can someone please tell me how I convert a short filename eg
> c:\progra~1\access~1\this_i~1.txt into a long filename eg c:\program
> files\accessories\this is a long filename.txt. I am using Delphi Desktop
> 2.0 and I have searched every damn help file I can find along with the
> manuals and I can't find anything that gives me the answer I'm seeking.
> Please help. This problem is driving me nuts.

> Thanks in advance

> Greg Bell
> --

> It's news Jim, but not as we know it.

You may have to use FindFirst/FindNext/SysUtils.FindClose to get a
TSearchRec that's Win32FindData's alt name (short) matches the file
you are interested in. Then simply look at the the FileName for the long
file name.

Take a look at:

TI3199 - Using FindFirst and the WIN_32_FIND_DATA structure

available at:

http://www.borland.com/devsupport/delphi/ti_list/

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/

Re:Converting Short Filenames to Long Filenames


Quote
>Can someone please tell me how I convert a short filename eg
>c:\progra~1\access~1\this_i~1.txt into a long filename eg c:\program
>files\accessories\this is a long filename.txt.

There might be a better way, but this works for me:

function AlternateToLFN(alternateName: String): String;
var temp: TWIN32FindData;
    searchHandle: THandle;
begin
  searchHandle :=
    FindFirstFile(PChar(alternateName), temp);
  if searchHandle <> ERROR_INVALID_HANDLE then
  begin
    result := String(temp.cFileName);
    if result = '' then
      result := String(temp.cAlternateFileName);
  end
  else
    result := '';
  Windows.FindClose(searchHandle);
end;

function AlternateToLongPath(alternateName: String): String;
var lastSlash: PChar;
    tempPathPtr: PChar;
begin
  result := '';
  tempPathPtr := PChar(alternateName);
  lastSlash := StrRScan(tempPathPtr, '\');
  while lastSlash <> nil do
  begin
    result := '\' + AlternateToLFN(tempPathPtr) +
   result;
    if lastSlash <> nil then
    begin
      lastSlash^ := char(0);
      lastSlash := StrRScan(tempPathPtr, '\');
    end
  end;
  result := tempPathPtr + result;
end;

begin

{shortpath := 'c:\progra~1\access~1\this_i~1.txt'; }

longpath := AlternateToLongPath(shortpath);

{if longpath = 'c:\program files\accessories\this is a long filename.txt'
    then showmessage('It works.');  }

end;

Bryan Wilken

--
Bryan Wilken
bwil...@midusa.net
http://www.gcnet.com/bw/

Re:Converting Short Filenames to Long Filenames


Greg Bell <gre...@ihug.co.nz> wrote in article
<01bc4793$8b0a3d80$d2efd...@gregbl.ihug.co.nz>...

Quote
> Hi,

> Can someone please tell me how I convert a short filename eg
> c:\progra~1\access~1\this_i~1.txt into a long filename eg c:\program
> files\accessories\this is a long filename.txt. I am using Delphi Desktop
> 2.0 and I have searched every damn help file I can find along with the
> manuals and I can't find anything that gives me the answer I'm seeking.
> Please help. This problem is driving me nuts.

Here's how I did it, but there's probably a more straightforward way...

uses SysUtils;

function ExpandToLongFileName(AFileName : String) : String;
var
  FileInfo : TSearchRec;
begin
  FindFirst(AFileName, faAnyFile, FileInfo);
  Result := FileInfo.Name;
  FindClose(FileInfo);
end

======================================================================
Steve Dwire  -  Pensacola Christian College (include std. disclaimers)
Work: <sdw...@pcci.edu>                        Home: <dwi...@gulf.net>
WWW:  <http://www.pcola.gulf.net/~dwires>

Re:Converting Short Filenames to Long Filenames


Try using the GetFullPathName() Win32 API function, instead.  Much
easier.

Hope this helps,
-Jim Rofkar.

Re:Converting Short Filenames to Long Filenames


On 23 Apr 1997 13:31:26 GMT, "Steve Dwire" <sdw...@pcci.edu> wrote:

Quote
>Here's how I did it, but there's probably a more straightforward way...

>uses SysUtils;

>function ExpandToLongFileName(AFileName : String) : String;
>var
>  FileInfo : TSearchRec;
>begin
>  FindFirst(AFileName, faAnyFile, FileInfo);
>  Result := FileInfo.Name;
>  FindClose(FileInfo);
>end

Take care. This expands only the filename, not the path. FindFirst
only expands the last part, e.g. when you pass
C:\Progra~1\execut~1.exe you will get C:\Progra~1\executable.exe and
not C:\Program Files\executable.exe.

Cheers,

Marcus

*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Marcus Monnig - minib...@cs.tu-berlin.de - PGP Public Key on request
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

Re:Converting Short Filenames to Long Filenames


In article <335FA5B8.4...@instinet.com> on  Thu, 24 Apr 1997 13:26:00
-0500 "James D. Rofkar" <jim_rofkar%lotusnot...@instinet.com> wrote:

Quote

> Try using the GetFullPathName() Win32 API function, instead.  Much
> easier.

Except that doesn't actually do what you'd expect. It simply takes the
pathname of the current directory (which is not neccessarily the
directory where the file is!) and appends the name of the file passed to
the function.  It doesn't check to see if the file actually exists
there.

For example, if the file passed to GetFullPathName is
C:\DELPHI~1\MYFILE.EXE and the current directory is
E:\SomeOtherDirectory GetFullPathName will return
E:\SomeOtherDirectory\MYFILE.EXE!

Not really very useful :-(

--
Dr. Warren L. Kovach                     Internet: w...@kovcomp.co.uk
Kovach Computing Services               tel./fax: +44-(0)1248-450414
85 Nant-y-Felin, Pentraeth, Anglesey        
Wales LL75 8UY U.K.                   WWW: http://www.kovcomp.co.uk/

Other Threads