GetFilepart bug

Just starting out? Need help? Post your questions and find answers here.
williamvanhoecke
User
User
Posts: 65
Joined: Wed Jun 07, 2017 10:13 pm

GetFilepart bug

Post 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)
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: GetFilepart bug

Post by jacdelad »

Not a bug, the prefix can't be empty. This file has no extension, the whole name is the prefix.
Last edited by jacdelad on Tue Nov 28, 2023 11:20 am, edited 1 time in total.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4127
Joined: Thu Apr 18, 2019 8:17 am

Re: GetFilepart bug

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: GetFilepart bug

Post by jacdelad »

Interesting question.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4127
Joined: Thu Apr 18, 2019 8:17 am

Re: GetFilepart bug

Post 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.
normeus
Enthusiast
Enthusiast
Posts: 470
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: GetFilepart bug

Post 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.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
williamvanhoecke
User
User
Posts: 65
Joined: Wed Jun 07, 2017 10:13 pm

Re: GetFilepart bug

Post 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
:)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: GetFilepart bug

Post 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.
williamvanhoecke
User
User
Posts: 65
Joined: Wed Jun 07, 2017 10:13 pm

Re: GetFilepart bug

Post 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
williamvanhoecke
User
User
Posts: 65
Joined: Wed Jun 07, 2017 10:13 pm

Re: GetFilepart bug

Post 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. :)
BarryG
Addict
Addict
Posts: 4127
Joined: Thu Apr 18, 2019 8:17 am

Re: GetFilepart bug

Post 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.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetFilepart bug

Post 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.
Last edited by Little John on Tue Nov 28, 2023 11:08 am, edited 1 time in total.
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: GetFilepart bug

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: GetFilepart bug

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: GetFilepart bug

Post 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).
Post Reply