Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:59 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Is there any way to print DIR$ sorted?

Author Message
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 11:37am 25 Jul 2025
Copy link to clipboard 
Print this post

Hello all, I'm spending the whole day wrapping my head around this problem.

I am using the latest PicoMiteVGA Firmware.

the Files command allows to list the files sorted by name, for example. But this command isn't available in the program itself. BUT there is the DIR$ command.

It does just what I want, but with a big problem, it ALWAYS sort the DIRs by the date or time of creation. I've tested this many times with folders. To make things clear, I've freshly created folders one after one with this scheme:

first I created folder named "1"
then ... "7"
..10
..22
..5

  Quote  there is no way to sort the printing like:
1
5
7
10
22


Background:

I trying to save a listing in an array like this:


Dim String directory(200), fileName
Dim Integer foundFiles

f$=Dir$("*",Dir)
Do While f$ <> ""
   Inc foundFiles
   fileName= f$
   directory(foundFiles)=fileName
   f$=Dir$()
Loop

'print the array
For n=1 To 30
 Print directory(n)
Next n

Edited 2025-07-25 21:41 by Amnesie
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 11:51am 25 Jul 2025
Copy link to clipboard 
Print this post

Hi Daniel,
but there is a SORT command. You'll learn how to use it in FM (@javavi).
Or look at the old Klingon FM (for VT100), where it's done a little differently.
Regards
Michael

The central procedure was called

Sub GetFList(


by Vadim.
I think you can do it!
Edited 2025-07-25 22:23 by twofingers
causality ≠ correlation ≠ coincidence
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 12:36pm 25 Jul 2025
Copy link to clipboard 
Print this post

Hey, I can't thank you enough for pointing me in this direction!

I've tried to isolate the code snipped and it works perfectly! Now there is only one task left: to UNDERSTAND it  , but with the help of the manual and the SORT command, I think this is doable.  

For everyone who may need this code snippet:


Dim string  TMP$
Const RMAX=199  'Max number of Records in File List (255 max)
Dim string  FList$(RMAX)  LENGTH 63
Dim integer RQt,DQt,FQt,S,i

TMP$=Dir$("*",DIR)
Do While TMP$<>""
 If RQt=(RMax-1) Then Exit Do
 Inc RQt: Inc DQt
 FList$(RQt)="/"+TMP$
 TMP$=Dir$()
Loop

Sort FList$(),,2,1,RQt

For n=1 To 30  'change here for max listing...
 Print FList$(n)
Next n


Thank you again for this fast response! This helped me a lot!

Greetings
Daniel
Edited 2025-07-25 22:37 by Amnesie
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 12:49pm 25 Jul 2025
Copy link to clipboard 
Print this post

  Amnesie said  ... I've tried to isolate the code snipped and it works perfectly! ..

Really? That shouldn't work! Something's missing! I think—at a quick glance—you're only reading directories and not sorting yet.
But I don't want to complain if that's enough for you.
Michael

Hint:
To sort the file properties, you need a numeric index(rMax) array.
Edited 2025-07-25 22:52 by twofingers
causality ≠ correlation ≠ coincidence
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 01:04pm 25 Jul 2025
Copy link to clipboard 
Print this post

  twofingers said  
Really? That shouldn't work! Something's missing! I think—at a quick glance—you're only reading directories and not sorting yet.
But I don't want to complain if that's enough for you.
Michael
Hint:
To sort the file properties, you need a numeric index(rMax) array.


Uhmm... I don't know but it works. I tried it with different folders.
I thought:

Sort FList$(),,2,1,RQt


does the sorting... And the printing of the array does give back a sorted array of strings...    


For n=1 To 30  'change here for max listing...
Print FList$(n)
Next n


This gives me a perfectly sorted list... What I am missing? May I have found a bug (to my favour?)  

Greetings
Daniel
Edited 2025-07-25 23:08 by Amnesie
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 01:16pm 25 Jul 2025
Copy link to clipboard 
Print this post

  Amnesie said  Uhmm... I don't know but it works. I tried it with different folders.

Actually, reading the file list works in two steps:
1. Reading directories.
2. Reading file names.
I haven't tested why it seems to work for you.

  Quote  ... This gives me a perfectly sorted list... What I am missing?  ...

To sort the file properties, you need a numeric index(rMax) array.
And the corresponding numeric arrays with the file properties.
I assumed you also wanted to be able to sort by file size and date.
Michael
causality ≠ correlation ≠ coincidence
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 01:22pm 25 Jul 2025
Copy link to clipboard 
Print this post

Ah, maybe here comes the missunderstanding!

I am not interested in sorting file properties at all. I am only interested in the name of the folder (or file).

I just wanted a sorted list of strings, If you will. But thanks for this explanation!

I am working on a "journal / diary" application with graph stats and so on... this is why I was in need of sorting it and don't care about file properties:






Greetings
Daniel
Edited 2025-07-25 23:35 by Amnesie
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 01:41pm 25 Jul 2025
Copy link to clipboard 
Print this post

Hi Daniel,

sehr schön! Very nice!

Why I'm surprised: normally,
TMP$=Dir$("*",DIR)
should only read folders.
To read files (names), you need
TMP$=Dir$("*",FILE)
.

This just in case your method doesn't work.
Edited 2025-07-25 23:55 by twofingers
causality ≠ correlation ≠ coincidence
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 03:01pm 25 Jul 2025
Copy link to clipboard 
Print this post

Ah! Now I completly understand what you mean!



TMP$=Dir$("*",DIR)
should only read folders.

Yes, yes it does! It only reads folders.

What I initially wanted to say is the following:

Thanks to your hint with Vadims FM, I sucessfully cut out the code snippet and it works AS is in this example (only for DIR, of course). But I implemented it already in my program, the crucial hint from you, was the SORT command I wasn't aware of. I already adapted it in my program, since it was only one line to add!

I am completely aware of the fact, that there are three parameters: DIR, FILE, ALL

Typischer Fall von einander vorbeigeredet würde ich sagen   Ich danke dir vielmals - absolut entscheidender Hinweis von dir!


Greetings
Daniel
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 03:24pm 25 Jul 2025
Copy link to clipboard 
Print this post

  Amnesie said  ... Typischer Fall von einander vorbeigeredet würde ich sagen (Typical cases of talking past each other, I'd say.) ...


Yes, if I had understood correctly right away (no need to sort the file properties), then I could have given you simpler examples, like the early versions of FM or my NSA .

Something else:
I was just thinking today (last night) whether we shouldn’t have a central thread with examples for every command/function that go beyond the manual’s explanation. It doesn’t need to be sorted at first, and could later be incorporated into the FOTS. I believe there are complex, very useful functions that users avoid because their usage isn’t obvious. Examples from my subjective point of view: Dir$(), many of the MATH functions, Play Sound 1,,U,, etc.

Michael
causality ≠ correlation ≠ coincidence
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 03:58pm 25 Jul 2025
Copy link to clipboard 
Print this post

  twofingers said  

Something else:
I was just thinking today (last night) whether we shouldn’t have a central thread with examples for every command/function that go beyond the manual’s explanation. It doesn’t need to be sorted at first, and could later be incorporated into the FOTS. I believe there are complex, very useful functions that users avoid because their usage isn’t obvious. Examples from my subjective point of view: Dir$(), many of the MATH functions, Play Sound 1,,U,, etc.
Michael


Hi Michael,

I am also working on this and have created same examples myself. The biggest one is how to use the line command for drawing an autoadjustable and autoscaling graph (which I needed for my geiger counter). I already worte a whole explanation of this - but at this point in time it is only german language. I still have to translate it. Also some Math examples I tested and wrote example programs for it (and found bugs, which Peter fixed).

If you are curious, here is at least one whole complex example:


Linie bis Liniendiagramm.pdf

Be warned, this is a WHOLE complex example on 19 pages! I have more of those writte. But as said, have to translate them and error-check them again.

Greetings
Daniel
Edited 2025-07-26 01:59 by Amnesie
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5091
Posted: 04:01pm 25 Jul 2025
Copy link to clipboard 
Print this post

Michael,

+1

Some of my programs make use of MATH array functions, to replace nested for-next loops. These could educate others. Like use of math array multiplication to calculate linear interpolated values (cpnvert 16x12 thermal camera to a 63x47 pixel picture).

Another example is the use of the math standard deviation funtion that is mathmatically identical to calculating the rms value of an array of samples.

Smart things, speed up interpreted basic.

Volhout
Edited 2025-07-26 02:08 by Volhout
PicomiteVGA PETSCII ROBOTS
 
javavi

Guru

Joined: 01/10/2023
Location: Ukraine
Posts: 507
Posted: 04:13pm 25 Jul 2025
Copy link to clipboard 
Print this post

Hello all,
 The SORT command is wonderful, it sorts everything quickly and even creates an index array of sort order.
It's just a pity that there is no corresponding command in MMBASIC that could sort an array by this index array.
 At the same time, so that it does not use additional memory, such as an intermediate array for copying. In my FM program, I had to invent an algorithm for laying out lines in a list by index array myself, since there was limited RAM. But it is clear that this algorithm in MMBASIC works slower than it could work if it were in the form of a built-in BASIC command.
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 675
Posted: 04:23pm 25 Jul 2025
Copy link to clipboard 
Print this post

  Volhout said  Michael,
+1
Some of my programs make use of MATH array functions, to replace nested for-next loops. These could educate others.
Volhout


Harm,

  Quote  
These could educate others.


100% agreed. I am not that great in programming BASIC and always in the need of help to understand the examples in the manual (I do always try to read / search the manual first) but sometimes there are commands which are not that trivial to understand or to implement; there is often more needed to "show" how things work in MMBASIC. In this community there are a lot of people who have decades of knowledge in BASIC (and other programming lang.). I am relativly "new" to this and came from C and Python.

If I see some of your complex works like the thermal camera or the mains monitor with FFT involved or even interpolation, my brain just shuts up. It is impressive what can be done. There is so much to lern. I also try to document my learning process by saving and creating my examples and write some pages about it. Not something like interpolation or FFT, but simpler things   ... But I have to translate things to english.

I would LOVE to see some examples database of ALL functions / commands. This would help a lot.

Greetings
Daniel
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025