Long filename

Convert a DOS 8.3 format filename in to a long format filename.

Contributor: ANGUS JOHNSON


A number of people have been asking about

the API function GetFullPathName recently.

This function does not do what its name implies.

I have written a function which does convert a

short pathname into a long pathname and thought

I would share it with you.

NB: I haven't done any major checks for bugs so

no guarantees!


Angus Johnson



function GetLongFileName(fn: string): string;


  l,r: integer;

  path: string;

  sr: TSearchRec;


  {return '' if invalid path}

  if (length(fn)<3) or (pos(':\',fn)<>2) then begin

    result := '';



  {return if root directory}

  if length(fn) =3 then begin

    result := uppercase(fn);



  path := uppercase(copy(fn,1,3)); {path = root dir}

  l := 4;

  while true do begin {top of loop}

    r := l;

    while (fn[r] <> '\') and (r <= length(fn)) do inc(r);

    if Findfirst(path+copy(fn,l,r-l),faAnyfile,sr) = 0 then begin {OK}

      if (sr.attr and faDirectory) <> 0 then begin {yes, it is a directory}

        path := path + sr.name+'\';


        if r >= length(fn) then begin

          result := path;



        l := r+1;


      else begin {not a directory!}

        if r > length(fn) then dec(r);

        if (fn[r] = '\') or (r <> length(fn)) then {an error!}

          result := ''

        else begin {must be a file}

          result := path + sr.name; {OK!}






    else begin {An Error!!!!}

      result := '';




  end; {bottom of loop}



Share this article!

Follow us!

Find more helpful articles: