GetFilePart() excluding extension

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
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 »

skywalk wrote: Your last post fails in this test.
Nice find. The short one is crap anyway. :P
However my first one fails the tests partially too:

Code: Select all

Debug GetExtensionPart("foo. bar")
But that's because GetExtensionPart() fails the test. :)
At least it doesn't have the same behavior as PathRemoveExtension_(),
which I guess should be correct since it's an API specifically made for that.
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 »

Shield wrote:But that's because GetExtensionPart() fails the test.
At least it doesn't have the same behavior as PathRemoveExtension_(),
which I guess should be correct since it's an API specifically made for that.
What PB version are you running? This works in all cases on v5.
GetExtensionPart() does not fail.
But PathRemoveExtension_() fails with any spaces in Extension.

Code: Select all

Procedure.s FL_GetFilePartNoExt(FilePathName$)
  Protected.s gfp$ = GetFilePart(FilePathName$)
  Protected.i lenfp = Len(GetExtensionPart(gfp$))
  If lenfp
    gfp$ = Left(gfp$, Len(gfp$) - lenfp - 1)
  EndIf
  ProcedureReturn gfp$
EndProcedure

f$ = "foo. _bar"
Debug "; Use  = " + f$
Debug "; Ext  = " + GetExtensionPart(f$)
Debug "; File = " + FL_GetFilePartNoExt(f$)

f$="c:\1\2\ m.y. _complex file "
Debug "; Use  = " + f$
Debug "; Ext  = " + GetExtensionPart(f$)
Debug "; File = " + FL_GetFilePartNoExt(f$)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
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 »

Just updated to the newest beta.

Code: Select all

Debug GetExtensionPart("foo. bar")

file.s = "foo. bar"
PathRemoveExtension_(@file)
Debug file
GetExtensionPart() returns " bar" (space included), whereas PathRemoveExtension_() returns the original string,
because there is no extension to be removed.

I don't know if there any specifications on this or if this is just pure convention.
Fact is, that GetExtensionPart allows spaces within extensions and PathRemoveExtension_() does not.
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
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: GetFilePart() excluding extension

Post by BorisTheOld »

Being able to extract just the name portion of a file name or path is actually very useful. I've used this feature in other languages.

All that PB needs is one new command, "GetNamePart", since the other variations already exist:

x$ = GetNamePart(y$)
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: GetFilePart() excluding extension

Post by MachineCode »

Shield wrote:GetExtensionPart allows spaces within extensions and PathRemoveExtension_() does not.
Windows (and DOS) have never allowed spaces before or after a filename, and I guess that includes extensions; which would explain why PathRemoveExtension_() removes them.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: GetFilePart() excluding extension

Post by moogle »

I agree it should be GetNamePart() if we are going by the naming standard of GetExtensionPart(), GetFilePart() and GetPathPart()


OR they should be like this

Path$="C:\Windows\System32\explorer.exe"

NEW Function -> GetFilename() or GetFilenamePart() returns "explorer.exe"
GetFilePart() returns "explorer"
GetExtensionPart() returns "exe"


Makes a bit more sense? The filename is the whole thing, file part of the name is the first bit and of course the extension needs no explanation :)
Image
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: GetFilePart() excluding extension

Post by MachineCode »

An extension is actually part of the filename. Always has been, always will be. Here's proof:

Code: Select all

r=ReadFile(0,"C:\Program Files\Internet Explorer\iexplore")
Debug r ; Returns 0. Why? Because no such file exists!
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: GetFilePart() excluding extension

Post by sec »

MachineCode wrote:An extension is actually part of the filename. Always has been, always will be. Here's proof:

Code: Select all

r=ReadFile(0,"C:\Program Files\Internet Explorer\iexplore")
Debug r ; Returns 0. Why? Because no such file exists!
At low level of filesystem has no extension. At high end-user has minetype
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: GetFilePart() excluding extension

Post by moogle »

MachineCode wrote:An extension is actually part of the filename. Always has been, always will be. Here's proof:

Code: Select all

r=ReadFile(0,"C:\Program Files\Internet Explorer\iexplore")
Debug r ; Returns 0. Why? Because no such file exists!
I never said it wasn't but GetFilePart() makes sense to get the name without the extension ("explorer" instead of "explorer.exe") if we already have a GetExtensionPart() function and the GetFilenamePart() is a combination of both those functions.
Image
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: GetFilePart() excluding extension

Post by sec »

-1

Thanks.
wolf_II
New User
New User
Posts: 1
Joined: Mon Aug 05, 2013 9:17 pm

Re: GetFilePart() excluding extension

Post by wolf_II »

I tested FL_GetFilePartNoExt(FilePathName$) by skywalk (Posted: Fri Jan 25, 2013 4:36 pm) like this:

I tested with a file that has NO extension, but its name starts with a dot:
In the case of "C:\path\.test" => "". IMHO, this doesn't look right.
So I added a second condition (length of extension with dot < length of filename) into the code:
I also changed the names of some variables to my taste.

Code: Select all

;-------------------------------------------------------------------------------
Procedure.s GetNamePart(File.s) ; get filename without extension
;-------------------------------------------------------------------------------
    Protected.s f = GetFilePart(File)
    Protected lx = Len(GetExtensionPart(File))
    If lx And lx + 1 < Len(f)
        ProcedureReturn Left(f, Len(f) - lx - 1)
    EndIf
    ProcedureReturn f
    
EndProcedure
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: GetFilePart() excluding extension

Post by davido »

@wolf_II, Welcome!

Please see Manual: GetFilePart()

GetFilePart()

Syntax

Filename$ = GetFilePart(FullPathName$ [, Mode])
Description

Retrieves the file part of a full path.
Parameters

FullPathName$ The full path to get the filename from.
Mode (optional) It can be one of the following values:
#PB_FileSystem_NoExtension: Get the filename without its extension (if any).



Return value

Returns the file name. For example, if the full path is "C:\PureBasic\PB.exe", the result will be "PB.exe".
DE AA EB
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: GetFilePart() excluding extension

Post by Fred »

It's only since 5.20 which is still not released, so you can't really tell him to read the manual here ;)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: GetFilePart() excluding extension

Post by davido »

@wolf_II,

Apologies; Fred's always right! :oops:
DE AA EB
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: GetFilePart() excluding extension

Post by PB »

-1 to the original post. Crop off the extension yourself.
It's part of the filename, and everybody knows that.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply