Page 2 of 2
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 1:33 am
by jassing
c4s wrote:jassing wrote:PB wrote:Filesize checks the size of the specified file. An asterisk is not specifying a file. No bug.
The docs says to use it to check for the existence of a file... a file. So an invalid file should return -1
...Also it's often a tip here for newbies:
Pseudo conversation wrote:A: How can I find out if a file exists?
B: Use FileSize() like this:
Code: Select all
Procedure FileExists(File.s)
Protected Result
If FileSize(File) >= 0
Result = #True
Else
Result = #False ; Doesn't exist or a folder
EndIf
ProcedureReturn Result
EndProcedure
A: Ah great, I really didn't know I could use this function for it...thanks.

That function wil return TRUE for "*.jpg" because it allows wildcards....
You must also check to ensure that the file name is valid.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 4:58 am
by jassing
Removed due to inaccurate information.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 7:36 am
by C64
jassing wrote:FileSize() can't be used for folders either
The manual clearly states that FileSize() can be used with files and folders. It is YOU who is not using the command correctly, as has been pointed out with the GIGO principle. Please stop spreading false statements.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 7:42 am
by jassing
C64 wrote:jassing wrote:FileSize() can't be used for folders either
The manual clearly states that FileSize() can be used with files and folders. It is YOU who is not using the command correctly, as has been pointed out with the GIGO principle. Please stop spreading false statements.
Sorry.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 10:37 am
by c4s
jassing wrote:That function wil return TRUE for "*.jpg" because it allows wildcards....
You must also check to ensure that the file name is valid.
Yes, that was the whole point of this exemplary conversation I posted, here some real examples:
http://www.purebasic.fr/english/viewtop ... 13&t=41302
http://www.purebasic.fr/english/viewtop ... =13&t=4876
http://www.purebasic.fr/english/viewtop ... 13&t=40254
What I want to say is that first checking the filename itself isn't mentioned anywhere - not even in the "F.A.Q for PureBasic" (second link).
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 12:09 pm
by Trond
It is my opinion that FileSize() with a wildcard should return -1
as it is currently documented. However:
1. Contrary to
unpopular belief, I am not the master of the universe, so this is a matter of opinion.
2. The issue was asked about and replied to in an unconstructive manner.
3. It is a very minor issue, because the input is invalid.
I want to try some constructive thinking:
A. What could be a viable solution for the affected?
B. How to know whether this is severe enough to be reported as a bug?
First I'll have a go with A. A procedure can be written to handle invalid characters in the file name in the following way:
Code: Select all
Procedure FileExists(FileAndPath.s)
If CheckFilename(GetFilePart(FileAndPath))
ProcedureReturn FileSize(FileAndPath)
Else
ProcedureReturn -1
EndIf
EndProcedure
This may be a solution, however, there can still be wildcards in the path. If that is expected, an additional check is needed:
Code: Select all
Procedure FileExists(FileAndPath.s)
If CheckFilename(GetFilePart(FileAndPath))
If FindString(FileAndPath, "*", 1)
ProcedureReturn FileSize(FileAndPath)
EndIf
EndIf
ProcedureReturn -1
EndProcedure
A third alternative is to use an API function. However, after googling I found that the most common way of doing this (the only way I found, actually) has the same issue with wildcards.
As for B. I think a feature request to either update the manual or change the function to return -1 on wildcards is in place.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 12:58 pm
by C64
Trond wrote:What could be a viable solution for the affected?
In all seriousness and with no malice or sarcasm intended: nothing. The GIGO acronym is all that matters here, because the manual is clear enough: a file must be specified. A file with a wildcard is not a file. If it is, please zip me a file with an asterisk in its name so I can see it.
Let's stop and take stock of what you're asking: a new PureBasic user has come in, used a command incorrectly, and now we're talking about changing the command and adding a (minor) performance overhead to it, just to save the newbie from himself? Isn't that going to set a precedent for all commands?
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 1:24 pm
by Trond
If it is, please zip me a file with an asterisk in its name so I can see it.
Here you go:
http://www.2shared.com/file/hm_t-utp/stars.html
Many Linux/Unix filesystems support * in filenames, as does FAT32 according to Wikipedia.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 1:44 pm
by C64
All WinZip does is throw up an error and then show me an empty archive ("Total 0 files, 0 bytes"). Anyway, I get where you're coming from.

Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 2:48 pm
by Trond
Anyway, there is no law of nature that says garbage-in-garbage-out is a good way of designing things. I prefer garbage-in-error-out.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 4:31 pm
by c4s
Trond wrote:Anyway, there is no law of nature that says garbage-in-garbage-out is a good way of designing things. I prefer garbage-in-error-out.
Yep, me too.
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 4:49 pm
by Demivec
Trond wrote:It is my opinion that FileSize() with a wildcard should return -1 as it is currently documented.
I have the contrary opinion that FileSize() used with a wildcard should be allowed (as it does presently) and currently functions as would be expected from the documentation. The results are always based on a
single file or folder. If wildcards are used then it should be understood that any
one of the files or folders that match will be used to determine the results.
When FileSize() is used to determine whether a file exists and the filename used contains a wildcard then the result would be understood to mean whether any
single file matching the specification exists.
Even though I have a contrary opinion I do agree that the documentation should be updated (slightly) to mention that if any wildcard is used in the filename that the results will be based on
one of any of the files or directiories that match the filename (with wildcards).
Re: FileSize() with a wild card?
Posted: Fri Feb 18, 2011 5:39 pm
by skywalk
Wow, I thought by v4.51 everyone would agree on FileSize()
I have to agree with Demivec.
Respect '*' Wildcards, but add to the documentation please.
I use the current behavior to check for empty directories.
Very clean and simple.
http://www.purebasic.fr/english/viewtop ... 87#p347087
Re: FileSize() with a wild card?
Posted: Sat Feb 19, 2011 5:11 pm
by Vera
C64 wrote:jassing wrote:FileSize() can't be used for folders either
The manual clearly states that FileSize() can be used with files and folders. ... Please stop spreading false statements.
HELP wrote:FileSize()
Syntax
Result.q = FileSize(Filename$)
Description
Returns the size of the specified file.
Special 'Result' values: -1: File not found.
-2: File is a directory.
Supported OS
All
Please mark where you read
'FileSize() can be used with folders' or
stop spreading false statements !
C64 wrote:All WinZip does is throw up an error and then show me an empty archive ("Total 0 files, 0 bytes").
Ain't this enough evident to better move to a proper zipper ?
~~~~~~~~~~~~
As for the specific work of the
filesize() command - it's works very precise on Linux
(and I presume the same for MAC):
Code: Select all
Debug FileSize(#PB_Compiler_Home + "*") ; -1
Debug FileSize(#PB_Compiler_Home + "*.*") ; -1
Debug FileSize(#PB_Compiler_Home + "sd*") ; -1
Debug FileSize(#PB_Compiler_Home + "sdk") ; -2
Debug FileSize(#PB_Compiler_Home + "*.png") ; value if exists
Debug FileSize(#PB_Compiler_Home + "~.png") ; value if exists
Debug FileSize(#PB_Compiler_Home + "a*.png") ; -1 although ai.png might exist
Debug FileSize(#PB_Compiler_Home + "ai.*") ; -1 although ai.png might exist
So in this case the manual might state
a - a warning not to use the asterix on WIN or
b - point out the hidden feature
greetings ~ Vera
Re: FileSize() with a wild card?
Posted: Sat Feb 19, 2011 5:55 pm
by C64
Vera, you do know a directory is a folder, right? So do I really need to point it out in the manual?