Remove the extension part of a file.

Share your advanced PureBasic knowledge/code with the community.
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 »

@Wolf
producing uncleanable garbage is no alternative... ;)
oh... and have a nice day.
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 »

Maybe you're right. I don't know if the stringmanager notes the memory-lenght of a string or it checks the null termination.
Therefore it could be unsafe.

Regards
Wolf
zikitrake
Addict
Addict
Posts: 875
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Remove the extension part of a file.

Post by zikitrake »

:D My code:

Code: Select all

Procedure.s noext(tmp$)
	pos.l = FindString(tmp$, ".", 1)
	r$ = tmp$
	Repeat
		posseg = pos
		If pos : r$ = Left(tmp$, pos-1) : EndIf
		pos.l = FindString(tmp$, ".", pos + 1)
		If pos< = posseg : Break : EndIf
	ForEver
	ProcedureReturn r$
EndProcedure

Debug noext("I.have.too.many.points.txt")
Debug noext("I have only one point.txt")
Debug noext("I haven't points")

PB 6.21 beta, PureVision User
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Remove the extension part of a file.

Post by helpy »

and how will your procedures handle ".htaccess" ;-) ?
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
zikitrake
Addict
Addict
Posts: 875
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Remove the extension part of a file.

Post by zikitrake »

helpy wrote:and how will your procedures handle ".htaccess" ;-) ?
:D

Code: Select all

Procedure.s noext(tmp$)
	pos.l = FindString(tmp$, ".", 1)
	r$ = tmp$
	Repeat
		posseg = pos
		If pos > 1 : r$ = Left(tmp$, pos-1) : EndIf
		pos.l = FindString(tmp$, ".", pos + 1)
		If pos< = posseg : Break : EndIf
	ForEver
	ProcedureReturn r$
EndProcedure

Debug noext(".I.have.too.many.points.txt")
Debug noext("I have only one point.txt")
Debug noext(".htaccess")
PB 6.21 beta, PureVision User
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 »

ROFL.
Jetzt hast du uns bei den Eiern. ^^ (german metaphor. I don't know what it is in english.)

Very good objection!
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 »

Kaeru Gaman wrote:

Code: Select all

filename$ = "Review 2009.09.23.doc"
Debug StringField(filename$,1,".")
pwned :!:
Joakim Christiansen wrote:Can I get a cookie now?
NOPE!
:lol:
This is a fun thread :)
I like logic, hence I dislike humans but love computers.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: Remove the extension part of a file.

Post by utopiomania »

Code: Select all

PathRemoveExtension_(@file)
@breez4me, does this leave the file on disc without the extension part?

If so, thanks for the tip :)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: Remove the extension part of a file.

Post by Kale »

All these procedures are too big. Text manipulation such as this is what regular expressions where invented for. :wink:

This is cross-platform:

Code: Select all

Procedure.s RemoveExtension(Filename.s)
	CreateRegularExpression(0, "(?<!\A)\.[\w]+\Z")
	ProcedureReturn ReplaceRegularExpression(0, Filename, "")
EndProcedure


Debug RemoveExtension(".htaccess")
Debug RemoveExtension("Filename.exe")
Debug RemoveExtension("Filename.html")
Debug RemoveExtension("Filename.config")
Debug RemoveExtension("File.Name.jpeg")
Debug RemoveExtension("I have only one point.txt")
Debug RemoveExtension(".I.have.too.many.points.txt")
--Kale

Image
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 »

It is just a short memory leak @Kale ^^

My recommendation.

Code: Select all

Procedure.s RemoveExtension(sFilename.s)
   Static idRegEx.i
   
   ; Object recycling.
   If Not (idRegEx)
      idRegEx = CreateRegularExpression(#PB_Any , "(?<!\A)\.[\w]+\Z")
      If Not (idRegEx) 
         ProcedureReturn sFilename
      EndIf
   EndIf
   
   ProcedureReturn ReplaceRegularExpression(idRegEx, sFilename, "")
EndProcedure


Debug RemoveExtension(".htaccess")
Debug RemoveExtension("Filename.exe")
Debug RemoveExtension("Filename.html")
Debug RemoveExtension("Filename.config")
Debug RemoveExtension("File.Name.jpeg")
Debug RemoveExtension("I have only one point.txt")
Debug RemoveExtension(".I.have.too.many.points.txt")
Regards
Wolf
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Remove the extension part of a file.

Post by SFSxOI »

OK, I got a stupid question. Why can't you just rename the file to remove the extension?

Code: Select all

RenameFile("C:\test.txt", "C:\test")

works fine


The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
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 »

Kale wrote:All these procedures are too big. Text manipulation such as this is what regular expressions where invented for. :wink:
Image
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 »

Kale wrote:All these procedures are too big. Text manipulation such as this is what regular expressions where invented for. :wink:
Apropos "too big": How big will be your resulting Exefile if you use reg. expressions instead of a small procedure which do the same job? ;)
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
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 »

SFSxOI wrote:OK, I got a stupid question. Why can't you just rename the file to remove the extension?

Code: Select all

RenameFile("C:\test.txt", "C:\test")

works fine
@SFSxO1: You wouldn't know what to name it to until you had removed the extension. :wink:

I believe the thread title was meant to read "Remove the extension part of a filename."
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: Remove the extension part of a file.

Post by Kale »

Kaeru Gaman wrote:
Kale wrote:All these procedures are too big. Text manipulation such as this is what regular expressions where invented for. :wink:
xkcd.com image
Jealous? :wink:
Hroudtwolf wrote:It is just a short memory leak @Kale ^^
Explain. I don't understand why you've coded your procedure like that.
--Kale

Image
Post Reply