subscript( ) example
The following example displays all the nonzero-size files in your Windows Temp directory. First it tries to find the directory where your temporary files are stored by looking for the operating system environment variable TMP. Then it uses dir( ) to store the file information for all the files in that directory (or the current directory if the TMP directory is not found) to the array aFiles. All the rows that have a file size of zero are deleted using a combination of scan( ), subscript( ), and delete( ).
scan( ) can simply search for zeros because there are no other numeric columns in the array created by dir( ). If it finds one, subscript( ) is called to return the corresponding row number for the matching element. Then the row number is used in the delete( ) call.
Manifest constants to represent the columns are created with the #define preprocessor directive to make the code more readable.
#define ARRAY_DIR_NAME 1 // Manifest constants for columns returned by dir( )
#define ARRAY_DIR_SIZE 2
#define ARRAY_DIR_DATE 3
#define ARRAY_DIR_TIME 4
#define ARRAY_DIR_ATTR 5
// Look for OS environment variable TMP
cTempDir = getenv( "TMP" )
// If defined, make sure it has trailing backslash
if "" # cTempDir
if right( cTempDir, 1 ) # "\" // No trailing backslash
cTempDir += "\" // so add one
endif
endif
aFiles = new Array( )
nFiles = aFiles.dir( cTempDir + "*.*" ) // Read all files in TMP dir
nElement = aFiles.scan( 0 )
do while nElement > 0 // Find zero-byte files and
aFiles.delete( aFiles.subscript( nElement, 1 ), 1 ) // delete by row
nFiles-- // Decrement file count
nElement := aFiles.scan( 0 )
enddo
for nFile = 1 to nFiles // Display results
? aFiles[ nFile, ARRAY_DIR_NAME ]
?? aFiles[ nFile, ARRAY_DIR_SIZE ] at 25
endfor