Read files in directory?

Blitz3D Forums/Blitz3D Beginners Area/Read files in directory?

Guy Fawkes(Posted 2013) [#1]
Hi all. I was working on my project as usual & everything was going well. Until I needed a function which would grab all the names of files & their full directories into the same function return variable and then returning it via a function.

I need to be able to grab from folder "X", file(s) "10293.x", "10294.x", etc, and it must grab their FULL directories as well, so "10293" becomes "C:\Users\X\Desktop\10293.x" & so on until it has read through ONLY once, ALL the files in the directory, "meshes".

Here's the code:



If you need me to explain further, just ask me and I will do my best to go a bit deeper into what I need help with.

Thank you guys!


virtlands(Posted 2013) [#2]
Hi Thundros,

Here's a redo, in case you like...
Will use a function List_X_Files( x_path$, ".x" )




Guy Fawkes(Posted 2013) [#3]
Hi, @virtlands. Thank you, this is AMAZING! There's only 1 small problem though. I would like the ability to use a Return function and return the array of results kinda like PHP does.

What I mean is, I would like to return:



Like that.

If you can do that for me, I would hug you. LOL

Thanks, @virtlands!


virtlands(Posted 2013) [#4]
Hmmm, are you trying to . . .

(A:) Return an array of strings ? (or, return a pointer to an array of strings ?)
(B:) Allow the function to return 1 string at a time ?


Guy Fawkes(Posted 2013) [#5]
I need to do (A).


virtlands(Posted 2013) [#6]
I'm not sure,
but I doubt it's possible to return an array of strings from a Blitz3d function.
That might be possible to do in 'C' instead.

Were you considering the possibility of concatenating all found
files into 1 huge string ? (and then returning that 1 string?)
[ I haven't considered that possibility yet; It's too unusual. ]


What follows is another recoding.
The purpose here is to return arbitrary collections of string data,
by way of indexes.



Thanks for the virtual hug.


Guy Fawkes(Posted 2013) [#7]
No problem, bro. You earned that hug! :P


Guy Fawkes(Posted 2013) [#8]
Here ya go, I reformatted the source code so that it's easier to understand (Mainly changed spaces to tabs) :P

Credit: Virtlands

==Return array values from functions==

ReturnArray.bb:



EDIT:

- Added a recursive folder / file counter.
- Kept hard way of getting file count as well as easy way.
- See comments in code.



Thanks once again, Virtlands! :D

Mythros


virtlands(Posted 2013) [#9]
Aha! Well, we both overlooked some things.

I forgot to tell you to delete the following remnant which is not needed
from the code [directly above].

Global A_max = 9999999
Dim A$(A_max)
--------------------------------------------------------------

Furthermore, the following is an essential part of the code,

;; .. Start with about 9999999 items.
Global ssmax = 9999999

Except for some things,

(a) Why do you need nearly 10 million file positions ?

(b) The program already has an automatic re-dimension function
that detects when its array is nearly maxed out, to add about 100 more entries.

(c) The Dim sstack$(ssmax) command consumes memory immediately,
at the rate of 4 bytes per string entry,
which means that 10 million string allocations consumes
a total of 40 megabytes, even for empty strings.

That's why I started off with a low value, such as 32.
32 empty strings = 128 bytes.

-------------------------------------------------------------------------

Here is another variation for you.
IN this case, the function returns 1 superstring.

S = List_X_Files_s( x_path, ".x" ) ;;

(String data is separated by a 'separator' character, such as "*" . )

A special routine deconstructs the string (S$) back into human readable data ::



This would perhaps solve your wish that a "string" be returned.
--------

Update: I found your recursive "ScanDir( )" addition.
It's an interesting strategy.
Will study it further to see how it performs.


Guy Fawkes(Posted 2013) [#10]
Thanks alot, Virtlands! Kudos to you, and another hug! :P

I have once again made the above code by Virtlands, easier to read.

ReturnSuperString.bb:



Thanks again!

Mythros