88. Auto-loading of disc files ~~~~~~~~~~~~~~~~~~~~~~~~~~ When using programs that save and load data files frequently, care has to be taken to load the most recently saved file. If files are saved with numbers as filenames which are increased by one each time, the procedure will load the file with the highest number. The example below assumes that the highest possible number is 10, but the lowest can be 0 or 1. LISTING-1: 100 file%=11:REPEAT:file%=file%-1:REM start with highest possible plus 1. 110 chan%=OPENIN(STR$(file%)):REM Use OPENUP on Basic 2. 120 UNTIL chan%<>0 OR file%=0 130 IF chan% THEN 200 140 PRINT"No file found":*CAT 150 STOP:REM This bit is up to you. 200 PRINT"Found file number ";file% 210 REM INPUT#chan% can now be used in normal way. 220 CLOSE#chan%:REM Tidy up afterwards. 230 REM Rest of the program. LISTING-2: 100 FOR file%=0 TO 9:REM Save dummy files to disc. 110 chan%=OPENOUT(STR$(file%)) 120 CLOSE#chan%:NEXT In listing 1, Line 110 converts file% to a string and attempts to open a file of that name on the disc for input. A number is allocated to chan% for this operation, but this is 0 if the file is not found. The program loops between lines 100 and 120 until either chan% is not 0 or file% has reached 0. To test this, write some dummy files to disc as shown in listing 2, and then try the example in listing 1. Rather than have just numbers as the filenames, you could save files as "data1", "data2" etc., by modifying the (STR$(file%)) to ("data"+STR$(file%)). Note that this technique can be adapted to automatically load a series of files in turn, performing a search or other operation on each one.