FileSize() with a wild card?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: FileSize() with a wild card?

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: FileSize() with a wild card?

Post by jassing »

Removed due to inaccurate information.
Last edited by jassing on Fri Feb 18, 2011 7:42 am, edited 1 time in total.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: FileSize() with a wild card?

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: FileSize() with a wild card?

Post 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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: FileSize() with a wild card?

Post 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).
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: FileSize() with a wild card?

Post 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.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: FileSize() with a wild card?

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: FileSize() with a wild card?

Post 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.
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: FileSize() with a wild card?

Post 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. :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: FileSize() with a wild card?

Post 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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: FileSize() with a wild card?

Post 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.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Demivec
Addict
Addict
Posts: 4258
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: FileSize() with a wild card?

Post 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).
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: FileSize() with a wild card?

Post 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. :wink:
http://www.purebasic.fr/english/viewtop ... 87#p347087
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: FileSize() with a wild card?

Post 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 :mrgreen:


greetings ~ Vera
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: FileSize() with a wild card?

Post by C64 »

Vera, you do know a directory is a folder, right? So do I really need to point it out in the manual?
Post Reply