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

scandir - distinguish files and dirs 1

Status
Not open for further replies.

scienzia

Programmer
Feb 21, 2002
160
IT
I use scandir to parse a directory's content.
How can I distinguish the files and the directories in the dirent structs?


Thanks in advance
 
In my code, I'm using readdir to get the listing from a directory. Not sure the difference, maybe you can tell me - I haven't looked at this in a while.

When you have the filename, you can run the stat command on it. After that, you can use the S_ISDIR macro as in:

#include <sys/stat.h>
...

struct stat status_buff;
...

stat_return = stat(incom_file, &status_buff);
if (stat_return == ERR_RETURN)
{
ERROR STUFF
}


if (S_ISDIR(status_buff.st_mode))
{
DIRECTORY CODE
}
else
{
FILE CODE
}

I hope that helps and does what you want. Let me know if something isn't quite right, but this works for me and has been in production for years. WARNING: The stat command can error off if the system resources are very heavily loaded - it happens occasionally on the HP boxes we run on.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top