Page 1 of 2

GetFilepart bug

Posted: Mon Nov 27, 2023 8:29 pm
by williamvanhoecke
Debug Len(GetFilePart("C:\PureBasic\PB.exe"))
Debug Len(GetFilePart("C:\PureBasic\PB.exe",#PB_FileSystem_NoExtension))

Debug Len(GetFilePart("C:\PureBasic\.exe"))
Debug Len(GetFilePart("C:\PureBasic\.exe",#PB_FileSystem_NoExtension))

results in
6
2
4
4

should be
6
2
4
0



// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)

Re: GetFilepart bug

Posted: Mon Nov 27, 2023 8:43 pm
by jacdelad
Not a bug, the prefix can't be empty. This file has no extension, the whole name is the prefix.

Re: GetFilepart bug

Posted: Mon Nov 27, 2023 10:08 pm
by BarryG
But there is no file part? And the manual doesn't say anything about a prefix requirement.

Code: Select all

file$=".exe" ; No file part specified (just an extension).
Debug GetFilePart(file$,#PB_FileSystem_NoExtension) ; ".exe" - why? This is not the file part.

Re: GetFilepart bug

Posted: Mon Nov 27, 2023 10:21 pm
by jacdelad
Interesting question.

Re: GetFilepart bug

Posted: Mon Nov 27, 2023 10:33 pm
by BarryG
Workaround:

Code: Select all

Procedure.s GetFilePartEx(file$,flags)
  file$=GetFilePart(file$)
  If Left(file$,1)="." And CountString(file$,".")=1
    file$=""
  EndIf
  ProcedureReturn GetFilePart(file$,flags)
EndProcedure

Debug GetFilePart("C:\PureBasic.exe",#PB_FileSystem_NoExtension) ; "PureBasic" - Right.
Debug GetFilePart("C:\.exe",#PB_FileSystem_NoExtension) ; ".exe" - Wrong.

Debug GetFilePartEx("C:\PureBasic.exe",#PB_FileSystem_NoExtension) ; "PureBasic" - Right.
Debug GetFilePartEx("C:\.exe",#PB_FileSystem_NoExtension) ; "" - Right.

Re: GetFilepart bug

Posted: Mon Nov 27, 2023 10:40 pm
by normeus
".exe" is a valid filename, just like ".htaccess" they just don't have extensions.

Code: Select all

Debug Len(GetFilePart("C:\PureBasic\.exe"))    ; 4 character filename
Debug CheckFilename("C:\PureBasic\.exe")      ; returns 0 if a valid filename

[EDIT]
another example

Code: Select all

Debug GetFilePart("C:\.ForgotToCheckVarForFilename.lechat.thecat.diekatze.ilgatto",#PB_FileSystem_NoExtension)
Debug GetExtensionPart("C:\.ForgotToCheckVarForFilename.lechat.thecat.diekatze.ilgatto")

Norm.

Re: GetFilepart bug

Posted: Mon Nov 27, 2023 11:31 pm
by williamvanhoecke
I have this software where customer enter a raw 'name' for there file and the extention is then automatically added.
I then check is a 'name' was entered and if it is valid with CheckFilename(name).
So i get things like "C:\user\benjamin\customerfiles\.lti"

Realy I don't care if ".lti" is a valid filename or not, since ".lti" is the extention the len(getfilepart(".lti",#PB_FileSystem_NoExtension)) should give 0

Since I know there IS an extention my workaround is simple
if Len(StringField(GetFilePart(filename),1,".")) = 0 --> no filename here
:)

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 12:02 am
by Demivec
No bug in the original post.

https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file

Relevant quotes from the above link:
Use a period to separate the base file name from the extension in the name of a directory or file.
However, it is acceptable to specify a period as the first character of a name. For example, ".temp".
Since a period is used as a separator between a filename and an extension, a period as the first character is part of the filename since there is no filename yet to mark a separation from for an extension to occur.

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 12:11 am
by williamvanhoecke
@normeus
normeus wrote: Mon Nov 27, 2023 10:40 pm ".exe" is a valid filename, just like ".htaccess" they just don't have extensions.
That may correct but then how do you know if you have "an empty filename with an extention" or "A weired legal filename with no extention" ??
Unless your running a server, I think its not a good practice to start filnames with a dot, makes as you see things complicated.

As Einstein said "All solutions must be simple...." wouldend it be great, left from the dot is the filname, right from the dot is the extention :D

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 12:27 am
by williamvanhoecke
So
I read all the replies, they are all correct.
There is no bug, for the system,
since filename ".lti" is a legal filename with no extention.

but from the users or programmers point of view the GetFilePart(filename,#PB_FileSystem_NoExtension) can NEVER work correctly .
since filename ".lti" can be
a legal filename with no extention
an empty (illegal) filename with an extention (as in my case)

Conclusion, nothing needs to be fixed, just work around it. :)

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 3:58 am
by BarryG
williamvanhoecke wrote: Tue Nov 28, 2023 12:11 amhow do you know if you have "an empty filename with an extention" or "A weired legal filename with no extention" ??
In such a case, you can't ever know. It's a problem without a solution.

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 11:03 am
by Little John
williamvanhoecke wrote: Tue Nov 28, 2023 12:11 am @normeus
normeus wrote: Mon Nov 27, 2023 10:40 pm ".exe" is a valid filename, just like ".htaccess" they just don't have extensions.
That may correct but then how do you know if you have "an empty filename with an extention" or "A weired legal filename with no extention" ??
Since e.g. ".exe" and ".htaccess" are valid file names, an empty filename with an extension simply does not exist. That's by definition, and it doesn't matter whether or not someone likes it.
williamvanhoecke wrote: Tue Nov 28, 2023 12:11 am Unless your running a server, I think its not a good practice to start filnames with a dot, makes as you see things complicated.
What is good practice and what is not is a different question. This is not a bug in PureBasic.

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 11:08 am
by NicTheQuick
On Linux files starting with a dot are treated as hidden files. The name `.config` is basically a file named `config`, but hidden.

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 11:20 am
by jacdelad
So, after a long discussion we're back to what I said in my first post.
I guess GetFileExtension() just delivers the part after the last ".", regardless of what is in front of it.

Re: GetFilepart bug

Posted: Tue Nov 28, 2023 11:30 am
by Little John
jacdelad wrote: Tue Nov 28, 2023 11:20 am So, after a long discussion we're back to what I said in my first post.
Yep. Image
jacdelad wrote: Tue Nov 28, 2023 11:20 am I guess GetFileExtension() just delivers the part after the last ".", regardless of what is in front of it.
There is no GetFileExtension() function in PureBasic.
You probably mean GetExtensionPart(). As far as I can see, its behaviour is consistent with GetFilePart() (PB 6.03 LTS on Windows).