Fetch number of file in folder in SD card


#1

Hi all!

I'm working on a kind of rhythm machine based mainly on sample. I would like to be able to load Raw samples on different folders and not having to go inside the patch to mention the program how much raw file there is in a folder.

So I wanted to know if there is a method so that the patch will, at a certain point, fetch the number of file inside a certain folder and return it as an integer value.

Thanks!


#2

I made something like that for the table save& load modules (sss/table), but I'm not sure if they're uploaded as my folder isn't able to sync anymore. As long as the basic filename is the same (without the added index) it will check how many files, using that name, it has in that folder.


#3

Hey thanks for your reply. What is the name of the object you are referring to?
I can see your folder, but I'm not sure it is synchronized.


#4

oh oh, I can't find it in the list either... that means it's SOMEWHERE in my patches folder....
Don't have the time to look at it for now. But luckily I do still partly remember what I did.
I put part of the loading code into a loop. It's the part that checks if a file is there and then returns an error or not. If it doesn't return an error, it looks for the next index, if it does return an error, it knows it's at the end of the list and uses the last index that didn't return an error.


#5

Oh that's interesting, unfortunately, i'm not sure I can achieve that in code on my own, I'll have a look anyway and try to reverse engineer some part of the code. And if you find the object somewhere, do let me know!
thanks for your time


#6

what you know.. it was easier to find then I though :slight_smile:

check next unused filename on SDcard.axp (9.5 KB)


#7

though it seems it doesn't work right now, as there are some problems when using a table load module that is embedded.
I do think I got it to work at some point when I saved the module to my own folder. But I got to look into that to be sure.
I think there's some header file in the table folder that should be present in the folder that this module is saved to. As it's embedded, it isn't looking in the right folder for this file.
So if you save the module to a folder, go look into the table folder to see if there's a header file and copy that one to the folder you've saved the module to.


#8

Got it! I had to change the header to look for the ff.h file on the root of the patch.
I'll look into modifying it to do what I mentioned!


#9

Awesome, I managed to make it work by fiddling with the code. It's not very optimised yet ( a lot of the initial purpose of your object is still there although I don't need it) but it is outputing the right number of file in a certain folder!

thanks a lot!


#10

what exactly did you change to get it to work?


#11

For my use, i figured out I only need your custom "findFree" object.
The first thing I have done is to replace the header include with "ff.h".
Then I added an int32 outlet (called amount) and declared in the K-rate Code: "outlet_amount=cnt;".

I do not use any of the other inlet or outlet functions at the moment. I may try to delete the function I don't use to save Ram, but it may take me some time to understand what part of the code is relevant to my use and what part is not.


#12

I meant the header file, what exactly did you change and where?


#13

Not sure what you call the header file. the patch was not executing because of the path of the (chibios/ext/fatfs/src/ff.h) not being right.
So I managed to find the ff.h file in my LXR firmware folder, copied it into the root of my patch and told the object to find that file directly at the root.

This was enough for the object and the patch to execute and perform what I needed.


#14

Hello @SirSickSik

I have been trying to finish and clean a patch that I'm working on lately. I'm using your findFree object but i'd like to use only one instance of it to find the number of files in multiple folders.
Basically, i just need to have the "prefix" as an inlet instead of an attribute. I have been trying to use strcat in your code in such a way:

strcpy(&c[0],inlet_prefix);
strcat(&c[0],"000attr_suffix");
strcpy(&C[0],inlet_prefix);
strcat(&C[0],"000attr_suffix");

It was not working if I was putting this code inside the init code tab so i moved it to K-rate code tab (I deducted this while studying the concat objects).

The path is right if I output both the c and C strings but the "free" display dos not light up and my "amount" outlet that I added in your code (outlet_amount=cnt;) only outputs 0.

Do you have any idea what goes wrong here?

Also, for the sake of saving SRAM, I'd like to erase all other functions that i'm not using, could you point me to what chunks of code I can delete?

thanks a lot!

best


#15

oef, it's a bit hard to get this module changed because of the header-problem we mentioned earlier.
Had to do quite a search again for the project that I build this in.

Remember, that, to change this module, you cannot embed it. You first need to save it to a folder of your own (so not in my sss-folder, but I assume you've already done this).

So once it's in your own folder that also contains the header file, you'll need to change the code without embedding the module and overwrite/save the module after you've changed something.

As you only need the amount of files in a folder,you'ld only need this part of the K-rate code:

if(inlet_findFree&&!ntrig){
stop=0;
ntrig=1;
}
else if((!inlet_findFree)&&ntrig){ntrig=0;}

while(!stop){
int i = cnt;
int i0 = i/10;
C[offset+2] = '0'+i-10*i0;
i = i0; i0 = i/10;
C[offset+1] = '0'+i-10*i0;
i = i0; i0 = i/10;
C[offset+0] = '0'+i-10*i0;
FIL FileObject;
FRESULT err;
err = f_open(&FileObject, &C[0], FA_READ | FA_OPEN_EXISTING);
if (err == FR_OK) {cnt+=1;}
else if(err!=FR_OK){stop=1;}
}

indeed, the "cnt-1" will show you the last file that is in use (so "cnt" is the amount of samples, where the samples start at index 000 up to index cnt-1)

for changing the prefix into an inlet, I'll need to experimentate a bit.. not sure if I can get it to work yet..


#16

I think this should work. At least, when I used the save-file first a couple of times to save a file, it was able to tell me how many of these files were on the disk..

change the first part to this:

if(inlet_findFree&&!ntrig){
stop=0;
ntrig=1;

strcpy(&c[0],"inlet_prefix000attr_suffix");
offset = strlen("inlet_prefix");
strcpy(&C[0],"inlet_prefix000attr_suffix");

}
else if((!inlet_findFree)&&ntrig){ntrig=0;}

oh, no, forget it, this didn't work.. it saved as inlet_prefi000.tab... haha..of course


#17

hmmm... not really getting it to work with an external prefix inlet.
It's really tiresome to check this out as I have the unmount/mount my SDcard all the time to check whether it's creating the right file names.

It currently does seem to count the files, but not sure it's actually working correctly..
I've used your code to create the string, but made one change. I've added the part where it gets the offset.
Then the strcat uses this offset to see where it needs to add the 000suffix, as I think your version overwrites the prefix part..

strcpy(&c[0],inlet_prefix);
offset = strlen(inlet_prefix);
strcat(&c[offset],"000attr_suffix");
strcpy(&C[0],inlet_prefix);
strcat(&C[offset],"000attr_suffix");

#18

thanks a lot for your help!
I managed to make it work thanks to your examples.
Now the only problem is that when I change the prefix (the path of the folder to scrape) the amount of files only updates when it is higher than the previous one.
For an example, if a folder has 10 files in it, it will output 10, but if the nex one has less, like 5, the output will stay at 10.
Weird…


#19

you could try to "manually" reset the cnt-value to zero with an external trigger, perhaps this might fix it.
But also, could it be that it is still reading the old location or have you conformation that it actually switched folder?


#20

The normal way to do this (on a real OS) would be to use f_opendir() and f_readdir(). I'm pretty sure these exist in chibiOS.

Edit: confirmed, this works on Axoloti