GetFilePart() excluding extension
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
GetFilePart() excluding extension
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.
If it sounds simple, you have not grasped the complexity.
Re: GetFilePart() excluding extension
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
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
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: GetFilePart() excluding extension
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
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.
If it sounds simple, you have not grasped the complexity.
Re: GetFilePart() excluding extension
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.
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.
Re: GetFilePart() excluding extension
Hi J.Baker,
What about "myfile.something.txt"?
Better to search for '.' from the end of the file$.
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
Re: GetFilePart() excluding extension
Got you covered. 
EDIT: Better code below.

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.
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.
Re: GetFilePart() excluding extension
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.
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.
Re: GetFilePart() excluding extension
It still doesn't work correctly. 
Edit: your edited version does.
Keep it simple:

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)
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
Re: GetFilePart() excluding extension
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.
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.
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: GetFilePart() excluding extension
So, I think there is a case for the simplicity of that flag 

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: GetFilePart() excluding extension
Yeah well, then...
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.
Code: Select all
Procedure.s GetFileName(filename.s)
ProcedureReturn Left(GetFilePart(filename), Len(GetFilePart(filename)) - Len(GetExtensionPart(GetFilePart(filename))) - 1)
EndProcedure
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.
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
Re: GetFilePart() excluding extension
Hi Shield,
Don't discount FindStringRev()
Your last post fails in this test.
Don't discount FindStringRev()

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
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: GetFilePart() excluding extension
I would argue that it's just as easy for you to crop the extension off.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.

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!
PureBasic: Born in 1998 and still going strong to this very day!
-
- 666
- Posts: 1033
- Joined: Mon Sep 01, 2003 2:33 pm
Re: GetFilePart() excluding extension
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)
- Thorsten1867
- Addict
- Posts: 1372
- Joined: Wed Aug 24, 2005 4:02 pm
- Location: Germany
Re: GetFilePart() excluding extension
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
("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]
Download of PureBasic - Modules
Download of PureBasic - Programs
[Windows 11 x64] [PB V5.7x]