Error with OSCLI "MKDIR"

Discussions related to database technologies, file handling, directories and storage
MattC
Posts: 107
Joined: Mon 16 Apr 2018, 06:17

Error with OSCLI "MKDIR"

Post by MattC »

Hi,

An initial part of my program requires that it tests whether a directory exists, and, if not, attempts to create one. I have been using a version of the one found on the wiki site, which tries to open the file "...\NUL".

Code: Select all

      DEF FN_dircreated(dir$)
      IF FN_direxists(dir$) THEN = TRUE
      OSCLI "MKDIR """ + dir$ + """"
      IF FN_direxists(dir$) THEN = TRUE ELSE = FALSE

      DEF FN_direxists(dir$)
      LOCAL F%
      IF RIGHT$(dir$) = "\" THEN dir$ = LEFT$(dir$)
      F% = OPENIN(dir$+"\NUL")
      IF F% CLOSE #F% : = TRUE ELSE = FALSE
If it can't open the file, then (assumption) the folder doesn't exists and the program therefore tries to create one. This has worked fine for years. However, I am now trying to test the existence of a folder which is in a restricted area (the Program Files folder). The moment the program tries to make the directory there is an untrappable (as far as I can see) error: 'Access denied'. Without know where the folder is to be created (the user might be able to change this location) or whether the area is restricted (at least to this program) the routine will try to create the directory ('MKDIR'). If access is denied, it errors.

I have a feeling that, many years ago, I seemed to remember a way of creating a directory using a SYS command that could return a result. This, theoretically, would prevent the untrappable error, whether the file existed or not, whether the area was restricted or not. If this is true, however, I cannot remember the command line, nor can I find anything on the MS information site to help. Does anyone know if I'm remembering right?

Matt

DDRM
Posts: 396
Joined: Mon 02 Apr 2018, 18:04

Re: Error with OSCLI "MKDIR"

Post by DDRM »

Hi Matt,

I think you are looking for CreateDirectory or its variants:

Code: Select all

      parent$ = "C:\David\BB4W\EXAMPLES\David\Forum bits"
      newdir$="\MySubDir"

      SYS "CreateDirectoryA",parent$ + newdir$,0 TO check%
      PRINT check%
The returned value will be 0 if the call failed, and something else if it succeeded. It appears that the second parameter (the 0) is NOT optional, despite what it says on the MSDN website - possibly due to changes in Windows version since the page I looked at. It is some sort of security setting - I haven't looked into the details, but you could check it up on MSDN.

This works for me on a quick check, and returns 0 if the directory already exists, but I haven't tried to break it (for example by writing to an inaccessible directory).

Best wishes,

D

Hated Moron
Posts: 97
Joined: Thu 07 Apr 2022, 10:10

Re: Error with OSCLI "MKDIR"

Post by Hated Moron »

DDRM wrote:
Fri 08 Apr 2022, 08:03
It appears that the second parameter (the 0) is NOT optional, despite what it says on the MSDN website
If MSDN says it is optional, it is optional! Such serious errors at MSDN are vanishingly rare. However, you need to understand what optional means in that context: it means you may safely pass the value zero for that parameter. It does not mean that you can actually omit the parameter (that would change the function signature)!

However, calling an Operating System function is not the recommended solution, because it makes your program platform-specific. If you want the MKDIR command to fail 'silently' you should wrap it in an SEH block:

Code: Select all

      ON ERROR LOCAL IF FALSE THEN
        OSCLI "MKDIR """ + new_directory$ + """"
      ENDIF : RESTORE ERROR
If you need to know why it failed the best approach is to call the appropriate version of 'DIscovering an unknown error' for your platform, i.e. for BBCSDL or BB4W. In a cross-platform program you can choose which version you call:

Code: Select all

      ON ERROR LOCAL IF FALSE THEN
        OSCLI "MKDIR """ + new_directory$ + """"
      ELSE
        IF INKEY(-256) = &57 THEN
          reason$ = FNwinerror
        ELSE
          reason$ = FNsdlerror
        ENDIF
      ENDIF : RESTORE ERROR
HM.
If you are unhappy with any aspect of this post, please report it to the forum administrator (click on the exclamation mark icon in the top-right corner) rather than complaining on the open forum or by private email.

MattC
Posts: 107
Joined: Mon 16 Apr 2018, 06:17

Re: Error with OSCLI "MKDIR"

Post by MattC »

Thanks, Both,

I'll investigate both options.

Matt