GetExtensionPart fails with a space

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Re: GetExtensionPart fails with a space

Post 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)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: GetExtensionPart fails with a space

Post 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
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetExtensionPart fails with a space

Post 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 :-)
Post Reply