[fpc-pascal]ExtractFileDir ?

Iván Montes senbei at terra.es
Thu Feb 13 15:08:09 CET 2003

----- Original Message -----
From: "Anton Tichawa" <anton.tichawa at chello.at>

> So there are two errors:
> 1. I decremented below j, but the programmer had in mind that i was above
> the while-statement should read 'while (I > J) ..'
> 2. The Copy returns the full string for length 0 -
> Note: I don't like the while structure for combined conditions. I prefer a
> repeat-until with several breaks. That usually makes things more clear. I
> know that this is not considered 'well-structured' programming, but the
> we live in is not 'well-structured'. Should I post a new, tested "repeat -
> break - break - until" version of this function?
my mistake, sorry, hopefully this should work fine

c:\test\ => \test
c:test\ => test
\test => \
c:a => <empty string>
c:\ => \
\\Workgroup\server\command.com => \\Workgroup\server

function ExtractFileDir( const FileName :string):string;
    len, j :integer;
  //Get the filename length
  len := length(FileName);
  //Check if it has defined a drive
  if (len>1) AND (FileName[2]=':') then j:=3
                                   else j:=1;

  //Find the last directory separator if any
  while (len>=j) AND NOT(FileName[len] in ['\','/']) do Dec(len);
  //Remove the ending slash
  if len>j then dec(len);

  if len<j then result := '' //no directory found
           else result := Copy(FileName, j, len-j+1);

More information about the fpc-pascal mailing list