by Richard Russell, June 2007
In BBC BASIC for Windows you can change the current directory (folder) using the *CD (or alternatively OSCLI “CD”) command:
NewDirectory$ = "C:\MyPath\MyFolder" OSCLI "CD """+NewDirectory$+""""
(for an explanation of the use of the quotation marks see the article Filenames containing spaces).
However, as a general rule, you probably should not do so!
An occasion when it is appropriate to use *CD (or an equivalent) is when your program prompts the user for a filename and you want to determine the default directory used if the user doesn't specify one explicitly:
*CD "C:\MyPath\MyFolder" INPUT "Enter a filename: "filename$ infile% = OPENIN(filename$)
This code segment will open, for input, the file specified by the user. If he enters a simple filename, rather than an absolute path, then the directory used will be the one selected in the *CD command.
The simple answer is, in most other cases! Whenever possible, you should specify an absolute path/file name rather than relying on the setting of the current directory. There are four main ways in which you can generate an absolute path/file name:
If you happen to know that an image file is located at the specific path “C:\folder1\folder2\image.bmp” then you can access it as follows:
BMPfile$ = "C:\folder1\folder2\image.bmp" OSCLI "DISPLAY """+BMPfile$+""""
This is unlikely to be a good method in a program that you intend to distribute, because you should never rely on knowing the specific directory structure (or even drive letter) of the disk it will eventually be running on.
If you know that an image file is located in a specific place relative to the directory in which your program is stored then you can access it as follows:
BMPfile$ = @dir$+"resources\image.bmp" OSCLI "DISPLAY """+BMPfile$+""""
This method is handy for accessing resource files, such as pictures and music. Conveniently, the BBC BASIC for Windows compiler can automatically embed such files in the executable and extract them when the program is run.
If you know that a file is located relative to the library folder @lib$ then you can use a similar method to the previous one:
BMPfile$ = @lib$+"mylib\image.bmp" OSCLI "DISPLAY """+BMPfile$+""""
This it most obviously useful for library files themselves, but can also be handy for other resource files when you don't want to store them relative to the program's own directory. This may be because the program's directory isn't guaranteed to be writable, or you want to hide the files from casual examination, or you want to delete them automatically when the program quits.
The majority of Windows API functions which return a filename return an absolute path. An example would be a user-selected filename obtained from the GetOpenFileName API function. Another would be accessing a file in the temporary folder:
tempfile$ = FNtemppath+"\myfile.tmp" outfile% = OPENOUT(tempfile$)
where FNtemppath is defined here.
One reason why it is generally undesirable to change the current directory within your program is that its initial setting may contain useful information. If you compile your program to a standalone executable, and the final user executes it by means of a shortcut (e.g. on the desktop) then the current directory is determined by the Start in property of the shortcut. This can be changed by the user by right-clicking on the shortcut and selecting 'Properties… Shortcut'.
Suppose you write a program which needs to store data files, and you would like the user to be able to determine where those files are stored. You could provide an explicit user interface for him to select it, but alternatively you could simply store the files in the current directory (by omitting the path) and let the user determine that by editing the shortcut properties. This is a 'cheap and cheerful', but effective, way of giving the user control without complicating your program.