Page 1 of 4

Remove the extension part of a file.

Posted: Mon Sep 21, 2009 9:49 pm
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

Re: Remove the extension part of a file.

Posted: Mon Sep 21, 2009 9:54 pm
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

Re: Remove the extension part of a file.

Posted: Mon Sep 21, 2009 11:25 pm
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

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 2:13 am
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,".")

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 5:57 am
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

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 6:29 am
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

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 6:47 am
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

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 6:51 am
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 :)

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 8:30 am
by blueznl
:lol:

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 11:02 am
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

Re: Remove the extension part of a file.

Posted: Tue Sep 22, 2009 1:42 pm
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

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 10:27 am
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

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 10:47 am
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!

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 10:49 am
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! ;)

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 11:35 am
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