User Tools

Site Tools


when_20and_20when_20not_20to_20use_20_2acd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
when_20and_20when_20not_20to_20use_20_2acd [2018/03/31 13:19]
127.0.0.1 external edit
when_20and_20when_20not_20to_20use_20_2acd [2018/04/17 19:28] (current)
tbest3112 Added syntax highlighting
Line 1: Line 1:
-=====When and when not to use _CD=====+=====When and when not to use *CD=====
  
 //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:\\ \\  //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:\\ \\ 
 +<code bb4w>
         *CD "​C:​\MyPath\MyFolder"​         *CD "​C:​\MyPath\MyFolder"​
-  ​or+</​code>​ 
 +or 
 +<code bb4w>
         NewDirectory$ = "​C:​\MyPath\MyFolder"​         NewDirectory$ = "​C:​\MyPath\MyFolder"​
         OSCLI "CD """​+NewDirectory$+""""​         OSCLI "CD """​+NewDirectory$+""""​
 +</​code>​
 (for an explanation of the use of the quotation marks see the article [[/​Filenames%20containing%20spaces|Filenames containing spaces]]).\\ \\  However, as a general rule, you probably should **not** do so!\\ \\  (for an explanation of the use of the quotation marks see the article [[/​Filenames%20containing%20spaces|Filenames containing spaces]]).\\ \\  However, as a general rule, you probably should **not** do so!\\ \\ 
 ===== When you //should// use *CD ===== ===== When you //should// use *CD =====
 \\  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:​\\ \\  \\  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:​\\ \\ 
 +<code bb4w>
         *CD "​C:​\MyPath\MyFolder"​         *CD "​C:​\MyPath\MyFolder"​
         INPUT "Enter a filename: "​filename$         INPUT "Enter a filename: "​filename$
         infile% = OPENIN(filename$)         infile% = OPENIN(filename$)
 +</​code>​
 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.\\ \\  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.\\ \\ 
 ===== When you //should not// use *CD ===== ===== When you //should not// use *CD =====
Line 17: Line 23:
 ==== Specify it explicitly ==== ==== Specify it explicitly ====
 \\  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:\\ \\  \\  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:\\ \\ 
 +<code bb4w>
         BMPfile$ = "​C:​\folder1\folder2\image.bmp"​         BMPfile$ = "​C:​\folder1\folder2\image.bmp"​
         OSCLI "​DISPLAY """​+BMPfile$+""""​         OSCLI "​DISPLAY """​+BMPfile$+""""​
 +</​code>​
 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.\\ \\  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.\\ \\ 
 ==== Specify it relative to @dir$ ==== ==== Specify it relative to @dir$ ====
 \\  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:\\ \\  \\  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:\\ \\ 
 +<code bb4w>
         BMPfile$ = @dir$+"​resources\image.bmp"​         BMPfile$ = @dir$+"​resources\image.bmp"​
         OSCLI "​DISPLAY """​+BMPfile$+""""​         OSCLI "​DISPLAY """​+BMPfile$+""""​
 +</​code>​
 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.\\ \\  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.\\ \\ 
 ==== Specify it relative to @lib$ ==== ==== Specify it relative to @lib$ ====
 \\  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:\\ \\  \\  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:\\ \\ 
 +<code bb4w>
         BMPfile$ = @lib$+"​mylib\image.bmp"​         BMPfile$ = @lib$+"​mylib\image.bmp"​
         OSCLI "​DISPLAY """​+BMPfile$+""""​         OSCLI "​DISPLAY """​+BMPfile$+""""​
 +</​code>​
 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.\\ \\  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.\\ \\ 
 ==== Obtain it from the Windows API ==== ==== Obtain it from the Windows API ====
 \\  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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​opensave|GetOpenFileName]] API function. Another would be accessing a file in the temporary folder:\\ \\  \\  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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​opensave|GetOpenFileName]] API function. Another would be accessing a file in the temporary folder:\\ \\ 
 +<code bb4w>
         tempfile$ = FNtemppath+"​\myfile.tmp"​         tempfile$ = FNtemppath+"​\myfile.tmp"​
         outfile% = OPENOUT(tempfile$)         outfile% = OPENOUT(tempfile$)
 +</​code>​
 where **FNtemppath** is defined [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​specialfolders|here]].\\ \\  where **FNtemppath** is defined [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​specialfolders|here]].\\ \\ 
 ===== Using the current directory ===== ===== Using the current directory =====
 \\  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. \\  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.
when_20and_20when_20not_20to_20use_20_2acd.txt · Last modified: 2018/04/17 19:28 by tbest3112