dirent.h distinguishes files from directories ?


 
Thread Tools Search this Thread
Top Forums Programming dirent.h distinguishes files from directories ?
# 1  
Old 02-04-2010
dirent.h distinguishes files from directories ?

In C, I want to list all files recursively in a folder. But to do that, how can I distinguish files from directories ? (I realise I could use find in a shell script or use system("ls -R > file") but I want to use only C.)

Code:
DIR        *ptr;
struct dirent    *files;
ptr = opendir("path-to-directory");

while( (files = readdir(ptr)) )
{
    printf("%s\n", files->d_name);
}
closedir(ptr);


Last edited by cyler; 02-04-2010 at 06:21 PM..
# 2  
Old 02-04-2010
See man 2 stat.

Code:
{
        struct statbuf buf;
        if(stat("/etc", &buf) != 0)
                perror("couldn't stat");
        else if(S_ISDIR(buf.st_mode))
                printf("/etc is a directory\n");
        else
                printf("/etc is a file\n");
}

Don't be tempted to just check the d_type field of the dir entries. We've had people ask here when it suddenly stops working, it turns out some filesystems report filetype there and some don't.
# 3  
Old 02-05-2010
Also, look at "man ftw".
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

2. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

3. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. Shell Programming and Scripting

How to store files/directories in different directories!!

Hi legends, I am writing a script, in that my requirement is, if all the fill types stored in one directory from that we need to separate different different directories based on the file types. for example in a directory(anish). 5 different types files 1- directory 2- .txt files 2- .sh... (1 Reply)
Discussion started by: anishkumarv
1 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. Shell Programming and Scripting

mget * (obtein files from current directory but not the files form sub-directories)

Hello, Using the instruction mget (within ftp) and with "Interactive mode off", I want to get all files from directory (DirAA), but not the files in sub-directories. The files names don't follow any defined rule, so they can be just letters without (.) period Directory structure example: ... (0 Replies)
Discussion started by: Peter321
0 Replies

8. Programming

struct dirent variances

Hello, Recently I need to create a filesystem related code. I noticed lot of inconsistent documentation regarding struct dirent: 1. 'd_namlen' field info libc in chapter ''File System Interface" specifies 'd_namlen' field as length of 'd_name' string. When compiling under Linux (and GNU... (3 Replies)
Discussion started by: odys
3 Replies

9. Programming

dirent.h problem ..

hi...do opendir/readdir/closedir work also in windows platform? I tried to access directory name (d_name) but it didn't work out (or perhaps I made a mistake). Could someone share ideas about this? Thank's in advance. (3 Replies)
Discussion started by: qqq
3 Replies
Login or Register to Ask a Question