This article covers the basic use of the Small Basic Stack, with an example recursively finding all jpg image files in and below an initial directory.
A stack is an ordered list where entries are added (pushed) and retrieved (popped) in a last-in first-out order. The name stack come from the fact that it is like a stack of plates. When we add a plate it goes on the top and when we remove a plate it is
the last one added that is removed.
A stack is useful to store things that we want process gradually as we unwind the stack, retrieving things to process. Often the things to add to the stack are not known at the start, but are the result of processing previous stack values. If we knew them
all at the start we could just put them in an array.
Only three methods are present, they are:
The stackName is a string, that is something enclosed in double quotes, for example "my_stack". The value may be any Small Basic variable or value.
We want to create an array with all of the jpg image files in a directory and all sub-directories, searching all folders recursively.
First we create an empty array to store the image file paths and the number of files in it (zero to start with). We also set the starting folder to search and add it to a stack we call "folders". In this example the starting folder location is the folder
where the Small Basic source file is saved (Program.Directory).
Next we want to keep working while we have folders still to check, or the stack still has some folders in it. We use a while loop for this. Inside the while loop we want to process the last folder added, so it will be popped first.
' More work to do here
We now want to process this folder; first we get all the sub-folders in the current working folder and push these onto the stack for later checking as the while loop repeats.
Having added the sub-folders to check later as the stack unwinds, we find all the files in the current working folder and add ones ending in ".jpg" to the array list. We check the file by first converting to lower case, in order to include all case variants
of jpg JPG etc.
Finally we print out the results, and below is the whole thing.