Author |
Message |
Nick
Guru
Joined: 09/06/2011 Location: AustraliaPosts: 512 |
Posted: 10:29pm 04 Jul 2012 |
Copy link to clipboard |
Print this post |
|
I need to be able to read the contents of the SD card from within a running program and the DIR$() seems ideal...
... If I can get it to work!
Can anyone explain how this command works? I just get a null string.
Nick |
|
BobDevries
Senior Member
Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Posted: 11:03pm 04 Jul 2012 |
Copy link to clipboard |
Print this post |
|
Hi Nick.
I used the following code and got it to read ONE entry only.
a$=""
a$=dir$("*.*",FILE)
print a$
This does indeed read the current directory, but only returns the first entry, and *NOT* in the order that the FILES command does it (sorted). Using this:
a$=""
a$=dir$("*.*",DIR)
Print a$
produces a single "." (current directory pointer)
I think this *may* be a bug. I wonder if an array is required to be used somehow? (I did try that but no luck )
Geoff?
Regards,
Bob Devries Dalby, QLD, Australia |
|
BobDevries
Senior Member
Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Posted: 11:35pm 04 Jul 2012 |
Copy link to clipboard |
Print this post |
|
Nick, good news....
a$=""
a$=Dir$("*.*",FILE)
print a$
do
a$=Dir$()
if a$<>"" then print a$
loop until a$=""
I've come across this construct before... Just can't remember where.
Regards, Bob Devries Dalby, QLD, Australia |
|
Nick
Guru
Joined: 09/06/2011 Location: AustraliaPosts: 512 |
Posted: 11:41pm 04 Jul 2012 |
Copy link to clipboard |
Print this post |
|
Thanks Bob. Thinking of creating a simple file manager but need to be able to read the current files and structure.
I'll give it a tryout later tonight.
Nick |
|
BobDevries
Senior Member
Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Posted: 11:48pm 04 Jul 2012 |
Copy link to clipboard |
Print this post |
|
ok, so here's how I believe it works....
The use of DIR$("*.*",FILE) primes the pump, so to speak.
Then you keep repeating the function with empty brackets DIR$() until the returned result is a null-string.
Hope that helps.
Regards,
Bob Devries Dalby, QLD, Australia |
|
BobDevries
Senior Member
Joined: 08/06/2011 Location: AustraliaPosts: 266 |
Posted: 12:13am 05 Jul 2012 |
Copy link to clipboard |
Print this post |
|
Here's a more useful example:
a$=""
count=0
Rem this section counts the number of files in the directory
a$=Dir$("*.*",FILE)
Do While a$<>""
count=count+1
a$=Dir$()
Loop
Dim fn$(count) : Rem DIMension an array "on the fly"
fn$(0)=Dir$("*.*",FILE) : Rem put the filenames into an array
For x=1 To count
fn$(x)=Dir$()
Next x
Rem print the array members
For x=0 To count
Print fn$(x)
Next x
Hope that helps others also. I had fun nutting this out. I'm sure there's a C construct that's the same or similar... I'll think of it sometime.
Regards,
Bob Devries Dalby, QLD, Australia |
|
Nick
Guru
Joined: 09/06/2011 Location: AustraliaPosts: 512 |
Posted: 11:22am 05 Jul 2012 |
Copy link to clipboard |
Print this post |
|
Thanks again Bob.
That's all working fine. |
|
wizged Newbie
Joined: 05/02/2013 Location: AustraliaPosts: 13 |
Posted: 10:53pm 26 Mar 2013 |
Copy link to clipboard |
Print this post |
|
I am doing something similar to this in my current project but have an additional "challenge"
My basic directory list is working fine using DIR$, but I would really like the output to be sorted. My problem is that, while a following bubble sort is OK for a small number of files its too slow for a large number (150-odd random files is taking about 20 seconds to sort). I've started looking for a more efficient sort algorithm, but I'm also wondering if there's an easier answer that I've missed...
The FILES command is already doing everything that I want to do, and its fast, but I can't see any way to make the FILES output available within my program. Does anyone know if this is possible? Maybe if there is some way to write the output of FILES to a temporary file on SD somehow rather than to the screen I could then just read that file in?
|
|
MicroBlocks
Guru
Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Posted: 01:34am 27 Mar 2013 |
Copy link to clipboard |
Print this post |
|
If sorting takes too long it is probably only a little part that is consuming most of the time.
I guess swapping the strings could be it.
If that is the case you could have a numeric array with pointers to the strings in the string array. You then swap the pointers instead of the strings.
It adds some complexity but might be a solution to speed it up.
Microblocks. Build with logic. |
|