Thread: Get list of just files

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    17

    Get list of just files

    I'm trying to find a way to get a list of only the files inside a directory. If that directory has other sub-directories in it, I don't want them in the list.

    I'm currently using scandir like this:

    char *pathname = "/var/";

    int count, i;
    struct direct **files;

    count = scandir(pathname, &files, file_select, alphasort);

    for (i=1; i<count+1; ++i)
    printf("%s\n", files[i-1]->d_name);

    I've looked around and can't find an easy way to check to see if an element in files is a directory or a file.

    Can anyone give me a suggestion?

    First time poster.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    There is no portable way to do it really. Depends on what OS you are using and such.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    17

    oops

    forgot that part.

    I'm using debian linux

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what does your file_select function do at the moment?

    It gets passed a pointer to a dirent, from which it should be easy to detect whether it's a directory or not.

    > for (i=1; i<count+1; ++i)
    So what's wrong with
    for (i=0; i<count; ++i)
    and removing the -1 inside the loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    17

    You're right

    I was focused more on the directory part and didn't realize I had a brain fart. I had copied some code I found online and hadn't really looked at it. - Thank you for not just laughing at me, but correcting me as well. I can deal with laughter. I can't stand my own laziness.

    So, how would you easily detect whether or not it's a directory. I've looking at doing something with a system call like "file *" or "ls -d */" and pipe it to a variable then split up the results then, but that sounds too ugly.

    My whole purpose for this is to run diff on every file, but not sub-directories against a "supposedly" duplicate file structure to look for any changes in files.

    Any pointers would be greatly appreciated.

    Thank you.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My pointer is to type "man dirent", and examine the d_type field of the structure.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    17

    Thank you

    I see now that in my sample code adding files[i]->file_type to my code will get me what I need. If it returns a 4, it's a directory.

    Thank you for smackin' me upside my head.

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    If it returns a 4, it's a directory.
    You might to still read the documentation. There is probably a #define that you should be using. No writing of magic 4's into code.

    Furthermore, file_type might be a bitmask of various things ... 4 might not be the only thing to be a directory. (Like, a symlink pointing to a directory might have both bits set, and != 4.)

    There is no portable way to do it really. Depends on what OS you are using and such.
    stat() isn't portable? (Does opendir, stat, & friends only work on Mingw/every compiler but MSVC++?)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    I'd advise you to use stat(2) and then use the macro S_ISDIR to filter out all the directories. Please refer to the man page of stat(2) for more information.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    17
    Cactus_Hugger - I did take your pointer and read the documentation and I did end up using the #define DT_DIR instead of 4. That seems to be working.

    I will also read the docs for stat() like you and mageshd84 seem to indicate.

    I haven't done C in Linux for sometime so I had totally forgot about reading the man pages. I won't make that mistake again.

    I remember struggling through programming back in the days before Al Gore invented the Internet. Sometimes I fall victim to relying on asking questions rather than seeking answers on my own.

    Thank you to all who answered here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM