NextFile

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 DelDir for one way of doing something like that.)

SuperBASIC users may have to alter the r.xx variables. (I use these as a convention to indicate that these are merely return parameters.)

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 rem + ------------------------------------------------------------------------ +
101 rem |<                               Next File                                >|
102 rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
103 rem | Get the next file in a directory.                                        |
104 rem | The routine must have been initiated by DirInit first. It is possible    |
105 rem | to have more than one "process" using the utility at the same time,      |
106 rem | provided each process maintains it own LOCal channel, and dirpos &       |
107 rem | dirlen variables.                                                        |
108 rem + ------------------------------------------------------------------------ +
109 rem | V0.01, pjw, 1gg5                                                         |
110 rem | V0.10, pjw, November 16th 2017, one level only                           |
111 rem + ------------------------------------------------------------------------ +
112 :
113 DEFine FuNction NextFile(ch, r.nm$, r.dt)
114 LOCal lp, t%
115 rem GLObal dirlen, dirpos
116 :
117 REPeat lp
118  dirpos = dirpos + 64: IF dirpos >= dirlen: CLOSE#ch: RETurn -10
119  GET#ch\ dirpos + 14, r.nm$
120  IF LEN(r.nm$) = 0: NEXT lp
121  BGET#ch\ dirpos + 5, t%
122  IF t% = 255: NEXT lp
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 rem 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 :
Generated with sb2htm on 2019 Mar 29
©pjwitte March 2oi9