BBC BASIC
« MKDIR error? »

Welcome Guest. Please Login or Register.
Jan 23rd, 2018, 7:12pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: MKDIR error?  (Read 332 times)
mavison
New Member
Image


member is offline

Avatar




PM


Posts: 27
xx MKDIR error?
« Thread started on: May 1st, 2017, 11:19am »

The help for the MKDIR command says "If a directory with that name already exists, the File exists error will result." which is ERR 196.

However, I seem to get ERR=254 Bad Command.

This is using SDL 16a (and also on the new 17a) on Windows.

I am trying to write code which works on Windows and MacOS to create a directory, but not raise error if it is already there.

Will I get ERR=254 on both platforms?
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 27
xx Re: MKDIR error?
« Reply #1 on: May 1st, 2017, 11:46am »

To add to my post: It seems on MacOS I do get the same 254 error.

But on the Mac an OPENIN(dir$+"\NUL") always returns zero, even when the directory exists. That was the previous method I tried ... which did not work!

If 254 is correct and consistent then I can use that.

User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 692
xx Re: MKDIR error?
« Reply #2 on: May 1st, 2017, 11:57am »

on May 1st, 2017, 11:19am, mavison wrote:
The help for the MKDIR command says "If a directory with that name already exists, the File exists error will result." which is ERR 196. However, I seem to get ERR=254 Bad Command.

The Help documentation for BB4W doesn't always apply in detail to BBCSDL; sometimes differences in the underlying Operating System make that inevitable.

Quote:
Will I get ERR=254 on both platforms?

Ideally you should not rely on the error code, and nor should you rely on being able to tell the difference between a 'directory already exists' and some other reason for an error, because the underlying OS may not reliably report that information.

The safest thing to do is to wrap the MKDIR in a SEH (Structured Exception Handling) clause and treat all errors the same way. If you just want to ignore an error do this:

Code:
      ON ERROR LOCAL IF FALSE THEN
        OSCLI "mkdir ......."
      ENDIF : RESTORE ERROR 

Richard.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 692
xx Re: MKDIR error?
« Reply #3 on: May 1st, 2017, 12:55pm »

on May 1st, 2017, 11:46am, mavison wrote:
But on the Mac an OPENIN(dir$+"\NUL") always returns zero, even when the directory exists.

Not only is the "NUL" filename an OS-specific feature, "\" isn't even a directory delimiter on a Mac!

Anyway, testing for the existence of a directory that way, even on Windows, is fundamentally flawed because it only tells you whether the directory existed at that instant. A few microseconds later things may be different: if the test told you the directory existed it may no longer do so, and if it told you it didn't now it might!

Some people think that trapping errors and allowing them to happen is poor programming practice, but on a pre-emptive multi-tasking system it is actually the best method.

Quote:
If 254 is correct and consistent then I can use that.

No, as I said, I cannot guarantee that. Treat all errors resulting from MKDIR the same, that way you won't need to care about the error code. Use SEH to confine the error trapping only to that command, as I showed.

I would want to be very cautious about relying on any error code other than those generated by the core BBC BASIC interpreter.

Richard.
User IP Logged

mavison
New Member
Image


member is offline

Avatar




PM


Posts: 27
xx Re: MKDIR error?
« Reply #4 on: May 1st, 2017, 3:50pm »

Quote:
The Help documentation for BB4W doesn't always apply in detail to BBCSDL; sometimes differences in the underlying Operating System make that inevitable.

I thought that might be the case. I am now using...
Code:
DEF PROCmkdir(dir$)               :REM Create directory if does not exist
ON ERROR LOCAL :IF ERR=254 THEN ENDPROC ELSE RESTORE ERROR :ERROR ERR,REPORT$+" at "+STR$ERL
OSCLI("mkdir """+dir$+"""")
ENDPROC 

which is has the same effect as your suggestion, but reports any other errors, and seems to work on both Windows and Mac (unlike the open /NULL). I note your concerns about using 254.

Thanks for your reply Richard.

Martin
« Last Edit: May 1st, 2017, 3:53pm by mavison » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls