scripting readfile()

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
graham
Posts: 457
Joined: 24 Aug 2007 22:08
Location: Isle of Man

scripting readfile()

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

admin
Site Admin
Posts: 66347
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: scripting readfile()

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

graham
Posts: 457
Joined: 24 Aug 2007 22:08
Location: Isle of Man

Re: scripting readfile()

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

admin
Site Admin
Posts: 66347
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: scripting readfile()

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

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Re: scripting readfile()

Post 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 :)
Proud XYplorer Fanatic

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: scripting readfile()

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

graham
Posts: 457
Joined: 24 Aug 2007 22:08
Location: Isle of Man

Re: scripting readfile()

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

j_c_hallgren
XY Blog Master
Posts: 5826
Joined: 02 Jan 2006 19:34
Location: So. Chatham MA/Clearwater FL
Contact:

Re: scripting readfile()

Post 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?
Still spending WAY TOO much time here! But it's such a pleasure helping XY be a treasure!
(XP on laptop with touchpad and thus NO mouse!) Using latest beta vers when possible.

Post Reply