How to read filenames of a dir without limitation

Posted By: Bone

How to read filenames of a dir without limitation - 09/09/12 09:19

Hello,

I want to read the filenames of files in a specified dir.
I tried txt_for_dir and it works but I have to set
a number of strings for the TEXT Object.
I want to get all filenames but the number of files
is not fixed.
Is there any other function that I can use?
Hope someone can help me
Posted By: EvilSOB

Re: How to read filenames of a dir without limitation - 09/09/12 10:02

Try this

Click to reveal..
Code:
//--------------------------------------------------------------------
var txt_for_bigdir(TEXT* txt, STRING* name)
{	if((!txt)||(!name))		return(0);	//invalid parameter/s
	//-----------------------------------------------------------------
	var i, old_len=txt.strings, count;	
	// Automatically flush out all old populated strings
	for(i=0; i<txt.strings; i++)			if((txt.pstring)[i])		
	{	str_remove((txt.pstring)[i]);		(txt.pstring)[i]=0;		}	
	// retrieve directory and verify we got it ALL
	while((count=txt_for_dir(txt,name))==old_len)	//if too small?
	{	//clear strings again AND double TXT.strings size at the time
		for(i=0; i<old_len;	i++)	{	txt_addstring(txt, NULL);
			if((txt.pstring)[i])		{	str_remove((txt.pstring)[i]);
												(txt.pstring)[i]=0;				}	}
		old_len = txt.strings;														}
	//-----------------------------------------------------------------
	return(count);
}
//--------------------------------------------------------------------



Use it in EXACTLTY the same way as you would use txt_for_dir.
But if the TXT is too small it gets auto-extended, and any existing
strings in the array will be wiped clean.

Warning! If it DOES get extended, or the TXT you supplied was too long,
there MAY be unused strings at the end of the TXT.
EG: If you retrieve a directory of 13 items, the TXT may actually be 16 long.

Always rely on the VAR that is returned. It IS an accurate count of how many
filenames were retrieved.
Posted By: Bone

Re: How to read filenames of a dir without limitation - 09/09/12 11:22

Thank you, your code works well!
Posted By: EvilSOB

Re: How to read filenames of a dir without limitation - 09/09/12 13:59

No problems dude. I just dug it out of my archives...
© 2024 lite-C Forums