Remove the extension part of a file.

Share your advanced PureBasic knowledge/code with the community.
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Remove the extension part of a file.

Post by Seldon »

Just a tiny function I needed. Maybe it can be useful for someone else.

Code: Select all

Declare.s RemoveExtensionPart(File$)

FileName$="pinco.exe"
Debug RemoveExtensionPart(FileName$)

Procedure.s RemoveExtensionPart(File$)
  Protected tmp$
  tmp$=GetExtensionPart(File$)
  If tmp$
    tmp$="."+tmp$
    ProcedureReturn(RemoveString(File$,tmp$))
  EndIf
  ProcedureReturn("")
EndProcedure
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Remove the extension part of a file.

Post by ts-soft »

thx,

here a bit shorter (not a procedure)

Code: Select all

FileName$="pinco.exe"
Debug Left(FileName$, Len(FileName$)  -  Len(GetExtensionPart(FileName$))  - 1)
Greetings
Thomas
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Remove the extension part of a file.

Post by Demivec »

There seem to be some inconsistencies with the version by Seldon and by ts-soft.

I did a variation of Seldon's code, calling it RemoveExtensionPart2(), that corrects the return value when there is no extension present.

Code: Select all

Procedure.s RemoveExtensionPart(File$)
  Protected tmp$
  tmp$=GetExtensionPart(File$)
  If tmp$
    tmp$="."+tmp$
    ProcedureReturn(RemoveString(File$,tmp$))
  EndIf
  ProcedureReturn("")
EndProcedure

Procedure.s RemoveExtensionPart2(File$)
  Protected tmp$
  tmp$=GetExtensionPart(File$)
  If tmp$
    tmp$="."+tmp$
    ProcedureReturn(RemoveString(File$,tmp$))
  EndIf
  ProcedureReturn File$
EndProcedure

Debug "filename: " + Chr(34) + filename$ + Chr(34)
filename$="pinco.exe"
Debug RemoveExtensionPart(filename$)
Debug RemoveExtensionPart2(filename$)
Debug Left(filename$, Len(filename$)  -  Len(GetExtensionPart(filename$))  - 1)

filename$="pinco."
Debug "filename: " + Chr(34) + filename$ + Chr(34)
Debug RemoveExtensionPart(filename$) ;fails
Debug RemoveExtensionPart2(filename$)
Debug Left(filename$, Len(filename$)  -  Len(GetExtensionPart(filename$))  - 1)

filename$="pinco"
Debug "filename: " + Chr(34) + filename$ + Chr(34)
Debug RemoveExtensionPart(filename$) ;fails
Debug RemoveExtensionPart2(filename$)
Debug Left(filename$, Len(filename$)  -  Len(GetExtensionPart(filename$))  - 1) ;fails
Seymour Clufley
Addict
Addict
Posts: 1265
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Remove the extension part of a file.

Post by Seymour Clufley »

Here's what I always do:

Code: Select all

ext.s = StringField(filename+".",2,".")
Safer:

Code: Select all

dots = CountString(filename,".")
ext.s = StringField(filename+".",dots+1,".")
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Remove the extension part of a file.

Post by Little John »

Demivec wrote:I did a variation of Seldon's code, calling it RemoveExtensionPart2(), that corrects the return value when there is no extension present.
Yes, that correction was necessary.
Here is my variation: :-)

Code: Select all

Procedure.s RemoveExtensionPart (file$)
   Protected lenExt

   lenExt = Len(GetExtensionPart(file$))
   If lenExt
      ProcedureReturn(Left(file$, Len(file$)-lenExt-1))
   Else
      ProcedureReturn file$
   EndIf
EndProcedure
Regards, Little John
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Remove the extension part of a file.

Post by ts-soft »

saferversion:

Code: Select all

FileName$ = "pinco"
If GetExtensionPart(FileName$)
  Debug Left(FileName$, Len(FileName$)  -  Len(GetExtensionPart(FileName$))  - 1)
Else
  Debug FileName$
EndIf
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Remove the extension part of a file.

Post by Little John »

ts-soft wrote:saferversion:
Thomas, do you mean that this version is safer than mine? If so, why do you think so? Both versions are almost equal.

Regards, Little John
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Remove the extension part of a file.

Post by ts-soft »

Little John wrote:
ts-soft wrote:saferversion:
Thomas, do you mean that this version is safer than mine? If so, why do you think so? Both versions are almost equal.

Regards, Little John
Is Safer than my first version :)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Remove the extension part of a file.

Post by blueznl »

:lol:
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Remove the extension part of a file.

Post by breeze4me »

Windows only.

Code: Select all

file.s{1024} = "c:\my files\my data.ico file.ico"

PathRemoveExtension_(@file)

Debug file  ; c:\my files\my data.ico file
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: Remove the extension part of a file.

Post by Seldon »

Thanks for all suggestions. OK, I've just collected the best solutions (IMHO).

Code: Select all

Declare.s RemoveExtensionPart(File$)

FileName$="pinco.exe"
Debug RemoveExtensionPart(FileName$)

Procedure.s RemoveExtensionPart(File$)
CompilerIf #PB_Compiler_OS<>#PB_OS_Windows
  Protected extension_length
  extension_length=Len(GetExtensionPart(File$))
  If extension_length
    ProcedureReturn(Left(File$,Len(File$)-extension_length-1))
  EndIf
  ProcedureReturn(File$)
CompilerElse
  Protected tmp$=File$
  PathRemoveExtension_(@tmp$)
  ProcedureReturn(tmp$)
CompilerEndIf
EndProcedure
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Remove the extension part of a file.

Post by Joakim Christiansen »

ts-soft wrote:saferversion:

Code: Select all

FileName$ = "pinco"
If GetExtensionPart(FileName$)
  Debug Left(FileName$, Len(FileName$)  -  Len(GetExtensionPart(FileName$))  - 1)
Else
  Debug FileName$
EndIf
Also safe:

Code: Select all

filename$ = "this file has no extension"
Debug StringField(filename$,1,".")

filename$ = "this file has a extension.txt"
Debug StringField(filename$,1,".")
:lol: :twisted: :D
Can I get a cookie now?

sorry for fooling around, I coluld make a uber nice procedure another day
I like logic, hence I dislike humans but love computers.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Remove the extension part of a file.

Post by Kaeru Gaman »

Joakim Christiansen wrote:Also safe:

Code: Select all

filename$ = "this file has no extension"
Debug StringField(filename$,1,".")

filename$ = "this file has a extension.txt"
Debug StringField(filename$,1,".")

Code: Select all

filename$ = "Review 2009.09.23.doc"
Debug StringField(filename$,1,".")
pwned :!:
Joakim Christiansen wrote:Can I get a cookie now?
NOPE!
oh... and have a nice day.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 676
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Remove the extension part of a file.

Post by Kurzer »

No, its not safe. Try this example:

FileName$ = "this file has a extension.txt but its a.doc"
Debug StringField(FileName$,1,".")

No cookie this time! ;)

Edit: Yeah, frogs rulez! ;)
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Re: Remove the extension part of a file.

Post by Hroudtwolf »

Hi,

Just another way...

Code: Select all

; 2009 Hroudtwolf
; PureBasic 4.x

Structure tChar
   StructureUnion
      c.c
      s.s {1}
   EndStructureUnion
EndStructure

Procedure.s removeExtensionPart (sSource.s)
   Protected *sourceStart           = @ sSource
   Protected *sourceEnd    .tChar   = *sourceStart 
   
   If Not (*sourceStart)
      ProcedureReturn sSource
   EndIf
   
   *sourceEnd + StringByteLength (sSource)
   
   While *sourceEnd > *sourceStart 
      
      If *sourceEnd\c = '.'
         *sourceEnd\c = #Null
         Break
      EndIf
      
      *sourceEnd - SizeOf (tChar)
   Wend
   
   ProcedureReturn sSource
EndProcedure

Debug removeExtensionPart ("thisismyfilename.ext.ext.bla.blup")
Regards
Wolf
Post Reply