BBC BASIC for Windows
Programming >> Database and Files >> pwd equivalent?
http://bb4w.conforums.com/index.cgi?board=database&action=display&num=1379782624

pwd equivalent?
Post by g3nrw on Sep 21st, 2013, 4:57pm

I want to dynamically discover the current Windows folder at runtime. Is there a way to do this?

--
73
Ian, G3NRW


Re: pwd equivalent?
Post by admin on Sep 21st, 2013, 5:58pm

on Sep 21st, 2013, 4:57pm, g3nrw wrote:
I want to dynamically discover the current Windows folder at runtime. Is there a way to do this?

The first thing to note is that the 'current directory' won't change unless you change it, since it's private to each process, so you can keep track of any changes you make yourself. Having said that, there's very rarely any need to change it:

http://bb4w.wikispaces.com/When+and+when+not+to+use+%2ACD

But if you simply want to know what it is you can call the 'GetCurrentDirectory' API:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934.aspx

A BBC BASIC function FNcurrentdirectory to do that is listed in the Help manual under 'Operating System Interface... *CHDIR':

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin8.html#chdir

Richard.
Re: pwd equivalent?
Post by g3nrw on Sep 22nd, 2013, 08:53am

Many thanks Richard. I discovered the links you mentioned soon after I posted -- there is so *much* excellent documentation to plough through!

Now another, even simpler, question to do with directories. I want to dynamically create a subfolder, called (for example) sub1, sub2, sub3, subn etc.

I have tried something like:

~~~~~~~~~~~~~~~~~~~~~
subfolder_name$ = "sub1"
*MKDIR subfolder_name$
~~~~~~~~~~~~~~~~~~~~~

but that just creates a folder called literally "subfolder_name$". Is there any way to express the name as a variable when using*MKDIR?

Another simple question: if the folder already exists, I get the "File exists" message. How can I trap (and ignore) this?

Sorry for the very basic questions. I really have dug through all the documentation I can find, and spent a long (interesting) time experimenting!

--
73
Ian, G3NRW






Re: pwd equivalent?
Post by admin on Sep 22nd, 2013, 10:01am

on Sep 22nd, 2013, 08:53am, g3nrw wrote:
I have tried something like:

Code:
subfolder_name$ = "sub1"
*MKDIR subfolder_name$ 

See the Help file under 'Operating system interface... Accessing star commands':

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin8.html#accessing

There it says "OSCLI must be used when all or part of the star command needs to be determined at run-time (i.e. is a variable rather than a constant)".

In the section on MKDIR there is code listed specifically to do what you want:

Code:
OSCLI "MKDIR """+directory$+"""" 

Quote:
Another simple question: if the folder already exists, I get the "File exists" message. How can I trap (and ignore) this?

You basically have three options:
  1. Test whether the directory exists first. You can easily do that by attempting to open the virtual file NUL which exists in every directory:
    Code:
      exists% = OPENIN(directory$ + "\NUL")
      IF exists% CLOSE #exists% ELSE OSCLI "MKDIR """+directory$+"""" 

  2. Wrap the MKDIR call in a procedure and trap errors locally:
    Code:
      DEF PROCmkdir(directory$)
      ON ERROR LOCAL ENDPROC
      OSCLI "MKDIR """+directory$+""""
      ENDPROC 

  3. Use the Windows API, and ignore the returned value:
    Code:
      SYS "CreateDirectory", directory$, 0 

Richard.