[fpc-pascal]ExtractFileDir ?

Iván Montes senbei at terra.es
Thu Feb 13 03:44:37 CET 2003


[OT: Sorry if this was posted before but I'm having some weird problems with
email]

According to the docs
http://www.freepascal.org/docs-html/units/node23.html#SECTION002366000000000
000000

"ExtractFileDir returns only the directory part of FileName, not including a
driveletter."

But it does return the driveletter, so either the docs are mistaken or the
RTL.

If the docs are right here there is a function that will return only the
dir, removing any drive letter.
 ie:
  c:\testdir\test\file.ext  => \testdir\test
  c:\testdir\ => \testdir
  c:testdir\ => testdir
  \testdir\test.exe => \testdir

 [objpas\fina.inc]

 function ExtractFileDir(const FileName: string): string;
 var i,j: longint;
 begin
   I := Length(FileName);
   if (I>1) and (FileName[2]=':') then j:=3
                      else j:=1;

   I := Length(FileName);
   while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
   if (I > j) and (FileName[I] in ['\', '/']) and
      not (FileName[I - 1] in ['/', '\', ':']) then Dec(I);

   Result := Copy(FileName, j, I-j+1);
 end;


HTH, ivan




More information about the fpc-pascal mailing list