QDOS SuperBASIC File Exists Function
QDOS SuperBASIC File Exists Function
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.
Re: QDOS SuperBASIC File Exists Function
I prefer to use Toolkit 2 functions, but without that I would probably use something like
Of course, you should also check for ERR_IU (IN USE -9), ERR_FE (FILE ERROR -16) and maybe other errors.
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
Re: QDOS SuperBASIC File Exists Function
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
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
--
All things QL - https://dilwyn.theqlforum.com
All things QL - https://dilwyn.theqlforum.com
-
- 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
Hi,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).
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
Derek
Re: QDOS SuperBASIC File Exists Function
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.
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
-
- 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
Hi,
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.
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
Derek
Re: QDOS SuperBASIC File Exists Function
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.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
ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
Re: QDOS SuperBASIC File Exists Function
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...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.
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.
Re: QDOS SuperBASIC File Exists Function
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
EX [Chan # TO] prog_spec {TO prog_spec} [ TO Chan #]
Tim