Page 2 of 4

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 11:57 am
by Kaeru Gaman
@Wolf
producing uncleanable garbage is no alternative... ;)

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 12:18 pm
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

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 12:36 pm
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")


Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 1:53 pm
by helpy
and how will your procedures handle ".htaccess" ;-) ?

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 1:57 pm
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")

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 2:03 pm
by Hroudtwolf
ROFL.
Jetzt hast du uns bei den Eiern. ^^ (german metaphor. I don't know what it is in english.)

Very good objection!

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 3:42 pm
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 :)

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 7:13 pm
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 :)

Re: Remove the extension part of a file.

Posted: Wed Sep 23, 2009 11:22 pm
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")

Re: Remove the extension part of a file.

Posted: Thu Sep 24, 2009 5:01 am
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

Re: Remove the extension part of a file.

Posted: Thu Sep 24, 2009 8:42 am
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



Re: Remove the extension part of a file.

Posted: Thu Sep 24, 2009 10:32 am
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

Re: Remove the extension part of a file.

Posted: Thu Sep 24, 2009 11:01 am
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? ;)

Re: Remove the extension part of a file.

Posted: Thu Sep 24, 2009 2:36 pm
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."

Re: Remove the extension part of a file.

Posted: Thu Sep 24, 2009 5:48 pm
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.