Creating a new directory

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Bad_Wolf
Posts: 19
Joined: Thu 05 Jul 2018, 06:00

Creating a new directory

Post by Bad_Wolf »

Hi all,

Can you help me with the following problem.

I want to create a directory structure but cannot find how to do it. This is what I want to do :

1. Check if directory "UserData" exist on a particular location.
2. If it doesn't exist, just create a new directory on that location named "UserData"
3. Create a variable which holds the path to the directory

This is what I found to get the directory path :

Code: Select all

DEF FN_GetDirName
      LOCAL filename%
      DIM filename% LOCAL 260
      SYS "GetModuleFileName", 0, filename%, 260
      = @dir$
In the above function, I do not understand in the SYS line what the values "0" and "260" mean. Please, can you help me with this one too.

Once the UserData directory is created, I will create other sub-directories inside it.

Can you please help me with creating the directory completely in code?

Your help will be very much appreciated!

Wish you a very nice day.

Chris
jgharston
Posts: 39
Joined: Thu 05 Apr 2018, 14:08

Re: Creating a new directory

Post by jgharston »

You mention 'a directory structure' but you also mention 'create a directory'. Which do you mean? The code you posted has nothing to do with creating directories, that tells you the pathname the running application was run with. The 0 means "this application", the 260 means "260 bytes reserved for the pathname" - as done in the previous line. Often the @dir$ system variable is more appropriate, that gives you the directory the running program was loaded from.

Do you want to create a directory, eg 'fred' in a directory that already exists? OSCLI "MKDIR "+dirname$

Do you want to create a directory where the directory it is is may not exists? For instance, you want to create the directory fred\jim\sheila\hazel and you don't know if fred, fred\jim, or fred\jim\sheila exist or not? I had thought there was code on the wiki to do this, but there isn't, so this is made up as I go:

DEFPROCf_createpath(dir$)
IF RIGHT$(dir$,1)<>"\" THEN dir$=dir$+"\":REM Ensure terminated
LOCAL dir%:REM Also sets dir%=0, start at beginning
REPEAT
dir%=INSTR(dir$,"\",dir%):REM Find next dir separator
IF dir% THEN PROCf_mkdir(LEFT$(dir$,dir%-1)):REM Create this component
UNTIL dir%=0:REM Loop for all components
ENDPROC
DEFPROCf_mkdir(dir$)
LOCAL ERROR:ON ERROR LOCAL ENDPROC:REM Ignore 'Already exists' error
OSCLI "MKDIR "+dir$
ENDPROC
Bad_Wolf
Posts: 19
Joined: Thu 05 Jul 2018, 06:00

Re: Creating a new directory

Post by Bad_Wolf »

Hello JgHarston,

Thank you very much for your help which I appreciate very much. You saved my day!

I choose your second procedure because it did exactly what I wanted to accomplish. I could not get the first procedure to work. When I tried it, the program hanged without any message.

Wish you a very nice day and all the best.

Chris