Readdir() - folder or file?

Hi,

How can I distinguish between files and folders using readdir?

1
2
3
4
5
6
7
8
DIR Dir;
struct dirent *DirEntry;
Dir = opendir("c:/test/")

while(Dir=readdir(Dir))
{
   cout << DirEntry->d_name;
}


My code works so far, but i want to display only files and no folders.

Thank you in advance for any help! Emufarm
Last edited on
Hi Emufarm

Adding small change to the above you can print either only the folders or the file

If your code has to be such that it should display only the files in the directory then add the above stuff in your code

1
2
3
4
5
6
7
8
9
10
11
12
unsigned char isFile =0x8;
DIR Dir;
struct dirent *DirEntry;
Dir = opendir("c:/test/")

while(Dir=readdir(Dir))
{
   if ( DirEntry->d_type == isFile)
   {
	cout <<"Found a File : " << DirEntry->d_name << endl;
   }
}


If your code should only show the Folders in the corresponding directory then this code snippet would be appropriate
1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned char isFolder =0x4;
DIR Dir;
struct dirent *DirEntry;
Dir = opendir("c:/test/")

while(Dir=readdir(Dir))
{
   cout << DirEntry->d_name;
   if ( DirEntry->d_type == isFile)
   {
	cout <<"Found a Directory : " << DirEntry->d_name << endl;
   }
}

Last edited on
Topic archived. No new replies allowed.