Page 1 of 2

FileSize() with a wild card?

Posted: Wed Feb 16, 2011 5:52 pm
by jassing
Removed due to inaccurate information.

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 6:30 pm
by kenmo
Seems to return the filesize of the first file that matches the wildcard pattern........

I don't know if this is considered a bug or not!

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 6:52 pm
by Demivec
jassing wrote:Shouldn't FileSize() return -1 if the file is, for example, "c:\temp\*.jpg" ?
Why would you think the filename cannot be specified with a wildcard?

If you don't want a wildcard in the filename you should filter it out before using FileSize().

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 7:19 pm
by jassing
Removed due to inaccurate information.

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 8:12 pm
by c4s
I would say it's a bug because that behavior isn't stated in the help file:
Help file wrote:Returns the size of the specified file.
Also my programs don't expect that behavior as I didn't know it until I read it here. Either the file exists or it doesn't. I seriously don't want to know what happens in any of my programs if the user uses something with "*" in a file string... :?

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 8:26 pm
by Arctic Fox
c4s wrote:I seriously don't want to know what happens in any of my programs if the user uses something with "*" in a file string... :?
CheckFilename() to the rescue :wink:

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 9:15 pm
by kenmo
jassing wrote:Nor is it returning the size of *ANY* file that matches the wildcard.. so what is it returning?
Are you sure about that?

It seems to return the size in bytes of the first file (for me at least) in any folder, of any file type.

Make sure you check the exact size in bytes... for example I tested it on a file that was 8181990 bytes, but in Windows Explorer it rounds this to 7.80 MB.

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 9:22 pm
by Demivec
jassing wrote:So in my code -- what is filesize() reporting? It's not reporting the sum of the files. Nor is it returning the size of *ANY* file that matches the wildcard.. so what is it returning?

I guess I misread the help -- it says to use it for testing the existance of a file.
the file c:\temp\*.jpg does not exist.
There are files that match that, but no file of that name exists.

Since we can't use filesize() for testing if a file is there or not -- what is the best way to do that?
or is it a case of "roll your own ..."
If the filename used with FileSize() contains a wildcard than the size of *ANY* file that matches will be returned, but only the first one. If no file matches then the value -1 will be returned.

When you say that it returns a value that doesn't match any file in the directory are the values being compared at a resolution of bytes? I ask because when byte values are converted to KB's for instance the value is frequently divided by 1024 and not 1000. If this is the case it can seem unequal when it is in fact equal.

When used to test for the existence of a file, any positive value returned indicates a file matching the filename exists. Again, if you don't want wildcards in the filename then filter them out first. If you are having a user enter the filename then validate it by removing the path and using CheckFilename() before passing it on to FileSize().

It's not a case of roll-your-own. Only you would know whether or not you would want to allow a user to enter a wildcard or not as part of a filename. Those things are hopefully determined in the planning stage but unfortunately crop up in the debug stage a lot. :wink:

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 9:50 pm
by jassing
Removed due to inaccurate information.

Re: FileSize() with a wild card?

Posted: Wed Feb 16, 2011 9:55 pm
by Trond
CheckFilename() checks a filename, not the whole path. In a path the characters : and \ are present, which are invalid in file names, thus the function returns 0.

Re: FileSize() with a wild card?

Posted: Thu Feb 17, 2011 4:28 am
by PB
Filesize checks the size of the specified file. An asterisk is not specifying a file. No bug.

Re: FileSize() with a wild card?

Posted: Thu Feb 17, 2011 4:48 am
by jassing
Removed due to inaccurate information.

Re: FileSize() with a wild card?

Posted: Thu Feb 17, 2011 9:57 am
by c4s
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. :)

Re: FileSize() with a wild card?

Posted: Thu Feb 17, 2011 10:02 am
by C64
Hi, jAssing. If I may:
jassing wrote:when I issue debug "checkfilename("C:\temp\snap6.jpg") it returns 0
The manual for CheckFilename() says: The 'Filename$' must not include its path.
jassing wrote:The docs says to use it to check for the existence of a file... a file
Filenames do not have wildcards in them. Use a valid filename specification with FileSize().
See also: http://www.techterms.com/definition/gigo

Re: FileSize() with a wild card?

Posted: Thu Feb 17, 2011 3:47 pm
by Demivec
jassing wrote:
Arctic Fox wrote:
c4s wrote:I seriously don't want to know what happens in any of my programs if the user uses something with "*" in a file string... :?
CheckFilename() to the rescue :wink:
Nah, because when I issue debug "checkfilename("C:\temp\snap6.jpg") it returns 0
When I drop to dos and copy/paste the file name/path and do dir c:\temp\snap6.jpg it's there...
Make a small change to your code:

Code: Select all

#filename = "c:\temp\myfun.txt"
If CreateFile(0,#filename)
  WriteString(0,"Fun")
  CloseFile(0)
  Debug "Checking filename: "+#filename
  Debug CheckFilename( GetFilePart(#filename ) ) ; <== Exclude the path from the filename check
  If CheckFilename( GetFilePart(#filename) )
    Debug FileSize(#filename)
  EndIf 
Else
  Debug "FAILED TO CREATE FILE"
EndIf