Page 1 of 3

GetFilePart() excluding extension

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

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 3:47 am
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 :(

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 4:17 am
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

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 4:48 am
by J. Baker
EDIT: Better code below.

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 4:51 am
by skywalk
Hi J.Baker,
What about "myfile.something.txt"?
Better to search for '.' from the end of the file$.

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 4:54 am
by J. Baker
Got you covered. ;)

EDIT: Better code below.

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 5:03 am
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$)

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 5:06 am
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)

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 5:19 am
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$)

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 5:51 am
by IdeasVacuum
So, I think there is a case for the simplicity of that flag :)

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 6:17 am
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.

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 6:56 am
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 ")

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 7:41 am
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$

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 1:38 pm
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)

Re: GetFilePart() excluding extension

Posted: Fri Jan 25, 2013 3:24 pm
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 ???)