GetFilePart() excluding extension

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

GetFilePart() excluding extension

Post by IdeasVacuum »

I think GetFilePart() should return only the name of the file, excluding the file extension. If we need the extension, we can use GetExtensionPart(), so apart from greater ease of use, it is more logical.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: GetFilePart() excluding extension

Post by skywalk »

I disagree. I did a random sample of my use of GetFilePart() and in all cases I used the entire string returned.
This would force me to append GetExtensionPart() in every case :(
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: GetFilePart() excluding extension

Post by IdeasVacuum »

I just did a search on my files - about 90% do use the entire string returned, so you are right, it's much more likely that the file name + extension is required. When it isn't required though, the solution can be inelegant.

So, how about an optional flag instead?

Filename$ = GetFilePart(FullPathName$) ;Returns file name including extension
Filename$ = GetFilePart(FullPathName$,#PB_NoExt) ;Returns file name excluding extension
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: GetFilePart() excluding extension

Post by J. Baker »

EDIT: Better code below.
Last edited by J. Baker on Fri Jan 25, 2013 5:04 am, edited 1 time in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: GetFilePart() excluding extension

Post by skywalk »

Hi J.Baker,
What about "myfile.something.txt"?
Better to search for '.' from the end of the file$.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: GetFilePart() excluding extension

Post by J. Baker »

Got you covered. ;)

EDIT: Better code below.
Last edited by J. Baker on Fri Jan 25, 2013 5:04 am, edited 1 time in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: GetFilePart() excluding extension

Post by J. Baker »

Or better yet...

Code: Select all

String$ = "/Users/jbaker/Desktop/myfile.something.txt"

Procedure.s GetFileName(FileName.s)
  
  FilePart$ = GetFilePart(FileName.s)
  
   Length = Len(FilePart$)
   
    Point = FindString(ReverseString(FilePart$), ".")
  
   ReturnName$ = Left(FilePart$, Length - Point)
  
  ProcedureReturn ReturnName$
  
EndProcedure

Debug GetFileName(String$)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: GetFilePart() excluding extension

Post by Shield »

It still doesn't work correctly. :wink:
Edit: your edited version does.


Keep it simple:

Code: Select all


path.s = "C:\foo\bar.txt"


Procedure.s GetFileName(filename.s)
	Protected length.i
	
	filename = GetFilePart(filename)
	length = Len(GetExtensionPart(filename))
	If Not length
		ProcedureReturn filename
	EndIf
	
	ProcedureReturn Left(filename, Len(filename) - length - 1)
EndProcedure


Debug GetFileName(path)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: GetFilePart() excluding extension

Post by J. Baker »

It's simple. ;)

Code: Select all

String$ = "/Users/jbaker/Desktop/myfile.something.txt"

Procedure.s GetFileName(FileName.s)
  
  ProcedureReturn Left(GetFilePart(FileName.s), Len(GetFilePart(FileName.s)) - FindString(ReverseString(GetFilePart(FileName.s)), "."))
  
EndProcedure

Debug GetFileName(String$)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: GetFilePart() excluding extension

Post by IdeasVacuum »

So, I think there is a case for the simplicity of that flag :)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: GetFilePart() excluding extension

Post by Shield »

Yeah well, then...

Code: Select all

Procedure.s GetFileName(filename.s)
   ProcedureReturn Left(GetFilePart(filename), Len(GetFilePart(filename)) - Len(GetExtensionPart(GetFilePart(filename))) - 1)
EndProcedure
But that's not the point. I tried to keep it simple instruction-wise.
Using FindString and ReverseString is kind of overkill and totally unnecessary (and not really fast either).

But of course it doesn't matter for this procedure.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: GetFilePart() excluding extension

Post by skywalk »

Hi Shield,
Don't discount FindStringRev() :wink:
Your last post fails in this test.

Code: Select all

Debug GetFileName("c:\1\2\my complex file")
Debug GetFileName("c:\1\2\ m.y. complex file ")
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: GetFilePart() excluding extension

Post by MachineCode »

IdeasVacuum wrote:I think GetFilePart() should return only the name of the file, excluding the file extension. If we need the extension, we can use GetExtensionPart(), so apart from greater ease of use, it is more logical.
I would argue that it's just as easy for you to crop the extension off. :) This is one of those requests where either way is just as easy as the other.

Anyway, for Windows users only (and passes skywalk's test):

Code: Select all

f$="C:\Program Files\My App.exe"
PathRemoveExtension_(f$)
Debug f$

f$="c:\1\2\ m.y. complex file "
PathRemoveExtension_(f$)
Debug f$
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: GetFilePart() excluding extension

Post by LuCiFeR[SD] »

ok, perhaps I will still post... Not ready to call it a day yet :)

Code: Select all

Procedure.s Getfilename(filename.s)
  While FindString(filename.s,".")
    filename.s= Left(GetFilePart(filename.s), Len(GetFilePart(filename.s)) - Len(GetExtensionPart(GetFilePart(filename.s))) - 1)
  Wend
  ProcedureReturn filename.s
EndProcedure

filename.s="I Know lets make an annoying string for this to parse.a.b.c.airbuscuit.cheese.exe"
Debug "original filename with extension:   " + filename.s
Debug "Filename with extension removed:    "+Getfilename(filename.s)
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: GetFilePart() excluding extension

Post by Thorsten1867 »

Code: Select all

Procedure.s GetFileNamePart(File.s) ; Filename without extension
  Protected Ext.s = GetExtensionPart(File)
  File = GetFilePart(File)
  If Ext And File
    ProcedureReturn Left(File, Len(File)-Len(Ext)-1)
  Else
    ProcedureReturn File
  EndIf
EndProcedure
Here is my example.
("c:\1\2\ m.y. complex file " = valid file name ???)
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
Post Reply