QDOS SuperBASIC File Exists Function

Anything QL Software or Programming Related.
User avatar
bwinkel67
QL Wafer Drive
Posts: 1542
Joined: Thu Oct 03, 2019 2:09 am

QDOS SuperBASIC File Exists Function

Post by bwinkel67 »

Without adding any toolkits to QDOS, what's the simplest way of checking if a file exists in SuperBASIC? I came up with a method that opens a file, dir's into that file, the loops through it to compare file names, but I was wondering if there was a simpler way to do that (similar to what FTEST from TK2 does).
Last edited by bwinkel67 on Sat Jun 22, 2024 12:08 am, edited 1 time in total.


User avatar
Andrew
QL Wafer Drive
Posts: 1047
Joined: Tue Jul 17, 2018 9:10 pm

Re: QDOS SuperBASIC File Exists Function

Post by Andrew »

I prefer to use Toolkit 2 functions, but without that I would probably use something like

Code: Select all

100 WHEN ERRor
110   IF ERR_NF
120      PRINT#0,'File does not exist!'\'Press a key'
130      RETRY 500
140   END IF
150 END WHEN
500 INPUT  "Filename:  ";fn$
510 OPEN_IN#4, fn$
520 PRINT#0,'File '&fn$&' exists!': close#4
Of course, you should also check for ERR_IU (IN USE -9), ERR_FE (FILE ERROR -16) and maybe other errors.


User avatar
dilwyn
Mr QL
Posts: 3118
Joined: Wed Dec 01, 2010 10:39 pm

Re: QDOS SuperBASIC File Exists Function

Post by dilwyn »

Another way is to error trap an OPEN_IN statement on ROMs which implement WHEN ERRor (i.e. not versions AH, JM or earlier). Sadly, even on ROMs which do implement WHEN, that's not always 100% reliable in interpreted BASIC apart from Minerva, though it should be OK when compiled on all ROMs.

As you've realised, doing this on a QL without Toolkit 2 can be fiddly and rather long-winded. A QL without TK2 feels rather bare these days!

EDIT:Sorry Andrew, our replies crossed


Derek_Stewart
Font of All Knowledge
Posts: 4768
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: QDOS SuperBASIC File Exists Function

Post by Derek_Stewart »

bwinkel67 wrote: Fri Jun 21, 2024 12:37 pm Without adding any toolkits to QDOS, what's the simplest way of checking if a file exists in SuperBASIC? I came up with a method that opens a file, dir's into that file, the loops through it to compare file names, but I was wondering if there was a simpler way to do that (similar to what FCHECK from TK2 does).
Hi,

Do you mean the Toolkit 2 function: FTEST

I see no mention of FCHECK in Toolkit 2, Superbasic Online Manual, other SBASIC texts. Can you explain where the FCHECK command / function comes from and what is the syntax?


Regards,

Derek
User avatar
bwinkel67
QL Wafer Drive
Posts: 1542
Joined: Thu Oct 03, 2019 2:09 am

Re: QDOS SuperBASIC File Exists Function

Post by bwinkel67 »

Derek_Stewart wrote: Fri Jun 21, 2024 1:28 pm Do you mean the Toolkit 2 function: FTEST
Yes, FCHECK was a typo (misremembering).


User avatar
bwinkel67
QL Wafer Drive
Posts: 1542
Joined: Thu Oct 03, 2019 2:09 am

Re: QDOS SuperBASIC File Exists Function

Post by bwinkel67 »

Oh, I like the WHEN ERRor method. I ended up writing a long-winded function and was hoping that either a command already existed or it could be more efficient. I think the OPEN_IN/WHEN ERRor method might just do that.

Here is my FCHECK function (that's why I confused the name with TK2's FTEST). This one doesn't use WHEN, so if that's flaky, then maybe this will do. Of course it requires writing to a disk but if a dynamic ram disk were installed, then it could use that.

Code: Select all

100 DEFine FuNction FCHECK(mdv$,name$)
105   DELETE mdv$&"dir"
110   OPEN_NEW #9, mdv$&"dir"
115   DIR #9, mdv$: CLOSE #9
120   OPEN_IN #9,mdv$&"dir"
125   REPeat loop
130     IF EOF(#9): CLOSE #9: DELETE mdv$&"dir": RETurn 0
135     INPUT #9, file$
140     IF file$=name$: CLOSE #9: DELETE mdv$&"dir": RETurn 1
145   END REPeat loop
150 END DEFine


Derek_Stewart
Font of All Knowledge
Posts: 4768
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: QDOS SuperBASIC File Exists Function

Post by Derek_Stewart »

Hi,
bwinkel67 wrote: Sat Jun 22, 2024 12:09 am Yes, FCHECK was a typo (misremembering).
I thought that I had not read the manual right.

If you are not using any toolkit extensions, then you are going have problems, as the WHEN ERRor error trapping did not really work correctly, till JS, ROM, but still had issues, Minerva fixed it.

But maybe the US ROMs were all debugged.


Regards,

Derek
User avatar
tofro
Font of All Knowledge
Posts: 3132
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: QDOS SuperBASIC File Exists Function

Post by tofro »

bwinkel67 wrote: Sat Jun 22, 2024 12:41 am Oh, I like the WHEN ERRor method. I ended up writing a long-winded function and was hoping that either a command already existed or it could be more efficient. I think the OPEN_IN/WHEN ERRor method might just do that.

Here is my FCHECK function (that's why I confused the name with TK2's FTEST). This one doesn't use WHEN, so if that's flaky, then maybe this will do. Of course it requires writing to a disk but if a dynamic ram disk were installed, then it could use that.

Code: Select all

100 DEFine FuNction FCHECK(mdv$,name$)
105   DELETE mdv$&"dir"
110   OPEN_NEW #9, mdv$&"dir"
115   DIR #9, mdv$: CLOSE #9
120   OPEN_IN #9,mdv$&"dir"
125   REPeat loop
130     IF EOF(#9): CLOSE #9: DELETE mdv$&"dir": RETurn 0
135     INPUT #9, file$
140     IF file$=name$: CLOSE #9: DELETE mdv$&"dir": RETurn 1
145   END REPeat loop
150 END DEFine
One proposal: Instead of writing to a temporary file on disk, I would use a pipe to "DIR" into and read from. That eliminates most of the mess the function might leave behind. Pipes in QDOS are extremely underrated.


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
bwinkel67
QL Wafer Drive
Posts: 1542
Joined: Thu Oct 03, 2019 2:09 am

Re: QDOS SuperBASIC File Exists Function

Post by bwinkel67 »

tofro wrote: Sat Jun 22, 2024 7:38 am One proposal: Instead of writing to a temporary file on disk, I would use a pipe to "DIR" into and read from. That eliminates most of the mess the function might leave behind. Pipes in QDOS are extremely underrated.
I understand pipes exist in QDOS but are they exposed to the user in SuperBASIC? Could you show me an example of how to do it as that would be pretty neat...

NOTE if you didn't read the first post, I'm trying to do this in JS ROM with no add-on toolkits, so no TK2.


swensont
Forum Moderator
Posts: 325
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: QDOS SuperBASIC File Exists Function

Post by swensont »

IIRC, Pipes or Filters only work with TKII. I checked the TKII manual and it shows an example of pipes using the EX command:

EX [Chan # TO] prog_spec {TO prog_spec} [ TO Chan #]

Tim


Post Reply