Page 2 of 3

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 3:51 pm
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.

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 4:36 pm
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$)

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 4:57 pm
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.

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 6:16 pm
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$)

Re: GetFilePart() excluding extension

Posted: Sat Jan 26, 2013 4:43 am
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.

Re: GetFilePart() excluding extension

Posted: Sun Jan 27, 2013 1:54 am
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 :)

Re: GetFilePart() excluding extension

Posted: Sun Jan 27, 2013 6:19 am
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!

Re: GetFilePart() excluding extension

Posted: Sun Jan 27, 2013 6:29 am
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

Re: GetFilePart() excluding extension

Posted: Sun Jan 27, 2013 12:09 pm
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.

Re: GetFilePart() excluding extension

Posted: Sun Jan 27, 2013 1:13 pm
by sec
-1

Thanks.

Re: GetFilePart() excluding extension

Posted: Mon Aug 05, 2013 9:58 pm
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

Re: GetFilePart() excluding extension

Posted: Mon Aug 05, 2013 10:22 pm
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".

Re: GetFilePart() excluding extension

Posted: Tue Aug 06, 2013 10:38 am
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 ;)

Re: GetFilePart() excluding extension

Posted: Tue Aug 06, 2013 11:20 am
by davido
@wolf_II,

Apologies; Fred's always right! :oops:

Re: GetFilePart() excluding extension

Posted: Tue Aug 06, 2013 11:42 am
by PB
-1 to the original post. Crop off the extension yourself.
It's part of the filename, and everybody knows that.