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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting file listing of directory and subdirectories 1

Status
Not open for further replies.

tryfekid

Programmer
Aug 16, 2001
9
0
0
US
Hi,

I'm trying to get a file listing to be processed. the file listing will contain all the files in the current directory and subdirectory. i know how to get the file listing of the current directory but not all the subdirectories.

any help is apprectiated.

thanks.
 
Just something to think about ;-). Not fully tested!
You can try something like this ... (using C on Linux):

Output is like:
DIR: X
FILE: afile.c
FILE: bfile.c
.
.
.


Code:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

void read_directory(char *path);

main()
{
	char	path[1024];

	strcpy(path, &quot;/home/users/test/C_CPP&quot;);

	read_directory(path);

	return 0;

}

void read_directory(char *path)
{
	char	temp[1024];

	struct stat	statbuf;
	struct dirent	*d_entry;
	DIR		*directory;

	if(! (directory = opendir(path))) return;

	while( (d_entry = readdir(directory)) )
	{
		if(strcmp(d_entry->d_name,&quot;.&quot;) != 0 &&
			strcmp(d_entry->d_name,&quot;..&quot;) != 0 )
		{
			sprintf(&temp[0], &quot;%s/%s&quot;,
				path,d_entry->d_name);
			stat(temp, &statbuf);

			if(S_ISDIR(statbuf.st_mode))
			{
				printf(&quot;DIR :\t %s\n&quot;,
					d_entry->d_name);
				read_directory(temp);
			}
			else if(S_ISREG(statbuf.st_mode))
			{
				printf(&quot;FILE:\t%s\n&quot;,
					d_entry->d_name);
			}
		}
	}

	closedir(directory);
}
 
What you could do is :
#include <sys/wait.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

//goto the dir.
int result= execvp(&quot;cd /home/someuser&quot;,0);
char ch=' ';
File* file;
//open a pipe.
file=popen(&quot;ls -R -1&quot;,&quot;r&quot;);
//read the pipe.
while(m=fgetc(file))!=EOF)cout<<m;

Then all you have to do is parse the list,but if
you use ls -R -1, then the files are quasi there to grab for you. Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Hi try
muppeteer is right.
Here is my contribution to get just all the file names.
//open a pipe.
file=popen(&quot;ls -R -1|grep '^[^$\.\/]'&quot;,&quot;r&quot;);
'try' this out.
have fun.

vgg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top