Board index » delphi » D2: Long filename to Short Conversion

D2: Long filename to Short Conversion

Hi,

Can anybody please tell me how to convert a long filename to the short 8.3
version without using GetShortPathName.  I have to read files from a CD and
with older drivers,  only the 8.3 filename is recognised.

Any help would be most gratefully received.

Thanks,

Mark Edwards

Private e-mail: m...@creativity.demon.co.uk
Corporate e-mail: lan...@cix.compulink.co.uk

 

Re:D2: Long filename to Short Conversion


Quote
"Mark Edwards" <m...@creativity.demon.co.uk> wrote:
>Can anybody please tell me how to convert a long filename to the short 8.3
>version without using GetShortPathName.  I have to read files from a CD and
>with older drivers,  only the 8.3 filename is recognised.

Why don't you want to use GetShortPathName?

Anyway, you could use FindFirst.  Just pass it the fully qualified
pathname, and it will fill in the TSearchRec you pass.  Then, I think you
can use the 'Name' value of the TSearchRec.  Can't remember for sure if
this is converted into short form or not.  If not, use
TSearchRec.FindData.cAlternateFileName.  It's an array of char, so you'll
have to typecase is working with string types.

Regards,
Brad
bstow...@pobox.com
Free Delphi Stuff:  http://www.pobox.com/~bstowers/delphi/

Re:D2: Long filename to Short Conversion


Quote
"Mark Edwards" <m...@creativity.demon.co.uk> wrote:

Yea try this code i came up with when i had tha same problem

the lfntoalternate function i got out of the swags, the rest is mine
you can feed a whole structure + filename example:

function lsfilename('c:\windows\hello matey\this is a
test\ok.bb.cc.exe');

will return the short version of the whole line! :)

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

function lsfilename(name:string):string;
var
   chstr:array [0..300] of char;
   countn,partcount:integer;
   newstr,part:string;
   ch:char;
   ignore:boolean;
   test:string;
begin
     part:='';newstr:=''; partcount:=0; ignore:=false;
     strpcopy(chstr,name);
     for countn:=0 to length(name)-1 do
     begin
          ch:=chstr[countn];
          if (ch='\') or (ch=':') then
          begin
               if partcount>7 then
               begin
                    newstr:=newstr+LFNToAlternate(newstr+part);
               end else
               begin
                    newstr:=newstr+part;
               end;
               if ch='\' then
               begin
                  newstr:=newstr+'\';
               end else
               begin
                   newstr:=newstr+':';
               end;
               part:=''; partcount:=0; ignore:=false;
          end else
          begin
               partcount:=partcount+1;
               part:=part+ch;
          end;
     end;
     if length(part)>0 then
     begin
          test:=LFNToAlternate(newstr+part);
          if length(test)>0 then
          begin
               lsfilename:=newstr+test;
          end else
          begin
               lsfilename:=newstr+part;
          end;
     end else
     begin
          lsfilename:=newstr;
     end;
end;

!Seeper!

j...@es.co.nz

Other Threads