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

FileSize() with a wild card?

Post by jassing »

Removed due to inaccurate information.
Last edited by jassing on Fri Feb 18, 2011 7:43 am, edited 2 times in total.
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Re: FileSize() with a wild card?

Post 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!
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 »

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().
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:44 am, edited 1 time in total.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: FileSize() with a wild card?

Post 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... :?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: FileSize() with a wild card?

Post 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:
User avatar
kenmo
Addict
Addict
Posts: 2032
Joined: Tue Dec 23, 2003 3:54 am

Re: FileSize() with a wild card?

Post 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.
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 »

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:
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:43 am, edited 1 time in total.
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 »

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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: FileSize() with a wild card?

Post by PB »

Filesize checks the size of the specified file. An asterisk is not specifying a file. No bug.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
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:44 am, edited 1 time in total.
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:
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. :)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: FileSize() with a wild card?

Post 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
Last edited by C64 on Fri Feb 18, 2011 1:01 pm, edited 1 time in total.
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 »

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