This routine gets the next file out of a directory, without tying the rest of your program into the directory processing loop.
The demo part of the program below is just to demonstrate how the routine can follow more than one thread in parallel. (Of course, the GLObal variables dirpos and dirlen could simply be added as parameters to the two main routines instead.)
The NextFile presented here is merely a skeleton that can be the basis for many different needs. For example one could add conditions for only returning the names of files with certain extensions - or other criteria. The same goes for the information returned: Perhaps the file length is wanted, or the type, etc.
This version also does not drill down into sub directories. That requires a somewhat different design. (I used a GLObal stack for the channel numbers.. See NextFileDir for one way of doing that.)
SuperBASIC users will have to alter the r.xx variables. (I use these as a convention to indicate that these are merely return parameters.) Note also that the directories used in this demo dont exist, so youll have to alter the code to point to some real directories or fake ram1_d1_ and ram1_d2_ to test.
10 cw0 = 0: cw1 = 1: cw2 = 2: FOR c = 0 TO 2: CLS#c 11 : 12 ch1 = Init('ram1_d1_', dl1, dp1): ERT ch1 13 ch2 = Init('ram1_d2_', dl2, dp2) 14 IF ch2 < 0: CLOSE#ch1: ERT ch2 15 : 16 er1 = 0: er2 = 0 17 REPeat lp 18 IF er1 AND er2: EXIT lp 19 IF NOT er1 THEN 20 er1 = NextThread(ch1, dl1, dp1) 21 IF NOT er1 THEN 22 PRINT#cw1; fnm$\, DATE$(dt) 23 ELSE 24 PRINT#cw0; 'Thread 1'! er1 25 END IF 26 END IF 27 : 28 IF NOT er2 THEN 29 er2 = NextThread(ch2, dl2, dp2) 30 IF NOT er2 THEN 31 PRINT#cw2; fnm$\, DATE$(dt) 32 ELSE 33 PRINT#cw0; 'Thread 2'! er2 34 END IF 35 END IF 36 END REPeat lp 37 : 38 DEFine FuNction Init(dir$, dirlen, dirpos) 39 rem Updates parameters dirlen and dirpos 40 RETurn DirInit(dir$) 41 END DEFine Init 42 : 43 DEFine FuNction NextThread(ch, dirlen, dirpos) 44 rem GLObal fnm$, dt 45 rem Updates parameter dirpos 46 RETurn NextFile(ch, fnm$, dt) 47 END DEFine NextThread 48 : 49 : 100 REMark + ------------------------------------------------------------------------ + 101 REMark |< Next File >| 102 REMark + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 103 REMark | Get the next file in a directory. | 104 REMark | The routine must have been initiated by DirInit first. It is possible | 105 REMark | to have more than one "process" using the utility at the same time, | 106 REMark | provided each process maintains it own LOCal channel, and dirpos & | 107 REMark | dirlen variables. | 108 REMark + ------------------------------------------------------------------------ + 109 REMark | V0.01, pjw, 1gg5 | 110 REMark | V0.10, pjw, November 16th 2017, one level only | 111 REMark | V0.11, pjw, 2021 Oct 04, tweak | 112 REMark + ------------------------------------------------------------------------ + 113 : 114 DEFine FuNction NextFile(ch, r.nm$, r.dt) 115 LOCal lp, t% 116 REMark GLObal dirlen, dirpos 117 : 118 REPeat lp 119 dirpos = dirpos + 64: IF dirpos >= dirlen: CLOSE#ch: RETurn -10 120 BGET#ch\ dirpos + 5, t% 121 IF t% = 255: NEXT lp 122 GET#ch\ dirpos + 14, r.nm$ 123 IF LEN(r.nm$) > 0: EXIT lp 124 END REPeat lp 125 LGET#ch\ dirpos + 52, r.dt 126 RETurn 0 127 END DEFine NextFile 128 : 129 DEFine FuNction DirInit(dir$) 130 LOCal ch 131 REMark GLObal dirlen, dirpos 132 : 133 ch = FOP_DIR(dir$): IF ch < 0: RETurn ch 134 dirlen = FLEN(#ch): dirpos = -64 135 RETurn ch 136 END DEFine DirInit 137 : 138 :