Re: Remove the extension part of a file.
Posted: Wed Sep 23, 2009 11:57 am
@Wolf
producing uncleanable garbage is no alternative...
producing uncleanable garbage is no alternative...

http://www.purebasic.com
https://www.purebasic.fr/english/
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")
helpy wrote:and how will your procedures handle ".htaccess"?
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")
Kaeru Gaman wrote:pwnedCode: Select all
filename$ = "Review 2009.09.23.doc" Debug StringField(filename$,1,".")
NOPE!Joakim Christiansen wrote:Can I get a cookie now?
Code: Select all
PathRemoveExtension_(@file)
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")
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")
Code: Select all
RenameFile("C:\test.txt", "C:\test")
works fine
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?Kale wrote:All these procedures are too big. Text manipulation such as this is what regular expressions where invented for.
@SFSxO1: You wouldn't know what to name it to until you had removed the extension.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
Jealous?Kaeru Gaman wrote:xkcd.com imageKale wrote:All these procedures are too big. Text manipulation such as this is what regular expressions where invented for.
Explain. I don't understand why you've coded your procedure like that.Hroudtwolf wrote:It is just a short memory leak @Kale ^^