Recursive Directory Reader (Source Included)

BlitzMax Forums/BlitzMax Programming/Recursive Directory Reader (Source Included)

dw817(Posted 2016) [#1]


Still working on the Carryall 750k Method program, I have need for a recursive directory reader as the program will have the ability to contain and compress a directory and its inner files recursively.

While it might've been remarkably easy to do a DOS SHELL and use Windows' own routine to get a directory recursively, I decided to make use of BlitzMAX's own directory get ability - which hopefully means this is Macintosh friendly as that was the gripe with some of my code.

It's not perfect, for some reason BlitzMAX wants to read "." and ".." in directories which is not very useful, so I wrote the code to go around these.

Using the SORT ability it puts the directories on the top, just where they ought to be. :)

I don't know how to read the length of a TLIST so I'm using EACHIN. Fortunately this works. I still have a lot to learn - many thanks to those people who have helped me in my coding thus far.

UPDATED 02-01-16






Derron(Posted 2016) [#2]
Edit: removed content (forum did a double post on updating the post content)


Derron(Posted 2016) [#3]
local length:int = myList.Count()

Maybe you want to have a look at this:
https://github.com/GWRon/Dig/blob/master/base.util.directorytree.bmx

Samples how to use:
https://github.com/GWRon/Dig/blob/master/samples/directorytree/directorytree.bmx

I just extended the helper a bit to allow more convenient setting of options. Also I am aware that my code is longer than yours - but this comes with adding flexibility/configurability and convenience ("SetIncludeFileEndings()").


@"tricky !"
Just call this a "recursive function" and there will be plenty of people here on the forums saying this is no longer "tricky".
Imho directory traversal is one of the most prominent examples of recursive functions.


@your code
Have a look at this short reply of Mark Sibly. Your code might run into trouble on Mac OS (open directory limit).
-> you should close directories you do no longer need to keep "opened" (CloseDir(handle)).
I think even my Code should fail somewhen (as it closes a dir after finishing traversal of all sub-elements). Think at a depth of 255 subdirectories, this might then crash too.

Also you might be interested in what Grable wrote 9 years ago.
Or what Robert Knight posted in the code archives.


bye
Ron


TomToad(Posted 2016) [#4]
Don't know why you don't use LoadDir(). Solves both the dot problems and open directory problems.



Derron(Posted 2016) [#5]
It does solve the "open directory problem" the same way as I do: by closing handles.

Else: LoadDir() is of course another option (to avoid handling ".."/"." on your own).
Do not know if LoadDir() for many subDirs is less efficient than handling everything (including recursion) on your own - as each time an array for 100 strings is initialized.
Think it could be neglected and LoadDir might be a shortening alternative.

bye
Ron


dw817(Posted 2016) [#6]
I'm just overwhelmed by what I see. You guys are really on top of this. :)

Did not know (but now do):

1. Command LoadDir() ' loads the entire directory instead of one file at a time
2. MyList.Count() ' to retrieve the total number of items in a TLIST.
3. file$[]=LoadDir(path$) ' $[] gives infinite string ?
4. CloseDir(Handle)

As for tricky, Ron, recursion to me is a relatively new thing. I couldn't have it in AmigaBASIC, Floating Point Basic, Integer Basic, GWBasic, or TRS 80.

I think I could've done it in Turbo Pascal, but at the time I didn't know anything like it existed.

No, someone introduced me to the method years ago on QBasic with a program they wrote that drew mandelbrots.

Now as hot shot as you guys are, it's probably nothing to code for you, but to me recursion is a marvel of intelligent programming and definitely helped my coding here so it didn't double in size.

I'll try to reserve the word tricky for other curious methods outside of recursion.

I'll also use the LoadDir() method,Tom, as it skips over the "." and ".."

Now I just need to sit down and look closely at what you wrote so I have a clear understanding of it.


Derron(Posted 2016) [#7]
"files$[]" is equal to "files:string[]"

If you remember "int[]" being an array of integers (without a given "length"), "string[]" is an un-initialized array of strings.

"files:string[10]" is then an array of 10 strings.

As we do not know the length of the array "LoadDir" returns, we just use the "file:string[]" one.


@Mandelbrot
Yes, artificial "art" is often using recursive calls.

Without it would more than double the size (as you go deeper and deeper and deeper...).


Have fun "learning".


bye
Ron


TomToad(Posted 2016) [#8]
Many a year ago, I wrote a tutorial using recursion. http://www.blitzbasic.com/Community/posts.php?topic=80746


dw817(Posted 2016) [#9]
Tom, Chess is one of my favorite games. The challenge ! Place 8 Queens on a chessboard that cannot capture each other ? I think I can write that without recursion.

The question is, if I do, is it supposed to be possible without recursion ? I love doing impossible things. :)