Page 1 of 1

scripting readfile()

Posted: 15 Jan 2009 18:27
by graham
Is it possible to test a return value for success or not of the function? Also, is the test bit in scripting 3 to provide and easy path to a sub?

Re: scripting readfile()

Posted: 15 Jan 2009 20:05
by admin
graham wrote:Is it possible to test a return value for success or not of the function? Also, is the test bit in scripting 3 to provide and easy path to a sub?
No.
Uh, no. (Not sure if I got the 2nd question...)

Re: scripting readfile()

Posted: 15 Jan 2009 20:32
by graham
ok to be more specific - in the script I posted to give pdf help file access I use readfile() to get the paths of files used subsequently, however, if not found (when run first time) the script gives a scripting error. Pressing continue at that stage causes the script to continue which proceeds to request input and then creates and writes the file.

So what I want is the ability in scripting to avoid the visible error message and go to the sub which handles a file not found condition.

Re: scripting readfile()

Posted: 15 Jan 2009 20:50
by admin
graham wrote:ok to be more specific - in the script I posted to give pdf help file access I use readfile() to get the paths of files used subsequently, however, if not found (when run first time) the script gives a scripting error. Pressing continue at that stage causes the script to continue which proceeds to request input and then creates and writes the file.

So what I want is the ability in scripting to avoid the visible error message and go to the sub which handles a file not found condition.
I personally could not live without a function ExistFile(), so why not simply add it to XY scripting? Any objections (comments) anybody?

Re: scripting readfile()

Posted: 15 Jan 2009 20:54
by jacky
graham wrote:ok to be more specific - in the script I posted to give pdf help file access I use readfile() to get the paths of files used subsequently, however, if not found (when run first time) the script gives a scripting error. Pressing continue at that stage causes the script to continue which proceeds to request input and then creates and writes the file.

So what I want is the ability in scripting to avoid the visible error message and go to the sub which handles a file not found condition.
hmm... I'm not sure this is possible right now, but why don't you simply use an INI file (with getkey/setkey) to store your settings, instead of using readfile()? Calling getkey on a non-existing file doesn't throw any error (nor does it create an empty file, just returns an empty string) so that should allow you to do what you need, no?

And if/once we'll get the ability to specify an itemslist to function report(), then obviously we'll have a file_exists function basically :)

Re: scripting readfile()

Posted: 15 Jan 2009 21:36
by TheQwerty
If you just want to ensure the files exist you could use WriteFile(), but if it didn't exist you'll create it in the process.

Code: Select all

$exists = WriteFile($file,"","n") > 1 ? "File Found" : "File Not Found (Until I created it just now.)");
But, like jacky, using Get/SetKey makes more sense to me.


EDIT: Don, why don't we have bitwise operators/comparison yet? Seems a bit silly to return a value that's been OR-ed, without the tools to properly dissect it. :P Luckily, it only has two bits right now!

Re: scripting readfile()

Posted: 15 Jan 2009 21:42
by graham
Jacky wrote:
hmm... I'm not sure this is possible right now, but why don't you simply use an INI file (with getkey/setkey) to store your settings, instead of using readfile()? Calling getkey on a non-existing file doesn't throw any error (nor does it create an empty file, just returns an empty string) so that should allow you to do what you need, no?
Yes you are right but, I wanted to try out using a specific file since we have read and write file then surely one should be able to test if a file exists. I think such a function existfile() as Don has mentioned could open up a lot of possibilities when processing text and other data in scripts.

Added to which, I posted the wrong copy of my script :oops: , now hopefully corrected. I have used the this script a lot and found it a quick way to get specific access to the pdf help file.

Re: scripting readfile()

Posted: 12 Feb 2010 16:36
by j_c_hallgren
(Reusing this thread as subject/topic is very appropriate):

I'm not a scripter but based on reading a request in http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=4689, it would seem there may not be any ability in scripting to read a given amount of data from a file...that is, like the first "n" chars, so that some decisions could be made if one finds a certain value within it...

Readfile appears to read the entire file which won't work very well (if at all) for huge files but reading the first, let's say, 100 chars should be enough to know that it's a jpg file or a gif.

Is there presently a way to do this?