Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you a directory list, and load the filenames into an array?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have looked everywhere, and nothing.
I found for_each_file(), but it doesnt seem to work, or I'm screwing up the syntax.

I thought scandir(), but its only for unix.

I'm using DOS (win98), long file names, and DJGPP as the compiler.

I am wanting to scan a Directory for all filenames ending in .m3u, the put the result in an array of say names[10][40], so I can call them up later.

It someone could show me the proper syntax, and some short sample code, I would be very thankful.

Scott
 
declare a char pointer

char *command="dir/b c:\directory>a.txt";
ststem(command);

the system command runs the commands that run on DOS prompt. the redirection operator redirects the o/p to the file a.txt. dir/b lists all files in a dir without any description.

include stdlib.h or dos.h to run this command.

I hope this works
regards
Nam
 
see thread205-117916 and thread205-116681 John Fill
1c.bmp


ivfmd@mail.md
 
Thank you all so very much, I don't think the windows.h example will work, as I will only be using the dos part of windows, with no gui running, but the system(command) looks promising,
I was kind of hoping for a cross-platform type of command, because I'd like to port it over to linux, but, without your help I'd have nothing at all.

Thank you guys.
Scott
 
You can use WinAPI without any windows or/and GUI. Just do not create windows. John Fill
1c.bmp


ivfmd@mail.md
 
Here is one possible solution using gcc. Fill in details where appropriate.

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

#define MAX 1000

int main()
{

DIR *dp;
struct dirent *dirp;
char *d[MAX];
int i, j;

if((dp = opendir(&quot;./&quot;)) == NULL) exit(1);

i = 0;
while((dirp = readdir(dp)) != NULL){
d = (char *) malloc(strlen(dirp->d_name)+1);
strcpy(d, dirp->d_name);
i++;
}

if(closedir(dp) < 0) exit(1);

printf(&quot;Directory Contents\n&quot;);
for(j = 0; j < i; j++) printf(&quot;%s\n&quot;,d[j]);

return 0;

}
 
you can try this one

Code:
#include <stdio.h>
#include <string.h>
#include <io.h>


void printFilesDir(char *mypath);

//FILE *fp;

int main(){


	//char mypath[]=&quot;c:\\&quot;;
	char *mypath;


	//fp=fopen(&quot;c:\\test.txt&quot;,&quot;w&quot;);

	mypath=strdup(&quot;c:\\&quot;);

	printFilesDir(mypath);


	//fclose(fp);

	return 0;

}



void printFilesDir(char *mypath){


	long hsearch;
	struct _finddata_t findinfo;
	long success;
	char *buffer;
	char strtmp[1000];
	char spec[]=&quot;*.*&quot;;
	char root[1000];

	strtmp[0]=0;



	strcpy(root,mypath);

	if( mypath[strlen(root)-1] != '\\')
		strcat(root,&quot;\\&quot;);


	sprintf(strtmp,&quot;%s%s&quot;,root,spec);


	hsearch= _findfirst(strtmp, &findinfo );

	if(hsearch == -1)
		return;


	do{
    	buffer = strdup(findinfo.name);

    	if( (strcmp(buffer,&quot;.&quot;)) != 0 && (strcmp(buffer,&quot;..&quot;)) != 0){


    		if(findinfo.attrib & _A_SUBDIR){

				strtmp[0]=0;
				sprintf(strtmp,&quot;%s%s&quot;,root,buffer);

        		printFilesDir(strtmp);

    		}else
        		fprintf(stdout,&quot;%s%s\n&quot;,root,buffer);
        		//fprintf(fp,&quot;%s%s\n&quot;,root,buffer);

		}

		success = _findnext(hsearch, &findinfo);
	}while(success == 0);


	_findclose(hsearch);

}

::)
bluenote@uyuyuy.com
(excuse my english)
 
You guys are making it too difficult, no offense.

system(&quot;dir /b *.m3u > m3udir.txt&quot;)

that will create a list of m3u files in the m3udir.txt file. You can then read each name out of the file and store them in your array respectively.

Armen
 
using system is a bad style of programming. Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top