User Tools

Site Tools


listing_20the_20contents_20of_20a_20directory

Differences

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

Link to this comparison view

listing_20the_20contents_20of_20a_20directory [2018/03/31 13:19]
127.0.0.1 external edit
listing_20the_20contents_20of_20a_20directory [2018/04/17 16:56] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, March 2010//\\ \\  The main //BBC BASIC for Windows// documentation explains how to [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​dirlist|list the contents of a disk directory]] and more detail is provided in the article [[/​Scanning%20a%20Directory%20%28Reading%20Directory%20Entries%29|Scanning a Directory (Reading Directory Entries)]], but both require the code to be adapted to the needs of the programmer. Some dialects of BASIC provide a built-in means of extracting the contents of a directory to a string array, for example Liberty Basic'​s **FILES** statement; this can be a more straightforward approach and avoids the need to work directly with the Windows API.\\ \\  The function **FNfiles** listed below provides a similar capability in BBC BASIC. It takes two parameters, a **filter** string to determine the directory (folder) and files to be scanned, and an array in which to return the results, for example:​\\ ​ //by Richard Russell, March 2010//\\ \\  The main //BBC BASIC for Windows// documentation explains how to [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwine.html#​dirlist|list the contents of a disk directory]] and more detail is provided in the article [[/​Scanning%20a%20Directory%20%28Reading%20Directory%20Entries%29|Scanning a Directory (Reading Directory Entries)]], but both require the code to be adapted to the needs of the programmer. Some dialects of BASIC provide a built-in means of extracting the contents of a directory to a string array, for example Liberty Basic'​s **FILES** statement; this can be a more straightforward approach and avoids the need to work directly with the Windows API.\\ \\  The function **FNfiles** listed below provides a similar capability in BBC BASIC. It takes two parameters, a **filter** string to determine the directory (folder) and files to be scanned, and an array in which to return the results, for example:​\\ ​
 +<code bb4w>
         nfiles% = FNfiles("​C:​\Windows\*.*",​ file$())         nfiles% = FNfiles("​C:​\Windows\*.*",​ file$())
 +</​code>​
 Here the array **file$()** is filled with a list of all the files and sub-directories in **C:​\Windows\** (starting at index zero), and the total number of entries is returned from the function. For each file or directory four items of information are returned as follows:​\\ ​ Here the array **file$()** is filled with a list of all the files and sub-directories in **C:​\Windows\** (starting at index zero), and the total number of entries is returned from the function. For each file or directory four items of information are returned as follows:​\\ ​
  
Line 17: Line 19:
 | 20\\ | Archive\\ | | 20\\ | Archive\\ |
  If necessary the array will be created by the function; it is not essential to DIMension it yourself. However if the array already exists, but is not big enough to contain the data, it will be discarded and a new array created. Since this will result in memory being lost, if you intend to call the function multiple times it is better to create an array big enough to hold the largest amount of data you are likely to need, for example:​\\ ​  If necessary the array will be created by the function; it is not essential to DIMension it yourself. However if the array already exists, but is not big enough to contain the data, it will be discarded and a new array created. Since this will result in memory being lost, if you intend to call the function multiple times it is better to create an array big enough to hold the largest amount of data you are likely to need, for example:​\\ ​
 +<code bb4w>
         DIM file$(1000,​3)         DIM file$(1000,​3)
         nfiles% = FNfiles("​C:​\Windows\*.*",​ file$())         nfiles% = FNfiles("​C:​\Windows\*.*",​ file$())
 +</​code>​
 If you want to list only a certain type of file, you can specify the filter string accordingly:​\\ ​ If you want to list only a certain type of file, you can specify the filter string accordingly:​\\ ​
 +<code bb4w>
         nfiles% = FNfiles("​C:​\Program Files\BBC BASIC for Windows\*.exe",​ file$())         nfiles% = FNfiles("​C:​\Program Files\BBC BASIC for Windows\*.exe",​ file$())
 +</​code>​
 The function may also be used as a means of detecting the existence of a file:​\\ ​ The function may also be used as a means of detecting the existence of a file:​\\ ​
 +<code bb4w>
         exists% = FNfiles("​C:​\pagefile.sys",​ file$())         exists% = FNfiles("​C:​\pagefile.sys",​ file$())
 +</​code>​
 This will return **1** if the file exists and 0 otherwise.\\ \\  Here is the function itself:​\\ ​ This will return **1** if the file exists and 0 otherwise.\\ \\  Here is the function itself:​\\ ​
 +<code bb4w>
         DEF FNfiles(filter$,​ RETURN array$())         DEF FNfiles(filter$,​ RETURN array$())
         LOCAL @%, D%, H%, I%, N%, P%, R%, S%, T%, size#         LOCAL @%, D%, H%, I%, N%, P%, R%, S%, T%, size#
Line 56: Line 65:
         NEXT P%         NEXT P%
         = I%         = I%
 +</​code>​
listing_20the_20contents_20of_20a_20directory.txt ยท Last modified: 2018/04/17 16:56 by tbest3112