Discussion:
Testing for the presence of a file
(too old to reply)
Graham Pickles
2018-10-08 11:35:31 UTC
Permalink
I'm having a senior moment! What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?

e.g. A file called test might/might not exist in my <Data$Dir>
directory. If it exists then I wish to remove it but don't want to
create an error if it doesn't exist!

Regards to all,
--
Graham Pickles
If you can't convince them confuse them. (Harry S. Truman)
Alan Adams
2018-10-08 11:50:39 UTC
Permalink
Post by Graham Pickles
I'm having a senior moment! What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?
e.g. A file called test might/might not exist in my <Data$Dir>
directory. If it exists then I wish to remove it but don't want to
create an error if it doesn't exist!
Regards to all,
X=OPENIN(TESTFILE$)
IF X=0 THEN
PRINT "FILE "+TESTFILE$+" DOES NOT EXIST"
ELSE
CLOSE#X
PRINT "FILE "+TESTFILE$+" EXISTS"
ENDIF

Alan Adams, from Northamptonshire
***@adamshome.org.uk
http://www.nckc.org.uk/
Bob Latham
2018-10-08 12:17:44 UTC
Permalink
Post by Alan Adams
Post by Graham Pickles
I'm having a senior moment! What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?
e.g. A file called test might/might not exist in my <Data$Dir>
directory. If it exists then I wish to remove it but don't want to
create an error if it doesn't exist!
Regards to all,
X=OPENIN(TESTFILE$)
IF X=0 THEN
PRINT "FILE "+TESTFILE$+" DOES NOT EXIST"
ELSE
CLOSE#X
PRINT "FILE "+TESTFILE$+" EXISTS"
ENDIF
Okay, complication of "in Basic" but isn't "OS_File 17" the way to go
with a bit of oscli perhaps?


Cheers,

Bob.
--
Bob Latham
Stourbridge, West Midlands
Alan Adams
2018-10-08 13:24:43 UTC
Permalink
Post by Bob Latham
Post by Alan Adams
Post by Graham Pickles
I'm having a senior moment! What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?
e.g. A file called test might/might not exist in my <Data$Dir>
directory. If it exists then I wish to remove it but don't want to
create an error if it doesn't exist!
Regards to all,
X=OPENIN(TESTFILE$)
IF X=0 THEN
PRINT "FILE "+TESTFILE$+" DOES NOT EXIST"
ELSE
CLOSE#X
PRINT "FILE "+TESTFILE$+" EXISTS"
ENDIF
Okay, complication of "in Basic" but isn't "OS_File 17" the way to go
with a bit of oscli perhaps?
And in the BASIC above, X=0 after the CLOSE, and near the start of the
progrsamme
X=0
so that your error trap can include

IF X<>0 THEN CLOSE#X
without introducing an error into the error handler.
Post by Bob Latham
Cheers,
Bob.
--
Alan Adams, from Northamptonshire
***@adamshome.org.uk
http://www.nckc.org.uk/
Bob Latham
2018-10-08 13:22:21 UTC
Permalink
Post by Bob Latham
Post by Alan Adams
Post by Graham Pickles
I'm having a senior moment! What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?
e.g. A file called test might/might not exist in my <Data$Dir>
directory. If it exists then I wish to remove it but don't want to
create an error if it doesn't exist!
Regards to all,
X=OPENIN(TESTFILE$)
IF X=0 THEN
PRINT "FILE "+TESTFILE$+" DOES NOT EXIST"
ELSE
CLOSE#X
PRINT "FILE "+TESTFILE$+" EXISTS"
ENDIF
Okay, complication of "in Basic" but isn't "OS_File 17" the way to go
with a bit of oscli perhaps?
Actually, if all you need it to delete a file if it exists but not
error if it doesn't then "XOS_File" 6 looks the business. PRM says no
error if not present but it does error if locked.

Only needs R0 set to 6 and R1 a pointer to the file.

That must be pretty easy to setup from Basic I would imagine probably
OSCLI.

The other choice I think is OS_FSControl 27 which will delete if
locked if you set the flags.

I'm far from an expert on this, hope I'm not miles out.


Cheers,

Bob.
--
Bob Latham
Stourbridge, West Midlands
John Williams (News)
2018-10-08 13:42:05 UTC
Permalink
Post by Graham Pickles
What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?
This simple function will not only tell you if it's there or not, but if it
is whether it's a file, directory, or image file:

:
DEF FN_objectpresent(fullpath$)
SYS"OS_File",23,fullpath$ TO present%,,,,,,objftype%
=present%
REM case present% of 0=absent 1=file 2=directory 3=image file
:

I use it quite a lot!

John
--
John Williams, now back in the UK - no attachments to these addresses!
Non-RISC OS posters change user to johnrwilliams or put 'risc' in subject!
Who is John Williams? http://petit.four.free.fr/picindex/author/
Graham Pickles
2018-10-09 08:33:46 UTC
Permalink
Post by John Williams (News)
Post by Graham Pickles
What is the simplest method of determining
in a Basic (RiscOs 4.02) program whether a named file exists in a
particular directory?
This simple function will not only tell you if it's there or not, but if it
DEF FN_objectpresent(fullpath$)
SYS"OS_File",23,fullpath$ TO present%,,,,,,objftype%
=present%
REM case present% of 0=absent 1=file 2=directory 3=image file
I use it quite a lot!
John
Many thanks. Of course. You reminded me. This is the method I have
adopted and it's ideal.

Regards,
--
Graham Pickles
I have not failed. I've just found 10000 ways that won't work. (Thomas
Edison)
John Williams (News)
2018-10-09 22:59:15 UTC
Permalink
Post by Graham Pickles
Post by John Williams (News)
DEF FN_objectpresent(fullpath$)
SYS"OS_File",23,fullpath$ TO present%,,,,,,objftype%
=present%
REM case present% of 0=absent 1=file 2=directory 3=image file
I use it quite a lot!
John
Many thanks. Of course. You reminded me. This is the method I have
adopted and it's ideal.
Thanks, Graham - it's not often I'm the chosen one!

But, to be fair, other contributions were equally good!

It's just that this one lends itself to being in a library of goodies!

Best wishes,

John
--
John Williams, now back in the UK - no attachments to these addresses!
Non-RISC OS posters change user to johnrwilliams or put 'risc' in subject!
Who is John Williams? http://petit.four.free.fr/picindex/author/
John Williams (News)
2018-10-08 13:50:30 UTC
Permalink
If it exists then I wish to remove it but don't want to create an error
if it doesn't exist!
Alternatively you could OSCLI (or WimpTask) "IfThere "+<path$>+"Wipe
"+<path$> it - that doesn't raise an error if it's not present!

John
--
John Williams, now back in the UK - no attachments to these addresses!
Non-RISC OS posters change user to johnrwilliams or put 'risc' in subject!
Who is John Williams? http://petit.four.free.fr/picindex/author/
John Williams (News)
2018-10-08 13:53:28 UTC
Permalink
Post by John Williams (News)
Alternatively you could OSCLI (or WimpTask) "IfThere "+<path$>+"Wipe
"+<path$> it - that doesn't raise an error if it's not present!
Sorry - missed the "Then" out!

John
--
John Williams, now back in the UK - no attachments to these addresses!
Non-RISC OS posters change user to johnrwilliams or put 'risc' in subject!
Who is John Williams? http://petit.four.free.fr/picindex/author/
j***@mdfs.net
2018-10-22 02:57:08 UTC
Permalink
If it exists then I wish to remove it but don't want to create an error
if it doesn't exist!
*Remove or OS_File 7.
j***@mdfs.net
2018-10-22 03:00:11 UTC
Permalink
Post by j***@mdfs.net
If it exists then I wish to remove it but don't want to create an error
if it doesn't exist!
*Remove or OS_File 7.
Sorry, *Remove or OS_File 6.

SYS "XOS_File",6,objecttodelete$

OS_File 6 is defined to return R0=0 and no error if the object doesn't
exist, but it doesn't hurt to X-ify it in case the filing system is
broken.
j***@mdfs.net
2018-10-22 02:55:51 UTC
Permalink
Why OS_File 17 or 23? What's wrong with good old OS_File 5?

SYS "OS_File",5,pathname$ TO objtype%
Matthew Phillips
2018-10-23 06:43:52 UTC
Permalink
Post by j***@mdfs.net
Why OS_File 17 or 23? What's wrong with good old OS_File 5?
SYS "OS_File",5,pathname$ TO objtype%
Personally, I avoid OS_File 5 because it uses File$Path -- and I've always
wanted to know whether a file was really where I was looking for it, not
somewhere else.

But perhaps File$Path only comes into effect if you do not supply a full path
in R1, in which case it isn't an issue.
--
Matthew Phillips
Durham
Loading...