Discussion:
Traversing a directory in BASIC...
(too old to reply)
u***@garethlock.com
2019-01-21 21:48:58 UTC
Permalink
I'm trying to write a routine that returns a list of items, both files and directories, in a given directory. Think I might have hit on something using OS_GBPB, but one has to supply the number of items to read. Been trying OS_FSControl 28 to return a count before calling OS_GBPB, can get this to count files, but not directories...

Has anyone come across a better way of doing this??
Gerald Holdsworth
2019-01-21 22:18:43 UTC
Permalink
Post by u***@garethlock.com
I'm trying to write a routine that returns a list of items, both files and directories, in a given directory. Think I might have hit on something using OS_GBPB, but one has to supply the number of items to read. Been trying OS_FSControl 28 to return a count before calling OS_GBPB, can get this to count files, but not directories...
Has anyone come across a better way of doing this??
Take a peek at:
http://www.reptonresourcepage.co.uk/Download.php?item=DecodingRepton.pdf
in the chapter "Useful Programs".

Here I've written a program, in BASIC, to iterate through a directory
tree and find a specific type of file. The actual code for doing this
was written, IIRC, by Alex Von Someron (of Aleph One) - but for some
reason I haven't credited him (oops...must correct that one).

I actually found it in a book I've got...somewhere!

You can also download the program here:
http://www.reptonresourcepage.co.uk/Download.php?item=Editcodes.zip

Cheers,

Gerald.
--
gerald at hollypops dot co dot uk
Repton Resource Page
www.reptonresourcepage.co.uk
Steve Fryatt
2019-01-21 22:44:48 UTC
Permalink
Post by u***@garethlock.com
I'm trying to write a routine that returns a list of items, both files and
directories, in a given directory. Think I might have hit on something
using OS_GBPB, but one has to supply the number of items to read. Been
trying OS_FSControl 28 to return a count before calling OS_GBPB, can get
this to count files, but not directories...
Has anyone come across a better way of doing this??
Isn't this one an FAQ by now?

OS_GBPB >= 9 returns telling you how many items have been read, and whether
there any any more items to read. You repeatedly call it, holding state
across calls, processing the results each time (IF ANY ARE RETURNED -- it's
quote valid for (next% <> -1) AND (read% = 0)) and continuing until it
reports no more results to come.

size% = 1024
DIM buffer% size%-1

next% = 0
read% = 0

REPEAT
SYS "OS_GBPB", 10, dir$, buffer%, objects_to_read%, next%, size%, 0
TO ,,,read%, next%

IF read% > 0 THEN
FOR item% = 0 TO read% - 1
REM Process the read items here
NEXT item%
ENDIF
UNTIL next% = -1

Do NOT assume that next% has any meaning beyond "-1" or "not -1".
--
Steve Fryatt - Leeds, England

http://www.stevefryatt.org.uk/
u***@garethlock.com
2019-01-21 23:02:49 UTC
Permalink
That's alright if you know the value of objects_to_read%. I seem to be coming a cropper when passing a nonsense number here when trying to read all the objects in the current directory whilst not knowing how many this is...
Steve Fryatt
2019-01-21 23:59:15 UTC
Permalink
Post by u***@garethlock.com
That's alright if you know the value of objects_to_read%. I seem to be
coming a cropper when passing a nonsense number here when trying to read
all the objects in the current directory whilst not knowing how many this
is...
objects_to_read% is a maximum. The call returns the number of items read in
the variable that I called read%, and that's what the IF statement was for:
you check how many items have come back, and process them (using a FOR loop
or whatever).

Rememeber that the call can return zero items even though there are more to
come. You muct check the number of items returned each time and skip if
there are none, but continue polling until next% is returned as -1.
--
Steve Fryatt - Leeds, England

http://www.stevefryatt.org.uk/
u***@garethlock.com
2019-01-22 02:12:38 UTC
Permalink
Just out of interest, what exactly would cause read% to return zero whilst next% <> -1 I.e there are still entries to read... Curiosity...
Matthew Phillips
2019-01-22 08:14:06 UTC
Permalink
Post by u***@garethlock.com
Just out of interest, what exactly would cause read% to return zero whilst
next% <> -1 I.e there are still entries to read... Curiosity...
It depends entirely on the implementation of the underlying file system which
may not be FileCore.

You should also not assume that the value in F4 increases by one each time
the information for a single file is read. Some applications come unstuck by
making this assumption, which does not hold true for FAT32FS, for example.

To give you an example of why a filing system might return no entries, let me
introduce you to the CP/M disc format, for which I wrote CPMFS. The CP/M
format was a predecessor of MSDOS and is the origin of the 8 + 3 (name +
extension) style of filename that persisted until Windows 95.

The directory on the disc is held in a block of a number of consecutive
sectors. Each directory entry is 32 bytes long. The first 16 bytes
contains a byte that can indicate the file is deleted, the filename, and a
directory entry count. The following 16 bytes point to the sectors on the
disc where the file is found.

The way I implemented CPMFS, R4 corresponds to the directory entry to read
next, so when you enter with R4 = 0 it reads the first entry. Suppose you
ask for four to be read each time. On exit R4 will equal 4, then 8, then 12,
and eventually will be set to -1 when there are no more to read.

But it's possible that all the first four directory entries are for deleted
files, in which case no results will be returned. Also, if a file is larger
than 16K it will require more than one directory entry to record the
allocated sectors, so any of the directory entries that handle the overflow
will not be reported when reading the files via OS_GBPB.

At least, that's one possible implementation. It's certainly how I handled
what R4 meant, but it may be that I just looped round until I had read the
number of objects requested in R3, and the exit value in R4 would have gone
up by more than four in these cases. I can't remember.

FAT32FS also implements R4 in a similar way. This means you cannot safely
take 1 off R4 to step back by 1 in the directory listing.

Another reason for returning zero in R3 might be if the filing system is
waiting for a network device to respond and has had to time out to keep the
machine responsive.
--
Matthew Phillips
Durham
Martin
2019-01-21 23:04:06 UTC
Permalink
On 21 Jan in article
Post by u***@garethlock.com
I'm trying to write a routine that returns a list of items, both
files and directories, in a given directory. Think I might have hit
on something using OS_GBPB, but one has to supply the number of
items to read. Been trying OS_FSControl 28 to return a count before
calling OS_GBPB, can get this to count files, but not directories...
Has anyone come across a better way of doing this??
Nope. OS_GBPB is the way to go.
The number of objects to read on entry is the *maximum* to read in
one go.
The actual number that has been read is returned, and it may be zero.
To go to the end of a directory you need to repeat until r4=-1.

A simple structure for the code can be seen in the OS StrongHelp
manual, if you do not have the PRMs.
--
Martin Avison
Note that unfortunately this email address will become invalid
without notice if (when) any spam is received.
u***@garethlock.com
2019-01-21 23:53:26 UTC
Permalink
Thanks to you all... I was already about half way there whilst messing around earlier. That little lot enabled me to get it working...
Alan Adams
2019-01-22 12:26:54 UTC
Permalink
Post by Martin
On 21 Jan in article
Post by u***@garethlock.com
I'm trying to write a routine that returns a list of items, both
files and directories, in a given directory. Think I might have hit
on something using OS_GBPB, but one has to supply the number of
items to read. Been trying OS_FSControl 28 to return a count before
calling OS_GBPB, can get this to count files, but not directories...
Has anyone come across a better way of doing this??
Nope. OS_GBPB is the way to go.
The number of objects to read on entry is the *maximum* to read in
one go.
So the simplest way for you to use this is to set the number to read to 1,
then you either get a file/directory, or nothing.
Post by Martin
The actual number that has been read is returned, and it may be zero.
To go to the end of a directory you need to repeat until r4=-1.
A simple structure for the code can be seen in the OS StrongHelp
manual, if you do not have the PRMs.
--
Alan Adams, from Northamptonshire
***@adamshome.org.uk
http://www.nckc.org.uk/
Martin
2019-01-22 16:30:40 UTC
Permalink
Post by Alan Adams
Post by Martin
On 21 Jan in article
Post by u***@garethlock.com
I'm trying to write a routine that returns a list of items, both
files and directories, in a given directory. Think I might have
hit on something using OS_GBPB, but one has to supply the number
of items to read. Been trying OS_FSControl 28 to return a count
before calling OS_GBPB, can get this to count files, but not
directories...
Has anyone come across a better way of doing this??
Nope. OS_GBPB is the way to go. The number of objects to read on
entry is the *maximum* to read in one go.
So the simplest way for you to use this is to set the number to
read to 1, then you either get a file/directory, or nothing.
That will work.

However, if you have a directory with lots of entries, it is better
to create a buffer that can take many entries so that the SWI is
called fewer times. Note that the Action you use (9, 10, 11 or 12)
dictates the amount of information returned, from just filename to 29
bytes+filename, so a 2048 byte buffer can hold from roughly 180 to 60
entries (assuming file names averaging 10 characters).

Note that if you specify a larger number to read than will fit in the
buffer then OS_GBPB is savvy enough to stop before the buffer is
overrun, so the number to read can be set high.
Post by Alan Adams
Post by Martin
The actual number that has been read is returned, and it may be
zero. To go to the end of a directory you need to repeat until
r4=-1.
A simple structure for the code can be seen in the OS StrongHelp
manual, if you do not have the PRMs.
That structure will just work. Anything else is very liable to cause
problems in some way or another.
--
Martin Avison
Note that unfortunately this email address will become invalid
without notice if (when) any spam is received.
j***@mdfs.net
2019-01-23 13:26:10 UTC
Permalink
The difficulty so many people seem to have in comprehending the
documentation for OSGBPB seems so prevalent, it's a fequently
frequently asked question, surely there's demo code somewhere?

http://beebwiki.mdfs.net/Scanning_Directories_(Reading_Directory_Entries)
Alan Adams
2019-01-23 15:01:12 UTC
Permalink
Post by j***@mdfs.net
The difficulty so many people seem to have in comprehending the
documentation for OSGBPB seems so prevalent, it's a fequently
frequently asked question, surely there's demo code somewhere?
http://beebwiki.mdfs.net/Scanning_Directories_(Reading_Directory_Entries)
The confusion is probably because OS_GBPB can do so many different things,
and each thing has options within it.

It won't be "an example", it's likely to be a booklet of them.
--
Alan Adams, from Northamptonshire
***@adamshome.org.uk
http://www.nckc.org.uk/
David Higton
2019-01-24 14:35:24 UTC
Permalink
Post by j***@mdfs.net
The difficulty so many people seem to have in comprehending the
documentation for OSGBPB seems so prevalent, it's a fequently
frequently asked question
The answer to the questions seems to boil down to:

RTFM.

Do what TFM says, not some other interpretation.

Dave
Richard Porter
2019-01-27 23:33:05 UTC
Permalink
Post by u***@garethlock.com
I'm trying to write a routine that returns a list of items, both files and
directories, in a given directory. Think I might have hit on something
using OS_GBPB, but one has to supply the number of items to read. Been
trying OS_FSControl 28 to return a count before calling OS_GBPB, can get
this to count files, but not directories...
Has anyone come across a better way of doing this??
If you have !SiteMatch you can copy the way it recursively scans a
directory. That's the one bit I haven't tampered with since I took it
over. much.

Loading...