Page 2 of 2

Re: GetExtensionPart fails with a space

Posted: Thu Jul 31, 2025 1:53 am
by BarryG
This is what I'm using now, since it covers all situations (spaces, multiple dots, dots at the start and end, dots in the path):

Code: Select all

Procedure.s GetFileExtension(file$)
  ext$=GetExtensionPart(file$)
  If ext$="" And Right(file$,1)<>"."
    dots=CountString(file$,".")
    If dots
      ext$=StringField(file$,dots+1,".")
    EndIf
  EndIf
  ProcedureReturn ext$
EndProcedure

Debug GetFileExtension("C:\File Path\.John Doe") ; John Doe
Debug GetFileExtension("C:\File Path\Name. . .John Doe") ; John Doe
Debug GetFileExtension("C:\File.Path\Name....John Doe....") ; "" (correct)

Re: GetExtensionPart fails with a space

Posted: Thu Jul 31, 2025 4:17 am
by Demivec
BarryG wrote: Thu Jul 31, 2025 1:53 am This is what I'm using now, since it covers all situations (spaces, multiple dots, dots at the start and end, dots in the path):
For windows, a full stop (period) can be the first character of a filename. If the first character is the only period in the filename then there is no extender.

Thus:

Code: Select all

Debug GetFileExtension("C:\File Path\.John Doe") ; *** should instead return ""
https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file

Re: GetExtensionPart fails with a space

Posted: Thu Jul 31, 2025 6:38 am
by Little John
Demivec wrote: Thu Jul 31, 2025 4:17 am
BarryG wrote: Thu Jul 31, 2025 1:53 am This is what I'm using now, since it covers all situations (spaces, multiple dots, dots at the start and end, dots in the path):
For windows, a full stop (period) can be the first character of a filename. If the first character is the only period in the filename then there is no extender.

Thus:

Code: Select all

Debug GetFileExtension("C:\File Path\.John Doe") ; *** should instead return ""
Exactly!
See also viewtopic.php?p=643530#p643530 :-)