Recent

Author Topic: SysUtils.ExtractFileExt  (Read 787 times)

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 13
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: 1705
  • 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.
Lazarus 2.3, FPC 3.3.1 macOS 12.0.1 x86_64 Xcode 13.1
Lazarus 2.3, FPC 3.3.1 macOS 12.0.1 aarch64 Xcode 13.1
Lazarus 2.3, FPC 3.2.2 FreeBSD 13.0 amd64 VM
Lazarus 2.3, FPC 3.2.2 FreeBSD 12.2 amd64 VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 VM
Lazarus 2.0.10 FPC 3.2.0 Win10 VM

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 13
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: 644
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

  • Sr. Member
  • ****
  • Posts: 382
  • 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: 4516
    • 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