Fetch number of file in folder in SD card


#21

Yup, that did the trick, I added an inlet and the following code:
if ((inlet_reset>0) && !ntrig) {
ntrig = 1;
cnt = 0;
}

So basically each time I press the "change folder path" button, it triggers the reset, then a delayed pulse object triggers the Findfree inlet.

It seems to work well like this but it seems to need at least 100ms of delay beatween the triggers, otherwise it stays stuck at cnt=0.

Thanks a lot!

@tele_player
Oh great, I'm only reverse engineering the objects so coding from scratch is still out of reach at the moment, but i'll look into the commands you are refering to!


#22

Great to read that it works!!
Are you planning to share the object?
Sounds like a very useful thing :slight_smile:


#23

Hey, yes, I could share it here once I'm done cleaning the object.

At the moment it is working but I still need to resolve some trouble in my patch: I'm actually using it in some kind of drum machine. When I press a button, my patch is looking for a sample folder and the samples inside this folder are played/streamed directly to the output of the AXO.
But it seems that the Axo does not like having different operations on the SD card at the same time: when i change the folder and the custom object is browsing the files, there is a digital blip/glitch if a sample is being played at the same time.

I might have to ask the object to browse every folder at the start of the patch, before playing samples, and store the values of the number of files in each folder in tables so I can recall it while playing.


#24

if teleplayer is right about the f_opendir() and f_readdir() working on the axoloti, I'ld go that way...
my way was a work around as I didn't know these codes either, but they should be waaay faster.


#25

Try this:

filecount2.axp (1.8 KB)

The embedded object has an edit button, use it to enter the folder name; be sure to include the leading / .


#26

Hey great thanks a lot!
I'm away from my axo for a week, I'll test your object as soon as I get back.
For my use, i would need to have the folder directory as a string inlet.
I guess if I replace the attr_folder with an charptr32 inlet it would work?


#27

Yes, that’s a simple change. I just wanted to demonstrate the use of opendir and readdir, and how to use Init Code to do something just once.


#28

Yes, and it is of great help, I understand how it works now!


#29

Thanks so much!!! This is brilliant!! :pray:


#30

So I'm back at my Axoloti.
I can't seem to make your object work with a string inlet.
I believe your code is in the Init part so it will only run once?
What I would like to do is be able to read the number of file in a folder while being able to change the folder path dynamically.

I tried to fiddle with the code but i'm not good enough to make it work yet I guess.


#31

Specifically, how will user enter the folder name? Select from a list? A fixed list, or one that is generated at run time according the contents of the SD?


#32

@tele_player
I use the object from rbrt "choose" that outputs a constant string based on an input index. Basically, everytime I push a button, the path of a folder changes.
I'd like to take this path as a string and feed your object with it, that will in turn give me the number of file present in this folder.
Maybe I need a trigger input to tell your object to search the folder only once when a trigger is present?


#33

I found this function and remembered this question, just pasting here without having read the full thread - please excuse my ignorance. Hope it helps.

uint16_t getNumFilesInDir (char* path)
{
	FRESULT res;
	FILINFO fno;
	DIR dir;
	char *fn;
	static char lfn[_MAX_LFN + 1];
	fno.lfname = lfn;
	fno.lfsize = sizeof(lfn);
	int i = 0;

	res = f_opendir(&dir, path);                       /* Open the directory */
	if (res == FR_OK) {
		for (;;) {
			res = f_readdir(&dir, &fno);                   /* Read a directory item */
			if (res != FR_OK || fno.fname[0] == 0) break;  /* Break on error or end of dir */
			fn = *fno.lfname ? fno.lfname : fno.fname;

			if (!(fno.fattrib & AM_DIR))                   /* It is a file. */
				i++;
		}
	}
	return i;
}