Recent

Author Topic: SysUtils.ExtractFileExt  (Read 1265 times)

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
SysUtils.ExtractFileExt
« on: September 14, 2021, 04:48:34 am »
Hello,

There is a bug in the ExtractFileExt function, when a file's name is null, this function returns null, for example:

SysUtils.ExtractFileExt('sample.txt') returns ".txt" however, SysUtils.ExtractFileExt('.txt') returns ""

Is there any other routine to extract filename's extension?

Thanks,

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: SysUtils.ExtractFileExt
« Reply #1 on: September 14, 2021, 05:01:22 am »
SysUtils.ExtractFileExt('sample.txt') returns ".txt" however, SysUtils.ExtractFileExt('.txt') returns ""

Which is correct because the filename ".txt" has no file extension. In UNIX and UNIX-like circles it is a hidden file.

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: SysUtils.ExtractFileExt
« Reply #2 on: September 15, 2021, 04:31:26 pm »
It's not correct! file name format should be like "name.ext"

if you try SysUtils.ExtractFileName('.txt') it will return null too!
so both functions return null.

SysUtils.ExtractFileExt('.txt') retuns ""
SysUtils.ExtractFileName('.txt') retuns ""

I expect to get ".txt" when I am calling the SysUtils.ExtractFileExt('.txt')

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: SysUtils.ExtractFileExt
« Reply #3 on: September 15, 2021, 05:16:19 pm »
if you try SysUtils.ExtractFileName('.txt') it will return null too!

I agree: in Linux, if '.txt' is the full name (so, without\no extension in this case) of a hidden file, SysUtils.ExtractFileName('.txt') should return the whole '.txt', imo (not null). No?
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: SysUtils.ExtractFileExt
« Reply #4 on: September 15, 2021, 05:16:36 pm »
if you try SysUtils.ExtractFileName('.txt') it will return null too!
No, it doesn’t, unless you’ve altered allowDirectorySeparators/allowDriveSeparators.

As trev already wrote, extractFileExt('.txt') correctly returns an empty string. The term file extension implies you’re extending a name. What “name” would you like to extend in .txt? Exactly, there is no name. On no OS in the world an empty string would constitute a valid file name, thus the entire string '.txt' is the file name and there is no file name extension.
Yours Sincerely
Kai Burghardt

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: SysUtils.ExtractFileExt
« Reply #5 on: September 15, 2021, 06:40:08 pm »
This behaviour is controlled by the global var FirstDotAtFileNameStartIsExtension.
It's default value is FALSE.

Code: Pascal  [Select][+][-]
  1.   FirstDotAtFileNameStartIsExtension := False;
  2.   S := extractfileext('.txt');
  3.   writeln('"',S,'"');
  4.   FirstDotAtFileNameStartIsExtension := True;
  5.   S := extractfileext('.txt');
  6.   writeln('"',S,'"');
Outputs:
Code: [Select]
""
".txt"

Bart

 

TinyPortal © 2005-2018